Nginx无法提供node.js POST请求

时间:2017-04-24 10:48:46

标签: node.js nginx pm2 ubuntu-server

我有一个VM Ubuntu15.04服务器,我已经配置了nginx来监听端口80上的请求,并将它们转发到不同端口上的相应应用程序。我有一个简单的node.js服务在端口3000上运行,它有一个GET和POST服务。我已经通过使用PM2启动它并在我的nginx默认conf中添加了一个proxy_pass到localhost:3000 /。问题是当我尝试使用GET请求它工作正常但在POST的情况下它显示404错误。我试图通过postman客户端使用POST服务。

这是我的nginx默认conf文件

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a   clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

upstream my_nodejs_upstream {
server 127.0.0.1:3000;
keepalive 64;                                                         
}

server {
listen 80 default_server;
listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/face_rec/ServerTest;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name localhost;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;

    proxy_pass http://localhost:3000;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header   X-Forwarded-Proto $scheme;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #       include snippets/fastcgi-php.conf;
    #
    #       # With php5-cgi alone:
    #       fastcgi_pass 127.0.0.1:9000;
    #       # With php5-fpm:
    #       fastcgi_pass unix:/var/run/php5-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}}
  # Virtual Host configuration for example.com
  #
  # You can move that to a different file under sites-available/ and symlink 
  that
  # to sites-enabled/ to enable it.
  #
  #server {
  #       listen 80;
  #       listen [::]:80;

  #       server_name example.com;

  #       root /var/www/example.com;
  #       index index.html;

  #       location / {
  #               try_files $uri $uri/ =404;
  #       }
  #}

任何参考,教程,建议或解决方案请告诉我如何操作。

2 个答案:

答案 0 :(得分:1)

在同一try_files块中使用proxy_passlocation可能无效。

如果您希望nginx测试是否存在静态文件并代理其他所有内容,请使用命名位置:

root /path/to/root;
location / {
    try_files $uri $uri/ @proxy;
}
location @proxy {
    proxy_pass ...;
    ...
}

使用nginx -t测试配置,因为您的问题似乎缺少结束}

有关详细信息,请参阅this document

答案 1 :(得分:0)

如果你的问题不在于甚至包含你的Node程序的一行,那就不可能告诉你你的代码有什么问题。

另外" 404错误"还不足以知道错误是什么,因为nginx显示的错误消息与Express不同,并且知道确切的消息会让我们知道错误的来源。

您应该首先使用以下方法确保您的GET和POST处理程序都正常工作:

curl -v http://localhost:3000/your/get/path

curl -v -X POST -d 'somedata' http://localhost:3000/your/post/path

来自运行应用的同一主机。

然后添加nginx代理,重新启动nginx以确保重新加载配置,并对端口80执行相同操作。如果有任何不同,则从那里开始工作并诊断差异。

但是如果POST处理程序不能在localhost:3000上运行,那么首先需要修复它。