防止NGINX删除端口

14
我希望在我的重写中动态保持ServerName和Port: 假设防火墙将端口8081重定向到80。 因此,如果我使用“192.168.1.123/frontend”或“my.domain.tld:8081/frontend”访问Web服务器,则应重定向到“192.168.1.123/frontend/”或“my.domain.tld:8081/frontend/”。
如果我使用普通的redirect rewrite ^(.*[^/])$ $1/ permanent;并访问端口8081,则会删除端口。(我已经尝试过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邮件列表! 我用重写规则解决了这个问题:

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

Nginx并不知道请求中有端口,因此无法阻止它。但是你可以重定向到完整的URI。 - Alexey Ten
无论如何,将防火墙重定向端口8081到80是一个奇怪的想法。 - Alexey Ten
@AlexeyTen为什么nginx不知道有一个端口?如果我使用my.domain.tld:8081/frontend/访问网站,nginx不会删除端口,只有在没有斜杠结尾的情况下访问时才会删除端口。 - Gurkengewuerz
Nginx不会移除端口,因为它不会重定向您。它不关心您在浏览器中看到的内容。但是它接受在80端口的连接,并且这是nginx知道的唯一端口。当您访问没有斜杠的URL时,nginx会将您重定向到带有斜杠的URL,此时生成的URL将不包含端口(因为80是默认端口)。 - Alexey Ten
当我测试其他人报告成功的各种选项时,它一开始并没有起作用。我不得不清除网站的缓存(例如通过使用开发者工具->网络->[x]禁用缓存)才最终看到成功的重定向。 - Leon
3个回答

13

我终于找到了解决你所描述问题的方法。我通过URL重写使其工作,但这似乎有点过度设计。

因此,对于任何遇到相同问题的人,最干净的解决方案是将此部分替换为:

proxy_set_header Host $host;

用这个:

proxy_set_header Host $http_host;

使用这种设置,Nginx将保留您重定向中的端口,无论您的防火墙配置如何。

希望这可以帮助。干杯!


1
你好,这是什么的解释?我非常感兴趣为什么它能够工作。 - nurgasemetey
也适用于我并解决了我的问题,由于缺少斜杠尾巴,oAuth重定向localhost:8000到localhost...现在按预期工作。 - sebastienbarbier

2

我的一个重要部分...大约三分之一的文件... /etc/nginx/sites-enabled/Site.conf

这里可能有一些有用的东西...一切都运行正常...我的nginx也进行了调优。 我的ssl在各方面都得到了100%的评价,我暴露的端口也有非常长且复杂的密码,我不得不将它们写下来以便记忆,或者只是开发和测试垃圾,不会造成任何伤害。但是...为了你自己的安全,请修改为你自己的值。

    ####################################################
    upstream dev {
        server 127.0.0.1://port// weight=1 fail_timeout=300s;
        keepalive 16;
      }
    ####################################################
    upstream l33t {
        server 127.0.0.1://port// weight=1 fail_timeout=300s;
        keepalive 16;
      }
    ####################################################
    upstream authserver {
        server 127.0.0.1://PORT// weight=1 fail_timeout=300s;
        keepalive 16;
      }


    #######################
    #  whereyougoing :80  #
    #######################

#nowhere..         you're going.... nowhere...

    ######################
    #   - FORCE HTTPS -  #
    ######################

    server {
        listen 80;
        server_name YOURSITE.COM;
        add_header Strict-Transport-Security max-age=2592000;
        rewrite ^/.*$ https://YOURSITE.COM permanent;
    }

    server {
        listen 80;
        server_name www.YOURSITE.COM;
        add_header Strict-Transport-Security max-age=2592000;
        rewrite ^/.*$ https://www.YOURSITE.COM permanent;
    }


    server {
        listen 80;
        server_name auth.YOURSITE.COM;
        add_header Strict-Transport-Security max-age=2592000;
        rewrite ^/.*$ https://auth.YOURSITE.COM permanent;
    }

    server {
        listen 80;
        server_name its.YOURSITE.COM;
        add_header Strict-Transport-Security max-age=2592000;
        rewrite ^/.*$ https://its.YOURSITE.COM permanent;
    }


    ######################################################
    #############  SSL SERVER starts here  ###############
    ######################################################

    server {

        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        server_name YOURSITE.COM www.YOURSITE.COM auth.YOURSITE.COM its.YOURSITE.COM;
        root /var/www/wordpress;
        index index.php index.htm index.html;
        access_log /var/log/nginx/rocketstack_ssl_access.log;
        error_log /var/log/nginx/rocketstack_ssl_error.log;

    #######################################
    #            Lock it down             #
    #######################################

    # SSL certificate locations
        ssl_certificate /etc/letsencrypt/live/YOURSITE.COM/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/YOURSITE.COM/privkey.pem;

    # Exclusions
        include snippets/exclusions.conf;

    # Security
        include snippets/security.conf;
        include snippets/ssl.conf;

    # Fastcgi cache rules
        include snippets/fastcgi-cache.conf;
        include snippets/limits.conf;
        include snippets/nginx-cloudflare.conf;

    ############################################
    #             port-authority               #
    ############################################

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

    ############################################
    #                Locations                 #
    ############################################

        location / {
            try_files $uri $uri/  /index.php?$args;
        }

        location /FOO {
            alias /var/www/devl;
            index index.php index.html index.htm;
            try_files $uri $uri/               /index.php?$args;
            autoindex                          on;
        }

        location /BAR {

              proxy_set_header Origin           http://$host;
              proxy_set_header Host             $http_host:$server_port;
              proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
              proxy_set_header Upgrade          $http_upgrade;
              proxy_set_header Connection       $http_connection;
              proxy_http_version 1.1;
          }


    ################# Fastphp accelleration #############

        location ~ \.php$ {
            try_files $uri =404;
            include snippets/fastcgi-params.conf;
            fastcgi_pass unix:/run/php/php7.2-fpm.sock;

    # Skip cache based on rules in snippets/fastcgi-cache.conf.

            fastcgi_cache_bypass     $skip_cache;
            fastcgi_no_cache         $skip_cache;

    # Define memory zone for caching.

            fastcgi_cache rocketstack;

    # Define caching time.

            fastcgi_cache_valid 60m;

    #increase timeouts

            fastcgi_read_timeout 3000;
            fastcgi_connect_timeout 3000;
            fastcgi_send_timeout 3000;
            proxy_read_timeout 3000;
            proxy_connect_timeout 3000;
            proxy_send_timeout 3000;
            send_timeout 3000;

    # Flexible SSL to be used So the server can talk non-ssl internally

            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-NginX-Proxy true;
        }
    }

    ##############################################
    ###########    Server ends here    ###########
    ###########                        ###########
    ###########  Call upstream starts  ###########
    ###########                        ###########
    ##############################################



          #######################
          #     auth-serve      #
          #######################

    server {
        listen 9001 ssl;

    #############  Lock it down  ################

    # SSL certificate locations
        ssl_certificate /etc/letsencrypt/live/YOURSITE.COM/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/YOURSITE.COM/privkey.pem;

    # Exclusions

        include snippets/exclusions.conf;

    # Security

        include snippets/security.conf;
        include snippets/ssl.conf;

    # Fastcgi cache rules

        include snippets/fastcgi-cache.conf;
        include snippets/limits.conf;
        include snippets/nginx-cloudflare.conf;

    ###########  Send to Location upstream ##############

        location /authserver {
            proxy_redirect /*                 /$1;
            proxy_pass http://authserver/;
            proxy_set_header Origin           $host;
            proxy_set_header Host             $host:$server_port;
            proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header Upgrade          $http_upgrade;
            proxy_set_header Connection       $http_connection;
            proxy_http_version 1.1;
        }

0

我已经尝试在虚拟主机中进行上述配置。

我也有类似的情况,我在家庭网络中有一个odoo实例需要从互联网访问,但我的ISP阻止了我的80;443端口,所以我必须使用其他端口,在我的路由器中使用端口转发8059转发到192.168.1.106:443

第一次尝试时,当我输入时:

erp.example.com:8059

返回 [not working] erp.example.com/web,其中 Nginx 会自动删除我输入的端口号。
尝试 #2 输入。

erp.example.com:8059/web

   Return [successful] erp.example.com:8059/web/login

尝试第三个应用程序 [Nextcloud] 已输入

nextloud.example.com:8060

   Return [successful] See, Nextcloud working well, is apication problem or  Nginx?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接