15

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;
        }
    }
}
Muhammad Umer
  • 249
  • 1
  • 2
  • 7
  • `$server_addr` is somehow 127.0.0.1 why?? – Muhammad Umer Mar 26 '15 at 18:09
  • i want host header to be www.asd.com when server `www.asd.com:80` is chosen – Muhammad Umer Mar 26 '15 at 18:20
  • 1
    or if there is a way to write `if(server1) than host = a; ` , `if(server2) than host = b; ` – Muhammad Umer Mar 26 '15 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. – Florin Asăvoaie Mar 26 '15 at 21:25
  • both client and middle server are me. – Muhammad Umer Mar 27 '15 at 00:33

2 Answers2

9

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

proxy_set_header Host $host;
Jeff Wong
  • 99
  • 1
  • 5
  • `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. – Muhammad Umer May 07 '15 at 03: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. – Jeff Wong May 08 '15 at 17:26
  • no virtual host is chosen by server at random – Muhammad Umer May 08 '15 at 17:27
  • 1
    Something like this? Create two levels of proxying, and set the host hardcoded to the endpoint on the second layer http://serverfault.com/a/622782/287634 – Jeff Wong May 08 '15 at 17:38
  • ill take a look, but i think ive already seen since i have upvoted some stuff there – Muhammad Umer May 08 '15 at 17:45
  • This link is a 404 at present. – Emmel Apr 18 '17 at 18:16
  • Thanks for the note -- I've edited the link to point to the nginx documentation at: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header – Jeff Wong May 03 '17 at 21:34
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.

GreenReaper
  • 351
  • 3
  • 8