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
- Check the syntax of your configuration files.
For Apache: runsudo apachectl configtestorsudo apache2ctl -t. For Nginx: runsudo nginx -t. This catches typos, missing brackets, and invalid directives before they break your server. - 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 (usesudo 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 theincludedirective in the main config points to the right directory. - Confirm the document root exists and has correct permissions.
Runls -ld /path/to/your/siteto verify the directory exists. The user that runs the web server (oftenwww-datafor Apache on Debian,nginxfor Nginx) must have read and execute permissions on the directory and all parent directories. Usesudo chmod 755 /path/to/siteandsudo chown -R www-data:www-data /path/to/site(adjust user/group for your OS). - Check the ServerName and ServerAlias directives.
In Apache, ensureServerName www.example.commatches the domain you’re testing. AddServerAlias example.comif you want both. In Nginx, the equivalent isserver_name www.example.com example.com;. If you’re testing locally, add an entry to your/etc/hostsfile mapping the domain to 127.0.0.1. - Restart the web server after any change.
Apache:sudo systemctl restart apache2(orhttpd). Nginx:sudo systemctl restart nginx. Always run the syntax test first to avoid taking your server down with a bad config. - Test with curl using the Host header.
Runcurl -H "Host: example.com" http://localhostto 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. - 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. Usesudo netstat -tulpn | grep :80to 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. - Verify DNS resolution.
If you’re accessing from an external machine, rundig example.comto 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-availabledoes nothing until you runa2ensiteand reload. - Using the wrong user for file ownership. On CentOS, Apache runs as
apache, notwww-data. Nginx often runs asnginx. Check withps aux | grep nginxorps aux | grep apache. - Missing trailing slashes in document root.
DocumentRoot /var/www/htmlis correct;/var/www/html/with a trailing slash can cause subtle path issues in some setups. - Not matching server_name exactly.
example.comandwww.example.comare 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.conffor Apache,example.com.conffor Nginx inconf.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 -Ito see response headers; it helps identify if you’re hitting a proxy or the wrong server. - Use
sudo apachectl -S(Apache) orsudo 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/hoststo add a line like127.0.0.1 example.comto 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_namematches the Host header exactly. In Apache, check that theNameVirtualHostdirective is present (or that you’re usingVirtualHost *:80). In Nginx, ensure thelistendirective 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
Indexesdirective if you expect directory listing. For Nginx, ensure theindexdirective 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
reloadwhich 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_nameor when the first defined vhost is used as a catch-all. Ensure each vhost has a uniqueserver_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
- Remote Desktop on Raspberry Pi: How to Enable It
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L