Reputation: 14825
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?
Upvotes: 10
Views: 5642
Reputation: 3076
In my case the browser was caching the redirect URL before I correctly set port_in_redirect on;
. So disable browser caching when debugging nginx rules.
Upvotes: 1
Reputation: 3165
I solved the same problem of broken redirects inside docker by updating the Host definition in the header as defined in the nginx.conf file:
location / {
proxy_set_header Host $http_host;
$http_host is a lower case value of $host with the port included.
Upvotes: 0
Reputation: 14825
So, I found a solution, I can specify the redirect as follows:
rewrite ^ $scheme://$http_host/foobar.html redirect;
This will preserve the port.
Upvotes: 10