Nginx + Node JS + Express JS产生502错误

时间:2019-06-06 14:39:16

标签: node.js express nginx server bad-gateway

我现在已经尝试了两天,以使服务器正常运行...我想我已经拥有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;
    }

但是我也测试了非常简单的conf,例如:

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

顺便说一句:我以非root用户身份通过​​pm2 start server.js通过systemctl和pm2像往常一样运行fedora 30和nginx。 我也使用UFW和禁用的防火墙,但是UFW中允许使用WWW ...

我非常感谢你们给我的所有帮助和建议。

我只是不知道该怎么办:(...

1 个答案:

答案 0 :(得分:0)

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

setsebool httpd_can_network_connect on -P

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

(13: Permission denied) while connecting to upstream:[nginx]

问候, 乔什。

如果需要,可以将页面签出,并警告其18+内容:D 我知道在IO设备上有一个画布Bug,无法在其中使用。 而且由于资产繁重,我不建议您使用有限的数据计划(约70Mb的3D资产)。请与我联系,以了解它是否有效(它只是更大的实验:))。 IP是:140.82.47.236