Dominik Krulak
Dominik Krulak

Reputation: 611

Hide port number in URL exposed by NGINX Docker container

I run Docker NGINX container with exposed ports 8080 and 8443.

So I visit welcome NGINX page at: http://www.nginx.test:8080


I'm trying to figure out IF it's possible to hide/remove port in URL.

So I visit welcome NGINX page at: http://www.nginx.test


Since the NGINX is reverse proxy server it should work. Still new to NGINX and I had to Google some answers and basically they are applied in tries bellow.

First I tried (didn't work) NGINX's directive proxy_redirect at server level like this:

server {
    listen 8080;
    listen [::]:8080;
    server_name www.nginx.test;

    proxy_redirect $scheme://$server_name:8080 $scheme://$server_name;
}

Then I tried (didn't work) NGINX's directive proxy_pass at location level like this:

server {
    listen 8080;
    listen [::]:8080;
    server_name www.nginx.test;

    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass $scheme://$server_name;
    }
}

Probably there's more than that to make it work.

Upvotes: 3

Views: 3298

Answers (1)

zigarn
zigarn

Reputation: 11595

http://www.nginx.test is interpreted as http://www.nginx.test:80 (and with SSL https://www.nginx.test==https://www.nginx.test:443).

So you have to listen on port 80 if you want to drop it from the URL.

With docker, you can map a port of the host to a different port in the container.
So without changing the content of your NGINX container, you can simply map port 80 of your docker server to port 8080 of your nginx container:

docker container run ... --publish 80:8080 your_image ...

Upvotes: 3

Related Questions