【问题标题】:Nginx + Node JS + Express JS producing 502 errorNginx + Node JS + Express JS 产生 502 错误
【发布时间】:2019-10-22 02:36:00
【问题描述】:

我现在已经尝试了两天来让我的服务器运行......我想我已经有大约 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;
    }

但我也测试了非常简单的配置,例如:

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 ...

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

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

【问题讨论】:

  • NGINX 日志说明了什么?
  • 哦,顺便说一句,如果有必要的话,这个应用程序是一个非常繁重的 3D 渲染东西,所以我认为可能是超时或数据限制被超过,但我不知道如何解决这个问题到现在为止,如果这真的是一件事,我现在不知道......
  • 这是请求的访问日志:77.191.179.164 - - [06/Jun/2019:14:48:15 +0000] "GET / HTTP/1.1" 502 3693 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-" 77.191.179.164 - - [06/Jun/2019:14:48:15 +0000] "GET /nginx-logo.png HTTP/1.1" 502 3693 "http://140.82.47.236/" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
  • 77.191.179.164 - - [06/Jun/2019:14:48:15 +0000] "GET /poweredby.png HTTP/1.1" 502 3693 "http://140.82.47.236/" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-" 77.191.179.164 - - [06/Jun/2019:14:48:16 +0000] "GET /favicon.ico HTTP/1.1" 502 3693 "http://140.82.47.236/" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"
  • 这是请求的错误日志:2019/06/06 14:50:57 [crit] 15010#0: *20 connect() to 127.0.0.1:3000 failed (13: Permission denied) while connecting to upstream, client: 77.191.179.164, server: _, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "140.82.47.236" 2019/06/06 14:50:57 [crit] 15010#0: *20 connect() to [::1]:3000 failed (13: Permission denied) while connecting to upstream, client: 77.191.179.164, server: _, request: "GET / HTTP/1.1", upstream: "http://[::1]:3000/", host: "140.82.47.236" 2019/06/06

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


【解决方案1】:

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

setsebool httpd_can_network_connect on -P

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

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

您好, 乔什。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-24
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 2022-08-18
    • 1970-01-01
    相关资源
    最近更新 更多