Node.js + Nginx抛出502错误的网关错误

时间:2018-06-02 13:16:02

标签: node.js nginx ubuntu-16.04

我刚刚将Node.js应用程序安装到开发环境中。配置是: Ubuntu 16.x PHP 7.0 Node.js 8.x Mysql的 phpMyAdmin的 nginx的

我的节点应用正在使用端口2000,子文件夹名称是nodeapp。虽然phpmyadmin正常打开,但Node app正在提供502 Bad gateway。

这是nginx conf文件:

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

    root /home/pjsp/public_html;
    index index.php index.html index.htm index.nginx-debian.html app.js;

    server_name mydomain.com;

    location /nodeapp {
    proxy_pass http://localhost/nodeapp:2000;
    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;
}
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

}

以下是我在/var/log/nginx/error.log文件中收到的错误:

2018/06/02 13:13:15 [error] 32209#32209: *763 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server: mydomain.com, request: "GET /nodeapp:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000

请帮忙!

  

更新

新配置文件:

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

    root /home/pjsp/public_html;
    index index.php index.html index.htm index.nginx-debian.html app.js;

    server_name app.pajasa.com www.app.pajasa.com;

    location /nodeapp {
    proxy_pass http://localhost;
    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;
}
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

}

现在收到错误:

2018/06/02 14:52:35 [alert] 3026#3026: *765 768 worker_connections are not enough while connecting to upstream, client: 127.0.0.1, server: app.pajasa.com, request: "GET /nodeapp:2000 HTTP/1.1", upstream: "http://127.0.0.1:80/nodeapp:2000", host: "www.app.pajasa.com"

url:www.app.pajasa.com/nodeapp:2000

3 个答案:

答案 0 :(得分:0)

README.md

您的网址方案错误,它始终是PROTO:// DOMAIN:PORT / PATH

答案 1 :(得分:0)

你的nginx中有一个无限循环。这就是你看到的原因:

/nodeapp:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000:2000....

如果您的Node应用正在侦听端口2000,请不要在Nnginx上使用listen 2000;

在您删除之后,请求:http://localhost/nodeapp将传递到您的节点应用。

此外,您的proxy_pass不正确,应该是:

 proxy_pass http://localhost:2000;

<强>更新

您的网址错误

www.app.pajasa.com/nodeapp:2000 // INCORRECT

首先,正如我们已经提到的,如果你使用Nginx代理你的Node.js APP,你不必在URL上添加端口,其次更重要的是不是端口如何工作:

www.app.pajasa.com:2000 // This is correct

从网址中删除:2000,Nginx会将其代理到您的节点应用。

www.app.pajasa.com/nodeapp

答案 2 :(得分:-1)

    listen 2000;

你的nginx不应该听app端口。在这种情况下,nginx会以递归方式调用它。