Set Up Multiple Domain Aliases on Apache and Nginx
Setting up multiple domain aliases lets you serve the same website content from several domain names, such as example.com, example.net, and example.org, without duplicating your files. This guide walks you through configuring both Apache and Nginx on a typical Linux server, covering the essential steps from DNS to virtual host configuration.
What You’ll Need
- A Linux server with Apache or Nginx installed and running.
- Root or sudo access to edit configuration files.
- All domain names you want to use must have DNS A records pointing to your server’s IP address.
- A text editor (nano, vim, etc.) and basic familiarity with the command line.
Step-by-Step
For Apache
- Locate your virtual host configuration. On most Debian/Ubuntu systems, these are in
/etc/apache2/sites-available/. On RHEL/CentOS, they are in/etc/httpd/conf.d/. You will typically have a default configuration file for your main site. - Open the configuration file for editing. For example:
sudo nano /etc/apache2/sites-available/example.com.conf - Add the alias domains to the
ServerAliasdirective. Inside the<VirtualHost>block, you should already have aServerNamedirective. Add aServerAliasline listing all additional domains, separated by spaces. For example:<VirtualHost *:80> ServerName example.com ServerAlias example.net example.org www.example.net www.example.org DocumentRoot /var/www/html </VirtualHost> - Save the file and exit the editor.
- Test the configuration. Run
sudo apachectl configtestorsudo apache2ctl -t. You should seeSyntax OK. - Enable the site if it’s not already enabled. Use
sudo a2ensite example.com.confif you created a new file. If you edited an existing enabled site, skip this step. - Reload Apache. Run
sudo systemctl reload apache2(orsudo systemctl reload httpdon RHEL/CentOS).
For Nginx
- Locate your server block configuration. On most systems, these are in
/etc/nginx/sites-available/(Debian/Ubuntu) or/etc/nginx/conf.d/(RHEL/CentOS). - Open the configuration file for editing. For example:
sudo nano /etc/nginx/sites-available/example.com - Add the alias domains to the
server_namedirective. Inside theserverblock, modify theserver_nameline to include all domains, separated by spaces. For example:server { listen 80; server_name example.com example.net example.org www.example.net www.example.org; root /var/www/html; } - Save the file and exit the editor.
- Test the configuration. Run
sudo nginx -t. You should seesyntax is okandtest is successful. - Enable the site if needed. On Debian/Ubuntu, create a symlink:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/. If you edited an existing enabled site, skip this step. - Reload Nginx. Run
sudo systemctl reload nginx.
Common Mistakes
- Forgetting DNS records. If a domain isn’t pointed to your server, the alias won’t work. Verify with
dig example.netthat it resolves to your server IP. - Using commas or other separators. Both Apache and Nginx expect spaces between domain names, not commas or semicolons.
- Not including the
wwwversions. If you wantwww.example.netto work, you must list it explicitly. Wildcard like*.example.netis possible but catches all subdomains. - Editing the wrong configuration file. On Debian/Ubuntu, you must edit files in
sites-availableand then enable them, not directly editsites-enabled(though it works, it’s not persistent across updates). - Forgetting to reload the web server. Changes only take effect after a reload or restart.
Tips
- Use a canonical redirect. To avoid duplicate content issues, consider redirecting all alias domains to your primary domain. In Apache, you can add
Redirect permanent / https://example.com/in a separate virtual host for each alias. In Nginx, add a separateserverblock withreturn 301for each alias. - Test each domain individually. Use
curl -H "Host: example.net" http://localhostto verify the server responds correctly without waiting for DNS propagation. - Use a configuration management tool. If you manage many servers, consider using Ansible, Puppet, or Chef to automate alias setup.
- Keep a backup. Before editing configuration files, copy them:
sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/example.com.bak.
Troubleshooting
- Site not loading for an alias. Check DNS resolution first:
dig example.net. If it resolves, check that the web server is listening on port 80/443:sudo netstat -tulpn | grep :80. Then verify the configuration is loaded:sudo nginx -T | grep server_name(Nginx) orsudo apache2ctl -S(Apache). - Getting a default site instead of your content. This usually means the alias isn’t matched. Ensure the
ServerAliasorserver_nameis spelled correctly and that the virtual host is the first one that matches. In Apache, if you have multiple virtual hosts, the first one with a matchingServerNameorServerAliaswins. In Nginx, the first server block that matches theHostheader is used. - SSL certificate errors. If you’re using HTTPS, you need a certificate that covers all aliases. Use a wildcard certificate (
*.example.com) or a multi-domain (SAN) certificate. For Let’s Encrypt, use the-dflag for each domain:certbot --nginx -d example.com -d example.net. - Changes not taking effect. Run
sudo systemctl reload apache2orsudo systemctl reload nginxafter every change. If you still see old behavior, check for syntax errors withconfigtestornginx -t.
Frequently Asked Questions
Can I use wildcards in ServerAlias or server_name?
Yes. For example, ServerAlias *.example.net matches any subdomain of example.net. However, this also matches www.example.net and any other subdomain, which may not be intended.
Do I need separate virtual hosts for each alias?
No. You can list multiple aliases in a single virtual host (Apache) or server block (Nginx). This is the simplest approach. Separate blocks are only needed if you want different behavior (like redirects) for each alias.
Will this work with HTTPS?
Yes, but you need a certificate that covers all domains. A single certificate with multiple SANs (Subject Alternative Names) is required. Let’s Encrypt can generate such certificates.
How do I redirect all aliases to my primary domain?
In Apache, create a virtual host for each alias with Redirect permanent / https://example.com/. In Nginx, add a server block for each alias with return 301 https://example.com$request_uri;.
What if I want different content for each domain?
Then you should not use aliases. Instead, create separate virtual hosts or server blocks with different DocumentRoot or root directives.
Do I need to restart or reload the web server?
Reload is sufficient and does not drop active connections. Use sudo systemctl reload apache2 or sudo systemctl reload nginx. A restart is only needed if you change the main configuration file or listen ports.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.