Below is the bare-bones stripped down Nginx config that demonstrates the problem I'm having directly. The "real-world" setup has multiple upstreams and multiple conditional checks that are omitted for clarity.
upstream barhost {
server example.com;
}
server {
listen 8080;
location / {
# this works fine if used directly:
# proxy_pass http://example.com/;
# this doesn't work, returns 404:
proxy_pass http://barhost/;
}
}
Results:
- Using
proxy_pass http://example.com/;
works perfectly fine, returns 200
- Using
proxy_pass http://barhost/;
(using upstream
) it returns 404
Some background info:
What am I doing wrong here?
Somewhat related posts:
UPDATE:
Thanks to a helpful user in the comments, I've investigated the request header that is proxied to example.com in this scenario. It's being set to barhost
which the server will give an invalid response to. This is the root cause of the issue.
So, with that known, I would like to set the Host
header properly.
Setting the upstream name to match the desired Host
header name does work:
upstream example.com {
server example.com:80;
}
Hard coding Host
header with proxy_set_header
also seems to work:
upstream barhost {
server example.com;
}
server {
listen 8080;
location / {
# This works:
proxy_set_header Host example.com;
# None of these work:
# proxy_set_header Host $host;
# proxy_set_header Host $http_upstream_host;
# proxy_set_header Host $proxy_host;
proxy_pass http://barhost/;
}
}
However, in my particular use-case it would ideal to instead set it dynamically using proxy_set_header
using a variable - is that possible?