Nginx dynamic proxy_pass doesn't resolve properly
Solution 1:
When you are using variables in a proxy_pass
directive, nginx will use runtime resolving except if :
- the target server is declared as an IP address
- the target server name is part of an upstream server group
- the target server name has already been resolved (e.g. it matches a server name in another server block)
Here, a runtime resolver won't help as localhost may not be resolved by a DNS. Also it's a waste to have runtime resolving as you can clearly avoid it here.
So, two simple solutions :
- use
127.0.0.1
- declare an upstream block if you have server names or a pool of target servers
Now you need your proxied server redirection to be correct. So either :
-
your proxy target handles the host header and you pass it through with :
proxy_set_header "Host" $host;
-
your proxy target can't handle the Host header for redirects and you need to rewrite them with nginx using :
proxy_redirect http://$proxy_host/$sub_domain http://$host;
However if it doesn't support the Host header at all, links will be broken.