大网数据广告活动
服务器运维问答

Using Nginx as a load balancer for Apache servers with vhosts

· 52CE 我爱测

The attached image below represents the application stack that I'm building. The Apache nodes are exact replicas of each other, and they are each serving the same vhosts, let's say example1.com, example2.com, and example3.com. Fu…

问题原文

原帖链接 · Armin · CC BY-SA 3.0

The attached image below represents the application stack that I'm building. The Apache nodes are exact replicas of each other, and they are each serving the same vhosts, let's say example1.com, example2.com, and example3.com. Furthermore, for the vhosts served by the Apache nodes that have a database backend, the databases will be hosted on the Galera cluster nodes. All of these nodes are running Ubuntu 16.06.

I have been able to configure most of this setup successfully so far, except I can't seem to pass the hostname through the Nginx web loadbalancer. For example when I go to example1.com (and continuously refresh), I can see that the app is getting served alternating from each of the Apache nodes correctly.

But when I go to example2.com, still example1.com is displayed. This is not an issue with my local /etc/hosts file. I believe it's because I have not correctly configured the Nginx load balancer to pass the hostname so that it's interpreted by the Apache nodes.

Here are my 3 nginx server blocks that are linked in sites-enabled

#/etc/nginx/sites-enabled/example1.dev
upstream example1 {
    least_conn;
      server do.webserver1:80;
      server do.webserver2:80;
  }

server {
  listen 80;
  server_name example1.dev;
  location / {
    proxy_pass http://example1;
  }
}

#/etc/nginx/sites-enabled/example2.dev
upstream example2 {
    least_conn;
      server do.webserver1:80;
      server do.webserver2:80;
  }

server {
  listen 80;
  server_name example2.dev;
  location / {
    proxy_pass http://example2;
  }
}

#/etc/nginx/sites-enabled/example3.dev
upstream example3 {
    least_conn;
      server do.webserver1:80;
      server do.webserver2:80;
  }

server {
  listen 80;
  server_name example3.dev;
  location / {
    proxy_pass http://example3;
  }
}

And here are my 3 vhost files for the Apache nodes:

#/etc/apache2/sites-available/example1.dev.conf
<VirtualHost *:80>
        ServerName example1.dev
        ServerAlias www.example1.dev
        ServerAdmin [email omitted]
        DocumentRoot /var/www/example1.dev/public_html

        ErrorLog /var/www/example1.dev/error.log
        LogLevel warn
        CustomLog /var/www/example1.dev/access.log combined
</VirtualHost>
<Directory "/var/www/html/example1.dev/public_html">
        AllowOverride All

        Order allow,deny
        Allow from all
</Directory>

#/etc/apache2/sites-available/example2.dev.conf
<VirtualHost *:80>
        ServerName example2.dev
        ServerAlias www.example2.dev
        ServerAdmin [email omitted]
        DocumentRoot /var/www/example2.dev/public_html

        ErrorLog /var/www/example2.dev/error.log
        LogLevel warn
        CustomLog /var/www/example2.dev/access.log combined
</VirtualHost>
<Directory "/var/www/html/example2.dev/public_html">
        AllowOverride All

        Order allow,deny
        Allow from all
</Directory>

#/etc/apache2/sites-available/example3.dev.conf
<VirtualHost *:80>
        ServerName example1.dev
        ServerAlias www.example3.dev
        ServerAdmin [email omitted]
        DocumentRoot /var/www/example3.dev/public_html

        ErrorLog /var/www/example3.dev/error.log
        LogLevel warn
        CustomLog /var/www/example3.dev/access.log combined
</VirtualHost>
<Directory "/var/www/html/example3.dev/public_html">
        AllowOverride All

        Order allow,deny
        Allow from all
</Directory>

The Apache vhost configuration was working correctly before I added the Nginx loadbalancer. They are all linked in sites-enabled

My guess is this happens because Apache will load the first site-enabled if it doesn't recognize what to do with a request in this case. Is that right?

For some context, I'm normally a web/mobile app developer but I've recently been diving into dev ops, site reliability, and orchestration/automation. This is my first project I'm testing out.

Can anyone give me some insight on how I can do this? Thanks so much in advance!

已采纳解答

原帖链接 · Armin · CC BY-SA 3.0

I simply needed to add proxy_set_header Host $http_host; to the nginx server block config files like below:

upstream example2 {
    least_conn;
      server do.webserver1:80;
      server do.webserver2:80;
  }

server {
  listen 80;
  server_name example2.dev www.example2.dev;
  proxy_set_header Host $http_host;
  location / {
    proxy_pass http://example2;
  }
}

I did this to each server block and now it works!

Thank you @JoshuaGriffiths!

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-11-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。