nginx重定向并在url中保留非标准端口

wbrvyc0a  于 2022-11-22  发布在  Nginx
关注(0)|答案(1)|浏览(196)

我有一个像https://app1.company.com:5555这样的端点,希望能够在所有页面的url中使用端口号浏览网站,也能够在没有端口号的情况下浏览,比如说https://dev-app1.company.com的其他server_name
因此,例如https://app1.company.com:5555/tag/generalhttps://dev-app1.company.com/categories/ulmighty应该都可以工作
我如何让nginx重定向并在端口存在时保持端口的名称?
目前有这个

server {
    listen 80;
    server_name dev-app1.company.com app1.company.com;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name dev-app1.company.com app1.company.com;

    location ^~ / {

        rewrite ^/(.*)$ /$1 break;
        proxy_pass http://localhost:9090;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

但问题是它不使用端口号进行重定向,我希望它能够使用url中的端口号进行重定向,只要服务在该端口上运行,并且它在5555端口上运行
最新消息:
应用程序已在侦听端口5555,我可以在https://app1.company.com:5555访问此处
当我有了这个

server {
    listen 80;
    server_name app1.company.com;

    return 301 https://app1.company.com:5555$request_uri;
}

但现在我想添加更多的服务器名,这样我也可以访问其他没有端口的服务器名

ds97pgxw

ds97pgxw1#

通过为标准和非标准端口提供额外的服务器块,可以解决此问题
下面是最终设置

server {
    listen 80;
    server_name dev-app1.company.com;

    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name dev-app1.company.com;

    location ^~ / {

        rewrite ^/(.*)$ /$1 break;
        proxy_pass http://localhost:9090;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}
server {
    listen 80;
    server_name app1.company.com;

    return 301 https://app1.company.com:5555$request_uri;
}

server {
    listen 443 ssl;
    server_name app1.company.com;

    location ^~ / {

        rewrite ^/(.*)$ /$1 break;
        proxy_pass http://localhost:9090;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
}

相关问题