Apache 2.4 – Fixing 502 Bad Gateway on Localhost

502apache-2.4virtualhost

I'm trying to run a Drupal 7 multi-site install on my Mac (10.12.6).

My directory setup:

~/Sites/index.html
~/Sites/drupal/docroot/sites/site1
~/Sites/drupal/docroot/sites/site2

My /etc/hosts:

127.0.0.1    site1.local
127.0.0.1    site2.local

If I go to http://localhost, I see my index.html file, no problem. If I visit http://site1.local or http://site2.local, I get 502 Bad Gateway.

in /usr/local/etc/httpd/httpd.conf:

  • I'm listening on Port 80
  • DocumentRoot and <Directory are set to /Users/me/Sites/
  • AllowOverride is set to All
  • ServerName is set to localhost, user & group are set to me, staff

And the following are uncommented:

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
LoadModule vhost_alias_module lib/httpd/modules/mod_vhost_alias.so
Include /usr/local/etc/httpd/extra/httpd-vhosts.conf

My vhosts file looks like:

<VirtualHost *:80>
    DocumentRoot /Users/me/Sites
    ServerName localhost
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /Users/me/Sites/drupal/docroot
    ServerName site1.local
    ErrorLog /var/log/apache2/site1cms-error.log
  Options ExecCGI FollowSymLinks
  SetEnv APPLICATION_ENVIRONMENT local
  SetEnv AH_SITE_ENVIRONMENT local
  SetEnv AH_SITE_GROUP local
  <Directory /Users/me/Sites/drupal/docroot>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
  </Directory>
</VirtualHost>
<VirtualHost *:80>
    DocumentRoot /Users/me/Sites/drupal/docroot
    ServerName site2.local
    ErrorLog /var/log/apache2/site2cms-error.log
  Options ExecCGI FollowSymLinks
  SetEnv APPLICATION_ENVIRONMENT local
  SetEnv AH_SITE_ENVIRONMENT local
  SetEnv AH_SITE_GROUP local
  <Directory /Users/me/Sites/drupal/docroot>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
  </Directory>
</VirtualHost>

If I curl site1.local from terminal the site loads fine, but loading from Chrome or Safari I get the 502.

I tried sudo lsof -t -i tcp:80 -s tcp:listen | sudo xargs kill in case some other apps were listening on port 80 but it didn't help.

I've looked in the Apache logs and the custom logs I created above, nothing is recorded.

Running MySql 5.7, PHP 5.6, Apache 2.4. Tried restarting all services.

Any ideas?

Best Answer

The problem was I was on a (work-mandated) Cisco VPN. Disconnected from said VPN and 502 error disappears.

An edge-case but FYI for anyone else who encounters this problem in the future.

Related Topic