Home > Enterprise >  nginx redirect with keeping non-standard port in url
nginx redirect with keeping non-standard port in url

Time:11-04

I have an endpoint like https://app1.company.com:5555 and will like to be able to browse the website with the port number in the url for all pages and also be able to browse without the port number at let say the other server_name of https://dev-app1.company.com

so for example https://app1.company.com:5555/tag/general , https://dev-app1.company.com/categories/ulmighty should all work

how do I get nginx to redirect and keep port in name whenever the port is there?

currently have this

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;
    }
}

but the issue is it does not redirect with the port number, i want it to be able to redirect with the port number in url as long as the service is running on that port and it is running on the 5555 port

UPDATE:

App is listening on port 5555 already and i can access here at https://app1.company.com:5555

when i have this

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

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

but now i want to add more server names so i can also access the other server names with no port at all

CodePudding user response:

Was able to fix this with having an extra server block for the standard and non-standard port

so here is final setup

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;
    }
}
  • Related