【问题标题】:Status 502 in NginxNginx 中的状态 502
【发布时间】:2017-07-06 02:00:41
【问题描述】:

我在 CentOS 7 服务器上配置了 Nginx。我有 2 个站点正在运行,我使用 Cerbot 安装了 SSL 证书,并且该过程执行没有错误。我有 2 个子域,x.mydomain.com 和 y.mydomain.com

我正在运行的站点对应于在它们自己的端口上运行的应用程序,其中一个为 9100,第二个为 9200,因此我配置了 Nginx 以将请求重定向到相应的端口。例如,第一个应用程序的服务器块是:

   server {
                listen          80;
                server_name     x.mydomain.com;
                access_log      logs/mydomainX.log main;

                location / {
                        proxy_pass      http://127.0.0.1:9100;
                }

                listen 443 ssl; # managed by Certbot
                ssl_certificate /etc/letsencrypt/live/x.mydomain.com/fullchain.pem; # managed by Certbot
                ssl_certificate_key /etc/letsencrypt/live/x.mydomain.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot


    if ($scheme != "https") {
        return 301 https://$host$request_uri;
    } # managed by Certbot

}

所以,如果我在浏览器中打开x.mydomain.com,我会从 Nginx 获得状态代码 502,但是,如果我直接使用 IP:xxx.xxx.xxx.xxx.xxx:9100 打开,那么我可以看到该站点。所以我在 Nginx 配置中一定有问题,我错过了什么?谢谢

【问题讨论】:

    标签: nginx centos centos7


    【解决方案1】:

    状态码 502 表明 nginx 可以连接到代理上游,所以 127.0.0.1:9100 上的上游有问题。

    如果对 xxx.xxx.xxx.xxx.xxx:9100 的请求工作正常,您可以将您的 nginx 配置更改为如下内容:

    server {
        listen          80;
        server_name     x.mydomain.com;
        access_log      logs/mydomainX.log main;
    
        location / {
               proxy_pass      http://xxx.xxx.xxx.xxx.xxx:9100;
        }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/x.mydomain.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/x.mydomain.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    
    
        if ($scheme != "https") {
            return 301 https://$host$request_uri;
        } # managed by Certbot
    
    }
    

    如果您仍想为 proxy_pass 使用 127.0.0.1:9100,您应该检查您的应用程序,可能它没有绑定到 127.0.0.1。

    【讨论】:

    • 我成功了,但没有任何改变。不知道如何调试它
    • @SrednyMCasanova 请显示来自 nginx error.log 的输出。
    • 我遇到了这个错误:" connect() to 192.168.10.106:8082 failed (13: Permission denied) while connected to upstream" 但我设法修复它只是输入setsebool -P httpd_can_network_connect 1
    猜你喜欢
    • 1970-01-01
    • 2019-05-29
    • 2022-01-02
    • 1970-01-01
    • 1970-01-01
    • 2016-04-24
    • 2012-05-15
    • 2020-05-15
    • 2021-03-29
    相关资源
    最近更新 更多