Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx: preserve port during redirect

I have an Nginx server listening on 80 ran inside a Docker container.

Inside the Nginx config I need to perform a redirect to a static page in specific situations.

rewrite ^ /foobar.html redirect;

The user can run the container specifying any port using the docker command line (for reference, she can expose the container on port 8000 and internally Nginx will still use 80).

Now, when Nginx redirects the URL, the port is replaced with the one used internally by Nginx instead of using the one used by Docker.

I tried to set a bunch of headers but they didn't help:

proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://app_server;

rewrite ^ /foobar.html redirect;

It still redirects to 80.

How can I tell Nginx to preserve the port used by the user?

like image 877
Fez Vrasta Avatar asked Jan 27 '17 21:01

Fez Vrasta


1 Answers

So, I found a solution, I can specify the redirect as follows:

rewrite ^ $scheme://$http_host/foobar.html redirect;

This will preserve the port.

like image 62
Fez Vrasta Avatar answered Sep 21 '22 12:09

Fez Vrasta