Nginx 作为 CentOS 7 上 Node.js 应用程序的反向代理

Nginx as reverse proxy for Node.js app on CentOS 7

我在服务器 port 5000 上有一个节点应用程序 运行,可以从我的浏览器访问。未安装 Apache。我按照安装和配置 Nginx 的指南进行操作,我能够成功打开为 centos Nginx 安装提供的默认 HTML 页面,这意味着端口 80 已打开(尚无 SSL)。然后我通过添加以下内容编辑了 /etc/nginx/nginx.conf 中的 location 设置:

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://localhost:5000;
    }

sudo systemctl restart nginx 成功重启后,我现在看到一个默认错误页面,它是 nginx 安装附带的,而不是我在 server_ip:5000 上运行的应用程序。我如何正确调试它并找出反向代理不起作用的原因?

我的完整 nginx.conf(我只删除了评论的 SSL 设置):

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 2048;

    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 / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_pass http://localhost:5000;
        }

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

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

我收到的错误页面是:/usr/share/nginx/html/50x.html

请更新您的 server 块,如下所示:

  server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;

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

        location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:5000;
        proxy_redirect off;
        }

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

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

此外,请确保您的应用程序正在侦听 localhost:5000。有时,该服务仅绑定到接口 IP 而不是本地主机。

如果问题仍然存在,请检查您的 nginx 错误日志并提供日志中的错误消息。

连同 nginx 配置文件,这是默认情况下禁用的 selinux 模块 httpd_can_network_connect 的问题。您可以通过以下命令启用它:

setsebool -P httpd_can_network_connect on

可以在 https://wiki.centos.org/TipsAndTricks/SelinuxBooleans

找到更多详细信息