1

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?

Rino Bino
  • 511
  • 5
  • 21
  • Have you looked at the request that's being made to the upstream server to figure out why you're getting a 404? Is it request the correct path? Is the `Host:` header correct? – larsks Feb 07 '23 at 04:04
  • @larsks You hit the nail on the head, thanks! So it's setting `Host` to `barhost`, I would like to overwrite this dynamically with `proxy_set_header`, do you know how to do this dynamically? I've put more details and findings in the original question. If not, feel free to respond about the Host header in the form of an answer and I'll gladly accept it since you've technically answered the question. I've added some working snippets above and would appreciate your insight on my follow up `proxy_set_header` question. Thanks again. – Rino Bino Feb 07 '23 at 05:25

0 Answers0