Nitish Phanse
Nitish Phanse

Reputation: 562

Removing Port number from nginx running on non standard port

I have an nginx server running on port 9094, and it listens to a server name which is mapped to localhost in the etc/hosts file.

server {
        listen 9094;
        listen [::]:9094;
        server_name www.myserver.local;
        port_in_redirect off;
        server_name_in_redirect off;

        proxy_set_header Host $host:$server_port;

        location /portal/foreman/ {
                rewrite ^/portal/foreman(.*)$ $1 break;
                proxy_pass http://localhost:3001;
                proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /api/ {
                rewrite ^/api(.*)$ $1 break;
                proxy_pass  http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /login {
                rewrite ^/login(.*)/$ /$1 permanent;
                proxy_pass http://localhost:3000;
                proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
}

In my /etc/hosts file i have the following entry

127.0.0.1  www.myserver.local

When i enter www.myserver.local:9094 everything works as usual, with the redirection. Can someone help me with a way i can remove the port number and the thing still works eg: www.myserver.local/login will redirect me directly to my login page..

when i run www.myserver.local in my browser, the browser gives an error saying, refused to connect

Upvotes: 2

Views: 5159

Answers (2)

Innocent Anigbo
Innocent Anigbo

Reputation: 4822

You have to change your config to listen on port 80 and the restart nginx. The config should look as below

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

use netstat -tulpn | grep 80 to ensure the server is listening on this port

Upvotes: 2

Kris
Kris

Reputation: 438

A browser uses one of these three ports.

  1. 80 (HTTP)
  2. 443 (HTTPS) or
  3. 8080 (HTTP Alternate)

So if you want to visit a non standard port from a browser, you need to specify the port in the url. It won't work otherwise.

Upvotes: 2

Related Questions