【问题标题】:502 Bad Gateway when redirecting on nginx在 nginx 上重定向时出现 502 Bad Gateway
【发布时间】:2015-11-02 08:55:26
【问题描述】:

我的 nginx 重定向有问题。我在 nginx 1.4.4 上工作,我有两个单独的重定向。它应该以两种方式工作: 第一个重定向:地址 address1.com 重定向到地址 address2.com -> 地址 address2.com 重定向到应用程序所在的 addres2.com:1234。

第二次重定向直接来自 ddress2.com: - address2.com 重定向到 address2.com:1234

现在的问题: - 从 address1.com 重定向到 address2.com 有效,但 address2.com 到 address2.com:port 无效。它以 502 Bad Gateway 错误结束。配置和

日志中的错误如下所示: 来自error.log的信息:

[error] : *386 connect() failed (111: Connection refused) while connecting to upstream, client: {client ip addr}, server:{server name}, request: 

“GET / HTTP/1.1”,上游:“https://127.0.0.1:{port}”,主机:“{服务器名称}”

Nginx 使用存储在 conf.d 位置的许多 .conf 文件。

address1.conf(这可行):

server {
    ### server port and name ###
    listen          {ip_addr}:443;
    ssl             on;
    server_name     address1.com;

    access_log      /var/log/nginx/address1.log;
    error_log       /var/log/nginx/address1-error.log;

   ssl_certificate      /etc/httpd/ssl/servercert.crt;
   ssl_certificate_key  /etc/httpd/ssl/private/serverkey.key;

    location / {
    rewrite ^ $scheme://address2.com redirect;
}}

address2.com conf 文件(这不是):

server {
    ### server port and name ###
    listen          {ip_addr}:443;
    ssl             on;
    server_name     address2.com;

    access_log      /var/log/nginx/address2.log;
    error_log       /var/log/nginx/address2-error.log;

    ssl_certificate      /etc/httpd/ssl/servercert.crt;
    ssl_certificate_key  /etc/httpd/ssl/private/serverkey.key;

    proxy_read_timeout   180;

    location / {

            proxy_pass https://127.0.0.1:{port};
            proxy_redirect off;

            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Ssl  on;
            proxy_set_header   X-Forwarded-Protocol $scheme;
            proxy_set_header   X-Forwarded-HTTPS on;
  }}

有趣的是,我有另一个应用程序正在使用 addr3.com -> addr3.com:port 方案,并且重定向工作得非常完美。唯一的

address2.conf 和 address3.conf 之间的区别在于应用程序工作的端口。每个地址都使用 https,防火墙上的 443 端口是开放的。

希望我的描述足够详细,如果不是让我知道的话。 几天来我一直在努力解决这个问题,但没有找到适合我的任何提示或解决方案。

如果有任何帮助,我将不胜感激。

【问题讨论】:

    标签: redirect nginx reverse-proxy


    【解决方案1】:

    问题可能出在 SELinux 上。检查它是否与sestatus 一起运行。由于某些转发对您有用,因此此命令可能是多余的,但其他人可能需要它:

    sudo setsebool -P httpd_can_network_connect 1
    

    要为特定端口启用转发(这可能是您的问题),请运行以下命令:

    sudo semanage port -a -t http_port_t -p tcp 8088
    

    将 8088 替换为相关端口。

    可能找不到命令semanage。您如何安装它取决于发行版,但您很可能可以通过谷歌搜索解决方案。

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 2014-02-22
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2018-04-15
      • 2021-05-07
      相关资源
      最近更新 更多