17

I have a site that works as load balancer. Site A.

I have other sites that only can have one domain. So if i just redirect to them it says host name not found.

So if i manually set header to something then only that site shows up.

How can i set proxy_set_header Host xxxx to server address chosen. This way each rerouting request will have different and appropriate host header.

It won't be a problem if my other 2 sites could work based on url and not host header.

worker_processes 1; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; error_log logs/error.log debug; pid logs/nginx.pid; events { worker_connections 1024; } http { upstream myapp1 { #server localhost:3333; server www.asd.com:80; } server { listen 80; location / { proxy_set_header Host $upstream_addr; // should become somehow www.asd.com right now this code doesn't work proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://myapp1; } } } 
5
  • $server_addr is somehow 127.0.0.1 why?? Commented Mar 26, 2015 at 18:09
  • i want host header to be www.asd.com when server www.asd.com:80 is chosen Commented Mar 26, 2015 at 18:20
  • 1
    or if there is a way to write if(server1) than host = a; , if(server2) than host = b; Commented Mar 26, 2015 at 18:30
  • 1
    The thing is that the appropriate host header should be, in my opinion, whatever you get from the client. If the client requests www.asd.com, then that's what the host header should be. This will help to correctly generate absolute urls in the backend when needed, set the proper domains for cookies etc. Whatever you are trying to do, you are probably looking at it the wrong way. Commented Mar 26, 2015 at 21:25
  • both client and middle server are me. Commented Mar 27, 2015 at 0:33

2 Answers 2

10

You need to set the header to the incoming host variable, as documented here:

proxy_set_header Host $host; 
7
  • This variable is equal to line Host in *the header of request* or name meaning whichever the client/browser tells, that's the one that gets chosen. Commented May 7, 2015 at 3:18
  • This is what you'd want to do, correct? The client requests for a resource/virtual host from your proxy, and you need to serve up a virtual host. The virtual host to be served is chosen by the client/borwser. Commented May 8, 2015 at 17:26
  • no virtual host is chosen by server at random Commented May 8, 2015 at 17:27
  • 1
    Something like this? Create two levels of proxying, and set the host hardcoded to the endpoint on the second layer serverfault.com/a/622782/287634 Commented May 8, 2015 at 17:38
  • ill take a look, but i think ive already seen since i have upvoted some stuff there Commented May 8, 2015 at 17:45
0

Answers to this question explain this behaviour and offer workarounds.

Essentially, the header is fixed well before the upstream is selected. If you cannot make all upstreams respond to a single Host: header, you have to fix the upstream at the same time as you set the header.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.