0

I'm trying to configure nginx as a reverse proxy for two websites on the same server. This is what I'm doing:

upstream alpha { server localhost:49212; } server { listen 80; server_name alpha.example.com; location / { proxy_pass http://alpha; } } upstream beta { server localhost:49213; } server { listen 80; server_name beta.example.com; location / { proxy_pass http://beta; } } server { listen 80; server_name ""; return 444; } 

It starts and doesn't complain about anything. Then, when I open alpha.example.com or beta.example.com - I always end up at http://localhost:49212. Moreover, no matter what URL I open on port 80, http://localhost:49212 is rendered.

This is not what I'd expect. I want only http://alpha.example.com to be redirected/proxied to http://localhost:49212 and nothing else. Looks like nginx doesn't pay attention to the Host HTTP header and just redirects everything to the first upstream.

What is wrong?

9
  • Have you restarted nginx? Check carefully for typos in server_name Commented Jul 29, 2015 at 11:24
  • Yes, restarted, checked for typos... Commented Jul 29, 2015 at 11:57
  • What is serving the sites? Commented Jul 29, 2015 at 16:52
  • A few Docker containers with Apache+PHP inside Commented Jul 29, 2015 at 17:07
  • I cross-posted this (similar) problem here: unix.stackexchange.com/questions/219094/… Commented Jul 29, 2015 at 17:13

1 Answer 1

1

OK, here is the answer. The server with an empty server_name has to be declared as a "default" one. More details here: http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name

Nginx, when Host HTTP header doesn't match any servers, goes into the default one, which is the first, if not specified otherwise. Here is the config that works:

upstream alpha { server localhost:49212; } server { listen 80; server_name alpha.example.com; location / { proxy_pass http://alpha; } } upstream beta { server localhost:49213; } server { listen 80; server_name beta.example.com; location / { proxy_pass http://beta; } } server { listen 80 default_server; # pay attention! server_name ""; return 444; } 

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.