【问题标题】:How nginx proxy to express(node.js)?Why res.data is index.html?nginx代理怎么表达(node.js)?为什么res.data是index.html?
【发布时间】:2017-12-24 08:14:17
【问题描述】:

我用Express框架写了一个Node.js服务器项目。在localhost上没问题。但是当我在远程服务器上构建时出现了一些问题。(centos 6.5; ngnix 1.11.6; express 4.14.1; node 6.9。 5)

  1. 这是 nginx.conf。

     listen       80;
     server_name  www.cheeseyu.cn cheeseyu.cn;
    
     #charset koi8-r;
    
     #access_log  logs/host.access.log  main;
    
     location / {
         proxy_pass http://127.0.0.1:3009;
         proxy_redirect off; 
     }
    
     location ~ .*.(gif|jpg|jpeg|png|bmp|swf|js|css|woff|ttf|TTF|svg)$ {
         root /home/www/blog;
         if (-f $request_filename) {
             expires 100d;
             break;
         }
     }
    
     error_page 405 =200 @405; 
     location @405 { 
         proxy_method GET;
         proxy_pass http://static_resource;
     }
    
     #error_page  404              /404.html;
    
  2. 有xhr的信息。 enter image description here enter image description here

  3. 它是 node.js。

var express = require('express');
var path = require('path');
var app = express();
var bodyParser = require('body-parser');
var routes = require('./routes');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.all('', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "");
    res.header("Access-Control-Allow-Headers", "X-Requested-With,Content-Type");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
});

routes(app);
app.set('port', process.env.PORT || 3009);
app.listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
});

所以我的问题是:
  • 一个。为什么状态是200,即使我停止了后台服务器?
  • 乙。为什么响应数据是 html(内容是 index.html)?
  • c。为什么状态仍然是 405,当我使用帖子时?我尝试过任何 我找到的方法。
  • d.为什么状态为 200,但请求后不要使用“.then”,而是使用 '.catch'?
  • e. nginx代理如何表达(node.js)?(我认为最重要的问题是 nginx 没有将请求代理到节点服务器。)

如需了解回复详情,请访问cheeseyu.cn
谢谢你的帮助:)

【问题讨论】:

    标签: node.js express nginx proxy


    【解决方案1】:

    这个设置没有任何问题。只是因为我重新打开了nginx,但是没有用。你应该停止nginx并打开nginx

    【讨论】:

      【解决方案2】:

      我可以告诉你我在 nginx conf 中使用了什么

          location / {
          proxy_pass http://your-domain.com:3009;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection 'upgrade';
          proxy_set_header Host $host;
          proxy_cache_bypass $http_upgrade;
      }
      

      这和一个进程管理器是我在服务器上运行我的第一个节点应用程序所需要的。 作为流程管理器,我使用了 stableloop。 重要提示:你还必须检查你的节点进程正在运行的巫婆端口,并让你的端口适应它。

      希望对你有所帮助。

      (所有更改后你必须$~ service nginx reload

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 2016-12-01
        • 2021-08-02
        • 1970-01-01
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多