防止NGINX移除端口 [英] Prevent NGINX to remove the port

查看:86
本文介绍了防止NGINX移除端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在重写时动态保留ServerName和Port: 假设防火墙将端口8081重定向到80. 因此,如果我使用"192.168.1.123/frontend"或"my.domain.tld:8081/frontend"访问网络服务器,则应将我重定向到"192.168.1.123/frontend/"或"my.domain.tld": 8081/frontend/"

I want to keep the ServerName and Port dynamicly on my rewrite: Lets say the Firewall redirect port 8081 to 80. So, if i access the webserver for example with "192.168.1.123/frontend" or "my.domain.tld:8081/frontend" i should be redirect to "192.168.1.123/frontend/" or "my.domain.tld:8081/frontend/"

如果我使用普通的redirect rewrite ^(.*[^/])$ $1/ permanent;,并且使用8081端口访问,则该端口被删除. (我已经尝试过port_in_redirect off;)

If i use the normal redirect rewrite ^(.*[^/])$ $1/ permanent; and i access with the port 8081 the port got removed. (I already tried port_in_redirect off;)

我几乎使用默认配置:

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

        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;
        rewrite ^(.*[^/])$ $1/ permanent;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
}

谢谢您!

解决方案: 感谢NGINX邮件列表! 我用一个重写规则解决了这个问题:

SOLUTION: Thanks to the NGINX Mailing list! I fixed this problem with a rewrite rule:

if (-d $request_filename) {
    rewrite [^/]$ $scheme://$http_host$uri/ permanent;
}

推荐答案

我终于找到了解决问题的方法.我通过URL重写使它起作用,但是这似乎有些过分了.

I finally found a solution to the problem you've well described. I made it work with URL rewriting, but it seemed a bit overkill.

因此,对于任何有相同问题的人,似乎最干净的解决方案是更换它:

So, for anyone having the same problem, it appears the cleanest solution would be to replace this :

proxy_set_header Host $host;

与此:

proxy_set_header Host $http_host;

通过此设置,无论防火墙配置如何,Nginx都会将端口保留在重定向中.

With this setup, Nginx will keep the port in your redirections, no matter you firewall configuration.

希望这会有所帮助.干杯!

Hope this helps. Cheers !

这篇关于防止NGINX移除端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆