Score:1

Using Nginx upstream hosts with proxy_pass

us flag

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?

pt flag
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?
Rino Bino avatar
us flag
@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.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.