Nginx redirect *:port to subdomain

nginxPROXYredirect

I'm working with Nginxand I want to know how can I redirect all request with a specific port to a subdomain ?

This is my default.conf :

server{
        listen 80 default_server;
        server_name localhost;

        location / {
                root /usr/share/nginx/html;
        }
}

server{
        listen 80;
        server_name blog.mydomain.com;

        location / {
                proxy_pass   http://my-ip:8080;
        }
}

So with this I have the default mydomain.com serve the html folder, and a subdomain blog.mydomain.com serve an application running port 8080.

My problem is when I try to access directly my-ip:8080, or mydomain.com:8080 or blog.mydomain.com:8080 the server serve the application running port 8080 and I want to redirect all these requests to blog.mydomain.com without the :8080.

How can I do that ? Automatically redirect to blog.mydomain.com if I specify :8080 in the url ?

Best Answer

I think you don't quite get how it works.

NGINX is not the one serving the files on port 8080 but the application running on that port. If you want to disable port 8080 serving the files, you should bind the application to 127.0.0.1:8080 or use a firewall to prevent connecting to it.

Moreover, since your configuration shows you proxying requests to your IP (if it has been redacted I imagine it is your public address), achieving a redirect on port 8080 will lead to a redirect loop.