简体   繁体   English

具有多个后端域的Nginx反向代理

[英]Nginx Reverse Proxy with Multiple Backend Domains

I have a 2 servers :- 我有2台服务器:-

Server 1 : NGINX Reverse Proxy. 服务器1:NGINX反向代理。

Server 2 : NGINX with 5-6 websites ( different domains ) 服务器2:具有5-6个网站(不同域)的NGINX

So basically, all users will come to Server 1 which will proxy_pass the traffic to Server 2 and get the response back. 因此,基本上所有用户都将进入服务器1,该服务器将proxy_pass流量传递到服务器2并获得响应。 Server 1 will also do Caching, WAF etc. 服务器1还将执行缓存,WAF等。

Here is my configuration for Server 1 :- 这是我对服务器1的配置:-

server {
  listen 80;
  server_name example.com www.example.com;

  location ~* {
     proxy_pass http://mysite:80;
}
}

server {
  listen 80;
  server_name server.com www.server.com;

  location ~* {
     proxy_pass http://mysite:80;
}
}

In my Server 2, in virtual.conf of NGINX, i have the following config : 在我的服务器2中,在NGINX的virtual.conf中,我有以下配置:

index index.php index.html;
server {
    listen   80;
    server_name  example.com www.example.com;

    location / {
        root   /var/www/websites/example/;
        include location-php;
    }
}

    server {
        listen   80;
        server_name  server.com www.server.com;

        location / {
            root   /var/www/websites/server/;
            include location-php;
        }
    }

However whenever i go to http://example.com or http://server.com ( directed via Sever 1 acting as Reverse Proxy ), it shows the Server 2's Default NGINX Page. 但是,无论何时我访问http://example.comhttp://server.com (通过充当反向代理的服务器1指示),它都会显示服务器2的默认NGINX页面。 I am not sure what am I doing wrong. 我不确定我在做什么错。 Also is this type of setup a proper way of doing things ? 这类设置是否也是正确的处理方式?

This is your host problem. 这是您的主机问题。
Due to your upstream name is mysite , so the host name in upstream request is mysqsite too. 由于您的上游名称是mysite ,因此上游请求中的主机名也是mysqsite
So the host isn't matched by the backend servers. 因此主机与后端服务器不匹配。

You can solve the problem like this by adding the directive before proxy_pass : 您可以通过在proxy_pass之前添加指令来解决此类问题:
proxy_set_header Host server.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM