Score:0

nginx redirect multiple server names with non-standard port in url

bv flag

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

us flag
Well first of you need Nginx to listen to whatever alternate port number you want to listen to. Currently it only listen to port 80 (http) and 443 (https). If you want to serve port 5555 from Nginx, then you need to add `listen 5555` ... and also decide if that port is encrypted or not.
djdomi avatar
za flag
in case you already have another service listen for 5555, then for the moment which states the reverse proxy should redirect the request to 9090 and not as you seems to 5555?
uberrebu avatar
bv flag
it is listening on port 5555 as i mentioned @LasseMichaelMølgaard
uberrebu avatar
bv flag
@djdomi i added update to bottom of question, i already have the app listening on port 5555 and can access there but now i wan tto add more server names so i can also access with no port in url
Score:0
bv flag

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;
    }
}
djdomi avatar
za flag
that was what I meaned. ;) my glass ball was working with success
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.