NGINX 使用「serverpool」作為同一 Apache 伺服器上不同域的負載平衡?

NGINX 使用「serverpool」作為同一 Apache 伺服器上不同域的負載平衡?

我有 nginx 作為 LB。 2 個 Apache 作為 Web 伺服器。可以說我有不同的域:

  • www.example.com
  • checkout.example.com

兩個網域將位於相同的 2 個 Apache 伺服器中。但ofcoz在不同的目錄下。並使用VHostApache vhost 檔案上的不同檔案。

就像下面的設計一樣:

          Nginx
            |
      -------------
      |           |
   Apache       Apache

以下是我目前現有的 Nginx .conf 文件,該文件不適用於第二個網域 (checkout.example.com)。

來自 NGINX (mysites.conf):

upstream serverpool {
  server 1.2.3.101:80 weight=1;
  server 1.2.3.102:80 weight=1;
}

server {
  listen 80;
  server_name www.example.com checkout.example.com;
  location / {
    proxy_pass http://serverpool;
  }
}

來自 2 個 Apache 伺服器相同的虛擬主機檔案(httpd.conf):

<VirtualHost *:80>
   ServerName www.example.com
   DocumentRoot /var/www/html/www.example.com/
</VirtualHost>
<VirtualHost *:80>
   ServerName checkout.example.com
   DocumentRoot /var/www/html/checkout.example.com/
</VirtualHost>

但每當我瀏覽那個(http://checkout.example.com), 這域名仍然出現在瀏覽器中..但包含 (www.example.com) 的內容,這是完全錯誤的。

請問我做錯了什麼?

答案1

您幾乎應該始終設定Host標題。否則 nginx 會回退到預設值proxy_set_header Host $proxy_host;,在您的情況下serverpool這對 apache 來說是無用的。

http://nginx.org/r/proxy_set_headerhttp://nginx.org/r/proxy_pass了解詳情。

upstream serverpool {
  server 1.2.3.101:80 weight=1;
  server 1.2.3.102:80 weight=1;
}

server {
  listen 80;
  server_name www.example.com checkout.example.com;
  location / {
    proxy_pass http://serverpool;
    proxy_set_header Host $host;
  }
}

答案2

您還需要將 HOST: header 傳送到您的上游伺服器 IP

這篇文章充分回答了問題

讓nginx在反向代理時傳遞上游的主機名

你的 nginx 設定也應該是這樣的

    upstream serverpool {
  server 1.2.3.101:80 weight=1;
  server 1.2.3.102:80 weight=1;
}

server {
  listen 80;
  server_name www.example.com checkout.example.com;
  location / {
    proxy_pass http://serverpool;
    proxy_set_header Host $host;
  }
}

相關內容