2017-04-24 124 views
0

我有一台VM Ubuntu15.04服务器,并且我配置了nginx来侦听端口80上的请求并将它们转发到不同端口上的相应应用程序。我有一个简单的node.js服务在端口3000上运行,它有一个GET和POST服务。我已经使用PM2启动它,并在我的nginx默认配置文件中添加了一个proxy_pass到localhost:3000 /。问题是,当我尝试使用GET请求它工作正常,但在POST的情况下,它显示404错误。我试图通过邮递员客户端使用POST服务。Nginx无法服务node.js 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; 
    #  } 
    #} 

任何参考,教程,建议或解决方案,请让我知道如何做到这一点。

回答

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上工作,那么你首先需要解决这个问题。

0

使用try_filesproxy_pass在相同的location块可能不会工作。

如果你想nginx来测试一个静态文件和其他代理一切的存在,使用一个名为位置:

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

测试使用nginx -t作为似乎缺少一个右}你的问题的配置。

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