title: Using Nginx as a load balancer for Apache servers with vhosts
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>But when I go to example2.com, still example1.com is displayed. This is not an issue with my local <code>/etc/hosts</code> 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.</p>
  
  <p>Here are my 3 nginx server blocks that are linked in <code>sites-enabled</code></p>
  
  <pre><code>#/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;
    }
  }
  </code></pre>
  
  <p>And here are my 3 vhost files for the Apache nodes:</p>
  
  <pre><code>#/etc/apache2/sites-available/example1.dev.conf
  &lt;VirtualHost *:80&gt;
          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
  &lt;/VirtualHost&gt;
  &lt;Directory "/var/www/html/example1.dev/public_html"&gt;
          AllowOverride All
  
          Order allow,deny
          Allow from all
  &lt;/Directory&gt;
  
  #/etc/apache2/sites-available/example2.dev.conf
  &lt;VirtualHost *:80&gt;
          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
  &lt;/VirtualHost&gt;
  &lt;Directory "/var/www/html/example2.dev/public_html"&gt;
          AllowOverride All
  
          Order allow,deny
          Allow from all
  &lt;/Directory&gt;
  
  #/etc/apache2/sites-available/example3.dev.conf
  &lt;VirtualHost *:80&gt;
          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
  &lt;/VirtualHost&gt;
  &lt;Directory "/var/www/html/example3.dev/public_html"&gt;
          AllowOverride All
  
          Order allow,deny
          Allow from all
  &lt;/Directory&gt;
  </code></pre>
  
  <p>The Apache vhost configuration was working correctly before I added the Nginx loadbalancer. They are all linked in <code>sites-enabled</code></p>
  
  <p>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?</p>
  
  <p>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.</p>
  
  <p>Can anyone give me some insight on how I can do this? Thanks so much in advance!</p>
  
  <p><a href="https://i.stack.imgur.com/tcGh5.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tcGh5.jpg" alt="Graphical representation of application cluster"></a></p>