1

I've been trying to do this thing for while but nothing working out for me. Hoping if anyone here can help....

The php app here reads $_SERVER['HTTP_HOST'] and then judging host part of the header, it decides which site to serves. The issue here, Nginx receives the headers as $http_host = mainweb.prd.mydomain.net and $x_custom_header = bob (or alice, joe etc.). I need to [re]set the value for http_host to bob.prd.mydomain.net. I suppose I need to do something like this:

set $host_header "${x_custom_header}.prd.mydomain.net";
proxy_set_header Host $host_header;

But this is obviously not working. Any idea how can I do that. All of the example that I can see here is actually for URL redirection, which is actually changing the URL in the browser, which I absolutely don't want. Any idea how can I do that? Any pointer to the right direction will be very much appreciate.

-San

MacUsers
  • 2,091
  • 3
  • 35
  • 56
  • Where does `x_custom_header ` come from? Client? – Sky Feb 08 '19 at 19:25
  • it technically comes from ha-proxy. User(s) asks for _bob.mywebsite.com_ that received by ha-proxy, which rewrites the _host_ header as ***mainweb.prd.mydomain.net***, sets up the _x-custom-header_ as ***bob*** and send both to Nginx. – MacUsers Feb 08 '19 at 19:37
  • 1
    `proxy_set_header` works with `proxy_pass`. How are you passing requests to PHP? If you are using `fastcgi_pass` you need to look at `fastcgi_param` instead. – Richard Smith Feb 08 '19 at 19:59
  • thanks @RichardSmith! It's `fastcgi` indeed. Do you have any example code somewhere that I can have look for reference? – MacUsers Feb 08 '19 at 21:04
  • 3
    See [this answer](https://stackoverflow.com/a/28890510/4862445). It's accepted, so presumably it worked. – Richard Smith Feb 08 '19 at 21:07
  • 1
    dear lord!! didn't think it's that's simple. I've been on that page for couple of times but missed the point that's what I need. Many thanks @RichardSmith! – MacUsers Feb 08 '19 at 21:59
  • just a follow up question, instead of setting the _domain_ part of the header staticky (i.e. `set $my_header "${x_custom_header}.prd.mydomain.net";`, is there a way to extract that from the original http_host header, which is always `mainweb.prd.mydomain.net`? – MacUsers Feb 08 '19 at 22:03
  • Possible duplicate of [Nginx: Override host header when using fastcgi\_pass](https://stackoverflow.com/questions/28889673/nginx-override-host-header-when-using-fastcgi-pass) – Zenexer Feb 11 '19 at 10:00
  • @MacUsers Not via simple built-in methods, but you could do it with something like Lua if you're using OpenResty. That probably deserves its own question, though. Glad the solution to the issue at hand was so simple! I've marked this as a duplicate to link anyone who finds this page to the correct answer. – Zenexer Feb 11 '19 at 10:02

0 Answers0