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?
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_nameNginx, when
Host
HTTP header doesn't match anyserver
s, goes into the default one, which is the first, if not specified otherwise. Here is the config that works: