首页 > 解决方案 > Nginx + Node JS + Express JS 产生 502 错误

问题描述

我现在已经尝试了两天来让我的服务器运行......我想我已经有 30 个不同的 nginx 配置并尝试了不同的端口和所有东西。我现在很不高兴,因为那些为设置 nginx + 节点服务器做教程的人甚至都没有提到错误,一切似乎都很简单,但显然不是......

我让 nginx 服务器以其标准配置运行:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
    listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

    error_page 404 /404.html;
            location = /40x.html {
        }

    error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

哪些服务器成功地为我提供了默认测试页面...

这是我通过 pm2 运行的 server.js:

var express = require('express'),
  path = require('path'),
  app = express();

//set the port
app.set('port', 3000);

//get node files i need
app.use('/fa', express.static(__dirname + '/node_modules/@fortawesome/fontawesome-free/'));

//tell express that we want to use the www folder
//for our static assets
app.use(express.static(path.join(__dirname, 'www')));

// Listen for requests
var server = app.listen(app.get('port'), function () {
  console.log('The server is running on http://localhost:' + app.get('port'));
});

所以我测试的最后一个配置是原始的,只是将位置块替换为:

location / {
        proxy_pass http://localhost:3000;
        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;
    }

但我也测试了非常简单的confs,比如:

events {
    worker_connections 1024;
}

http {
    server {
    listen       80;

        server_name  _;

        location / {
            proxy_pass http://localhost:3000;
            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;
        }   
    }
}

我只是不知道出了什么问题...

通过 curl http://localhost:8080我收到了我的 html,但没有收到我给定的服务器 IP ...

netstat 给了我这个:

netstat -tulpn | grep :80
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               
    LISTEN      14841/nginx: master 
    tcp6       0      0 :::80                   :::*                    
    LISTEN      14841/nginx: master

netstat -tulpn | grep :3000
    tcp        0      0 127.0.0.1:3000          0.0.0.0:*               
    LISTEN      14375/node /home/no

顺便说一句。:我一如既往地通过 systemctl 运行 fedora 30 和 nginx,并且通过 pm2 start server.js 以非 root 用户身份运行 pm2。我也使用 UFW 并禁用了 firwalld,但在 ufw 中允许使用 WWW ...

我真的很感谢你们能给我的所有帮助和建议。

我根本不知道该怎么办:(...

标签: node.jsexpressnginxserverbad-gateway

解决方案


好的,我现在想通了。这是一个 SELinux 规则错误。拒绝我通过 http 连接到我的服务器。

setsebool httpd_can_network_connect on -P

永久修复它。这篇文章回答了我的问题:

(13:权限被拒绝)同时连接到上游:[nginx]

问候,乔希。

如果您愿意,您可以查看该页面,警告其 18+ 内容:D 我知道 IO 设备存在画布错误,无法在那里工作。而且由于资产繁重,我不建议您使用有限的数据计划(大约 70Mb 的 3D 资产)。如果它工作正常与我联系(它只是一个更大的实验:))。IP为:140.82.47.236


推荐阅读