keepcalculator

06 Numbers and Powers

Fix Virtual Host Problems in Apache and Nginx

Card No. 06·12301 · Updated · Standard formula, shown below

Virtual host (vhost) misconfigurations are among the most common causes of websites returning the wrong content, showing a default page, or failing to load altogether. This guide walks you through diagnosing and fixing the typical issues in both Apache and Nginx, from file permissions and syntax errors to DNS and port conflicts. By the end, you’ll have a systematic checklist to get your vhosts working correctly.

What You’ll Need

  • Root or sudo access to your server (to edit config files and restart services).
  • SSH access to the machine running Apache or Nginx.
  • A text editor (nano, vim, or similar) installed.
  • Knowledge of your server’s OS (Debian/Ubuntu vs. RHEL/CentOS paths differ).
  • The domain name(s) you’re configuring, and the path to their document roots.

Step-by-Step

  1. Check the syntax of your configuration files.

    For Apache: run sudo apachectl configtest or sudo apache2ctl -t. For Nginx: run sudo nginx -t. This catches typos, missing brackets, and invalid directives before they break your server.
  2. Verify that your vhost files are actually being included.

    Apache on Debian/Ubuntu loads sites from /etc/apache2/sites-enabled/; ensure your site has a symlink there (use sudo a2ensite yoursite.conf). On RHEL/CentOS, check /etc/httpd/conf.d/. Nginx includes files from /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/ depending on your setup. Confirm the include directive in the main config points to the right directory.
  3. Confirm the document root exists and has correct permissions.

    Run ls -ld /path/to/your/site to verify the directory exists. The user that runs the web server (often www-data for Apache on Debian, nginx for Nginx) must have read and execute permissions on the directory and all parent directories. Use sudo chmod 755 /path/to/site and sudo chown -R www-data:www-data /path/to/site (adjust user/group for your OS).
  4. Check the ServerName and ServerAlias directives.

    In Apache, ensure ServerName www.example.com matches the domain you’re testing. Add ServerAlias example.com if you want both. In Nginx, the equivalent is server_name www.example.com example.com;. If you’re testing locally, add an entry to your /etc/hosts file mapping the domain to 127.0.0.1.
  5. Restart the web server after any change.

    Apache: sudo systemctl restart apache2 (or httpd). Nginx: sudo systemctl restart nginx. Always run the syntax test first to avoid taking your server down with a bad config.
  6. Test with curl using the Host header.

    Run curl -H "Host: example.com" http://localhost to see what response you get. If you see the default vhost, your server is not matching the correct server_name. If you get a 403, it’s likely a permissions issue. If you get a 404, check the document root path.
  7. Check for port and IP conflicts.

    If two vhosts are set to listen on the same IP:port but have different server_names, Nginx will use the first one as default. Apache will use the first one unless you specify _default_:80. Use sudo netstat -tulpn | grep :80 to see what’s listening. If another service (like a proxy) is on port 80, you’ll need to change your vhost to a different port or fix the conflict.
  8. Verify DNS resolution.

    If you’re accessing from an external machine, run dig example.com to confirm the domain resolves to your server’s IP. If it points elsewhere, update your DNS records at your registrar.

Common Mistakes

  • Forgetting to enable the site in Apache. Creating a config in sites-available does nothing until you run a2ensite and reload.
  • Using the wrong user for file ownership. On CentOS, Apache runs as apache, not www-data. Nginx often runs as nginx. Check with ps aux | grep nginx or ps aux | grep apache.
  • Missing trailing slashes in document root. DocumentRoot /var/www/html is correct; /var/www/html/ with a trailing slash can cause subtle path issues in some setups.
  • Not matching server_name exactly. example.com and www.example.com are different. If you only define one, the other will fall to the default vhost.
  • Editing the wrong config file. On Debian/Ubuntu Apache, the default site is 000-default.conf. If you edit that instead of creating a new file, you’ll change the default, not your vhost.

Tips

  • Use a consistent naming convention: example.com.conf for Apache, example.com.conf for Nginx in conf.d.
  • Keep a backup of working configs: sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/example.com.bak.
  • Test with curl -I to see response headers; it helps identify if you’re hitting a proxy or the wrong server.
  • Use sudo apachectl -S (Apache) or sudo nginx -T (Nginx) to dump the entire effective configuration. This shows which vhost is being used for each domain.
  • For local development, use sudo nano /etc/hosts to add a line like 127.0.0.1 example.com to test without changing DNS.

Troubleshooting

If the steps above don’t resolve your issue, work through these checks:

  • Still getting the default page? Your vhost is not being selected. Verify the server_name matches the Host header exactly. In Apache, check that the NameVirtualHost directive is present (or that you’re using VirtualHost *:80). In Nginx, ensure the listen directive matches the port and IP you’re testing.
  • Getting a 403 Forbidden? This is almost always a permissions issue. Check that the web server user can traverse the entire path to the document root. Also, check for an Indexes directive if you expect directory listing. For Nginx, ensure the index directive is set (e.g., index index.html index.php;).
  • Getting a 404 Not Found? The document root path is wrong or the file doesn’t exist. Double-check the path in your vhost and that the file is actually there. Use ls -la /var/www/example.com.
  • Server won’t restart after config change? Run the syntax test again. The error message usually points to the exact line number. Common issues: missing semicolon in Nginx, missing closing </VirtualHost> in Apache, or using an undefined variable.
  • Changes not taking effect? Did you actually restart the service? Some systems use reload which is gentler, but for vhost changes a full restart is safer. Also, check if there’s a caching layer (like Varnish) in front that might be serving stale content.
  • Multiple vhosts, but all show the same site? This happens when all vhosts have the same server_name or when the first defined vhost is used as a catch-all. Ensure each vhost has a unique server_name. If you want a catch-all, define it explicitly with _ in Nginx or _default_ in Apache.

Frequently Asked Questions

Why does my vhost show the default Apache page instead of my site?

Your vhost is not being loaded or the ServerName doesn’t match the domain you’re using. Run apachectl -S to see which vhost is the default and ensure your domain is listed. Also check that the site is enabled with a2ensite.

How do I set up multiple vhosts on the same IP?

Use name-based virtual hosting. In Apache, ensure NameVirtualHost *:80 is set (it’s default in modern versions) and create separate <VirtualHost *:80> blocks with different ServerName directives. In Nginx, just create separate server blocks with different server_name directives.

What is the difference between sites-available and sites-enabled in Apache?

sites-available holds all your vhost configs, even disabled ones. sites-enabled contains symlinks to the configs you want active. Use a2ensite to enable and a2dissite to disable.

My Nginx server_name doesn’t work with underscores in the domain?

Underscores are not valid in domain names, but Nginx allows them in server_name for internal use. If your domain has an underscore (like my_site.com), it’s invalid DNS, but Nginx will still match it. However, browsers may not resolve it. Use hyphens instead.

Can I use a port other than 80 for my vhost?

Yes, but you must specify the port in the listen directive (Nginx) or <VirtualHost *:8080> (Apache). You’ll also need to access the site via http://example.com:8080.

Why does my vhost work with www but not without?

You likely only defined server_name www.example.com. Add example.com to the same directive or create a separate vhost that redirects to the www version.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →