【问题标题】:Removing port from nginx redirect从 nginx 重定向中删除端口
【发布时间】:2013-05-19 11:43:25
【问题描述】:

我遇到了一个问题,即网站上的某些重定向包含代理通行端口,导致它们无用。我的配置如下:

物理服务器 1:

server {
    server_name example.com www.example.com;

    location / {
            proxy_pass              http://1.1.1.1:50493;
            proxy_set_header        X-Real-IP  $remote_addr;
            proxy_set_header        Host "example.com";
    }
}

物理服务器 2:

server {
            listen 50493;
            server_name example.com www.example.com;

            set_real_ip_from    1.1.1.1;

            root /var/www/example.com;
            index index.php index.htm index.html;

            location / {
                    if ($http_host !~ "^example.com"){
                            set $rule_0 1$rule_0;
                    }
                    if ($rule_0 = "1"){
                            rewrite ^/(.*) http://example.com/$1 permanent;
                    }
                            rewrite /[^/]+/([0-9]+)-[^/]+.html http://example.com/showthread.php?t=$1 permanent;
            }

            location ~ .php$ {
              fastcgi_pass   127.0.0.1:9000;
              fastcgi_index  index.php;
              fastcgi_param  SCRIPT_FILENAME /var/www/example.com$fastcgi_script_name;
              include fastcgi_params;
              fastcgi_read_timeout 600s;
              fastcgi_buffer_size 512k;
              fastcgi_buffers 16 512k;
            }
   }

通常,浏览工作正常。该网站可按预期浏览。但是,一些重定向的链接(例如,在登录操作之后)会重定向到包含端口 50493 的链接。例如,我们得到http://example.com:50493/index.php。这是行不通的。我的问题是,如何删除端口?

据我所知,论坛软件从 php 会话端口变量中获取端口。我尝试将 port_in_redirect 设置为 off,但无济于事。如果有帮助,此处突出显示的问题:http://kb.parallels.com/en/114425 类似。

谢谢。

【问题讨论】:

    标签: php nginx http-redirect


    【解决方案1】:

    在你的 nginx.conf 中

    http {
    
    port_in_redirect off;
    
    }
    

    【讨论】:

      【解决方案2】:

      它会在您提供的链接中告诉您如何解决问题。变化:

      <?php echo $VAR->domain->asciiName ?>:<?php echo $OPT['ssl'] ? $VAR->server->webserver->httpsPort : $VAR->server->webserver->httpPort ?>"
      

      <?php echo $VAR->domain->asciiName ?>"
      

      您必须在您的应用程序中而不是在 Nginx 中解决此问题。

      您的应用程序正在生成指向端口 50493 like this 的链接。除非您要设置一个内容过滤器来遍历所有 HTML 并将 example.com:50493/path/of/url 替换为 example.com/path/of/url,否则您必须在应用程序中修复它。

      【讨论】:

      • 这让我更近了一步,我现在有了一个脚本解决方案。谢谢。但由于它是 php-fpm 附加端口($_SERVER['SERVER_PORT']),我仍然希望修复 nginx。我可以使用 proxy_redirect 值部分修复它,但不能使用包罗万象。
      • PHP-FPM 没有附加任何东西。它正确地将 _SERVER["SERVER_PORT"] 设置为 50493,这是您的 PHP-FPM 正在侦听的端口,但它是将其附加到 URL 的并行代码。
      • 抱歉回复晚了。 Danack 的回答最终帮助了我。我在我的软件中为 $_SERVER['SERVER_PORT'] 创建了一个 if 条件。如果 php 检测到它是 50493(代理端口)我强制它到端口 80。
      【解决方案3】:

      尝试即时替换

      HttpSubModule

      sub_filter      
        'example.com:50493/' 'example.com/' ;
      sub_filter_once off;
      

      这应该可以解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-20
        • 2014-03-08
        • 2016-07-02
        • 1970-01-01
        • 2018-09-11
        • 2015-10-21
        • 2017-08-16
        • 1970-01-01
        相关资源
        最近更新 更多