我试图将nginxconfiguration为同一台服务器上的两个网站的反向代理。 这就是我正在做的事情:
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; }
它开始,不抱怨任何事情。 然后,当我打开alpha.example.com或beta.example.com – 我总是在http://localhost:49212 。 而且,无论我在端口80上打开什么URL, http://localhost:49212呈现http://localhost:49212 。
这不是我所期望的。 我只想要http://alpha.example.com被redirect/代理到http://localhost:49212 ,没有别的。 看起来像nginx不注意Host HTTP头,只是redirect到第一个upstream 。
哪里不对?
好的,这是答案。 server_name为空的server_name必须声明为“默认”服务器。 更多细节在这里: http : //nginx.org/en/docs/http/ngx_http_core_module.html#server_name
当Host HTTP头不匹配任何server时,Nginx将进入默认的Nginx,如果没有另外指定,Nginx会首先进入。 这是工作的configuration:
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; }