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?
So, I found a solution, I can specify the redirect as follows:
rewrite ^ $scheme://$http_host/foobar.html redirect;
This will preserve the port.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With