2017-04-24 151 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