Nginx – How to 301 redirect with NginX without adding port number

nginxredirect

I need to 301 redirect the contents of an entire directory to another directory with nginx. While my configuration works, it unfortunatelly also exposes the port number. This is I guess now a side effect since the inbound traffic for the specific domain is handled via HAProxy ACL rules to the 8000 port, while the url itself is port 80.

This is my nginx conf:

server {
    listen       8000;
    port_in_redirect off;
    location ~* ^/data/dir1/(.*)$ {
    return 301 /data/dir2/$1;
}
...

It redirects from URL:

domain.com/data/dir1/…

to:

domain.com:8000/data/dir2/…

How could I get rid of the 8000 inside the URL redirect?

The tmp workaround I have is to put :80 into the redirect but I fear that this will impact the URL google is listing as it includes a port number.

Best Answer

Can you try the following:

server {
    listen       8000;
    location ~* ^/data/dir1/(.*)$ {
    return 301 http://example.com/data/dir2/$1;
}