1

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 webmaster@example1.dev
        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 webmaster@example2.dev
        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 webmaster@example3.dev
        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!

Graphical representation of application cluster

  • 2
    Have you tried setting the host header? See: nginx.org/en/docs/http/… – Joshua Griffiths Nov 10 '17 at 10:14
  • What domain name are you using to access your websites? Your question says example2.com but the server_name directive uses example2.dev - these names must be identical, otherwise example1.dev will be selected every time. And using upstream means that Apache sees a strange value for the HTTP Host header. – Richard Smith Nov 10 '17 at 10:36
  • @RichardSmith I am using a fake domain mask for the IP using my /etc/hosts file, since I don't have my own domain pointed to that IP as an A-record, yet. – Armin Nov 10 '17 at 17:13
  • Apache uses the first virtualhost if "Host" header in the request is not defined in servername in any virtualhost. – ezra-s Nov 11 '17 at 13:18
1

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!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.