nginx proxy_pass is setting port in response
17,872
You have to set the following options:
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
This is a sample that works:
server {
listen 80;
listen 443;
server_name api.mysite.dev;
location /api/ {
proxy_pass http://127.0.0.1:8001/;
proxy_redirect off;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header Host "api.mysite.dev";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
Author by
Colin Grogan
Updated on June 14, 2022Comments
-
Colin Grogan almost 2 years
I have an Nginx config similar to:
server { listen 80; listen 443; server_name api.mysite.dev; location / { proxy_set_header Host "api.mysite.dev"; proxy_set_header X-Real-IP $remote_addr; proxy_pass $scheme://127.0.0.1:8001; } } server { listen 80; listen 443; server_name mysite.dev www.mysite.dev; # Forward all /api/ requests ti api.mysite.dev # sadly need to keep this around for backwards compatibility location /api/ { proxy_set_header Host "api.mysite.dev"; proxy_set_header X-Real-IP $remote_addr; proxy_pass $scheme://127.0.0.1:8001/; } # The proxy_pass here ends up putting the port (8002) in the response URL. location / { proxy_set_header Host "www.mysite.dev"; proxy_set_header X-Real-IP $remote_addr; proxy_pass $scheme://127.0.0.1:8002; } }
So, as said in the comment, when I request www.mysite.dev, my browser is forwarded to www.mysite.dev:8002. Any ideas what I'm doing wrong here?
Thanks in advance!