ThisCalculator

Everyday

Point Two Domains to One VPS: Nginx & Apache

Updated · Standard formula, shown below

This guide shows you how to host two different domains on a single VPS using both Nginx and Apache. You’ll configure Nginx as the front-end reverse proxy, routing each domain to a separate Apache virtual host, so both sites run simultaneously on the same server. You’ll need root access to your VPS and the domain names already pointing to its IP address.

What You’ll Need

  • A VPS with a public IP address (Ubuntu/Debian assumed).
  • Two domain names (e.g., example.com and example.org) with DNS A records pointing to your VPS IP.
  • SSH access with sudo privileges.
  • Nginx and Apache installed (Apache listening on a different port, e.g., 8080).

Step-by-Step

  1. Verify DNS resolution – Run dig +short example.com and dig +short example.org from your local machine. Both should return your VPS IP. If not, wait for DNS propagation (can take up to 48 hours).
  2. Install Nginx and Apache – If not already installed, run sudo apt update && sudo apt install nginx apache2. Ensure Apache is configured to listen on port 8080 (edit /etc/apache2/ports.conf and change Listen 80 to Listen 8080).
  3. Create Apache virtual hosts – For each domain, create a config file in /etc/apache2/sites-available/. Example for example.com (example.com.conf):
    <VirtualHost *:8080>
        ServerName example.com
        ServerAlias www.example.com
        DocumentRoot /var/www/example.com
        <Directory /var/www/example.com>
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>

    Create the document root directory and add a test index.html file. Repeat for the second domain.

  4. Enable the Apache sites – Run sudo a2ensite example.com.conf and sudo a2ensite example.org.conf. Then restart Apache with sudo systemctl restart apache2.
  5. Configure Nginx as a reverse proxy – Create a config file for each domain in /etc/nginx/sites-available/. Example for example.com (example.com):
    server {
        listen 80;
        server_name example.com www.example.com;
    
        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    Repeat for the second domain, using its own server_name.

  6. Enable the Nginx sites – Run sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ (and similarly for the second). Test the configuration with sudo nginx -t, then reload Nginx with sudo systemctl reload nginx.
  7. Test both domains – Open a browser and visit http://example.com and http://example.org. Each should show the respective test page. If not, check the troubleshooting section below.

Common Mistakes

  • Apache still on port 80 – If Apache listens on 80, it will conflict with Nginx. Change it to 8080 and restart.
  • Missing ServerAlias – Forgetting www can cause the non-www version to fail. Always include both.
  • DNS not propagated – Using a local hosts file to test before DNS updates can cause confusion. Wait for propagation or use curl -H "Host: example.com" http://your-vps-ip to test.
  • Firewall blocking port 8080 – If you have a firewall, ensure port 8080 is open only to localhost (or not at all, since Nginx proxies to it).

Tips

  • Use separate log files for each domain in both Apache and Nginx to simplify debugging.
  • Enable SSL with Let’s Encrypt for both domains after the basic setup works. Nginx can handle the certificates and proxy HTTPS to Apache.
  • Keep your Nginx configs in /etc/nginx/sites-available/ and symlink them; this makes disabling a site easy.
  • Use sudo apachectl configtest and sudo nginx -t before reloading to catch syntax errors.

Troubleshooting

  • Both domains show the same site – Check that each Apache virtual host has a unique ServerName and that Nginx proxies to the correct backend. Also ensure the Apache sites are enabled.
  • 502 Bad Gateway from Nginx – Apache may not be running or listening on 8080. Check with sudo systemctl status apache2 and sudo netstat -tulpn | grep 8080.
  • Connection refused – Nginx is likely not running or the config has errors. Run sudo nginx -t and check sudo systemctl status nginx.
  • One domain works, the other doesn’t – Verify the DNS for the failing domain points to your VPS. Also check the Nginx and Apache configs for typos in the domain names.
  • SSL certificate errors – If you added SSL, ensure the certificate covers both the www and non-www versions. Use certbot with the -d flag for both.

Frequently Asked Questions

Can I use Apache as the front-end and Nginx as the backend?

Yes, but the setup would be reversed. The principle is the same: one server proxies to the other. Nginx is often preferred as the front-end due to its performance with static files and concurrent connections.

Do I need a separate IP for each domain?

No. With virtual hosting, you can host multiple domains on a single IP address. This is the whole point of the setup described.

Can I point more than two domains this way?

Yes, you can add as many domains as your server resources allow. Just repeat the steps for each additional domain, creating a new Apache virtual host and a new Nginx server block.

What if I want to use HTTPS?

Use Let’s Encrypt with certbot. Install certbot and the Nginx plugin, then run sudo certbot --nginx -d example.com -d www.example.com. Certbot will automatically obtain and configure SSL certificates for your domains.

How do I test the setup without changing DNS?

Use the curl command with the Host header: curl -H "Host: example.com" http://your-vps-ip. This sends the request to your VPS but tells it which domain you’re trying to reach.

Is it better to use only Nginx or only Apache?

For simplicity, using only one web server is easier. However, some applications require Apache-specific features (like .htaccess). This dual setup lets you keep those features while benefiting from Nginx’s performance.

Related

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