Host Multiple Domains on Different Ports of One Server
This guide shows you how to run several websites on a single server, each bound to a different port. You’ll map each domain to a specific port (e.g., example.com:8080, another.org:8081) using a reverse proxy, so visitors can reach the correct site just by typing the domain. You’ll need a Linux server (Ubuntu/Debian assumed), root or sudo access, and at least two domains pointing to your server’s public IP.
What You’ll Need
- A Linux server (Ubuntu 20.04/22.04 or similar) with SSH access.
- Two or more domain names that you control, with DNS A records pointing to your server’s public IP.
- Web server software (we’ll use Nginx, but Apache works similarly).
- Basic comfort with the command line and editing text files.
Step-by-Step
- Install Nginx (if not already installed):
sudo apt update sudo apt install nginx - Create a directory for each site’s files (e.g., /var/www/site1 and /var/www/site2). Add a simple index.html to each so you can test later.
- Configure your first site to listen on a non-standard port (e.g., 8080). Create a new Nginx config file:
sudo nano /etc/nginx/sites-available/site1Add:
server { listen 8080; server_name site1.com www.site1.com; root /var/www/site1; index index.html; } - Create a second config for the other domain on a different port (e.g., 8081):
sudo nano /etc/nginx/sites-available/site2Add:
server { listen 8081; server_name site2.org www.site2.org; root /var/www/site2; index index.html; } - Enable both sites by creating symlinks in sites-enabled:
sudo ln -s /etc/nginx/sites-available/site1 /etc/nginx/sites-enabled/ sudo ln -s /etc/nginx/sites-available/site2 /etc/nginx/sites-enabled/ - Test the configuration and reload Nginx:
sudo nginx -t sudo systemctl reload nginx - Open the ports in your firewall (if using UFW):
sudo ufw allow 8080/tcp sudo ufw allow 8081/tcp - Test by visiting http://your-server-ip:8080 and http://your-server-ip:8081. You should see the respective index pages.
- Point your domains to the server by editing DNS records at your registrar. Create an A record for each domain (and www) pointing to your server’s public IP.
- Verify the domains work by visiting http://site1.com:8080 and http://site2.org:8081. If they load, you’re done.
Common Mistakes
- Forgetting to open the ports in the firewall or security group (if using a cloud provider).
- Using the same port for two sites – each server block must have a unique listen directive.
- Not setting server_name correctly – if you omit it, Nginx may route the wrong domain to the wrong site.
- Editing sites-available but not enabling – the symlink in sites-enabled is what Nginx actually reads.
- Testing before DNS propagates – DNS changes can take up to 48 hours.
Tips
- Use a reverse proxy for cleaner URLs – instead of making visitors type :8080, you can keep port 80 for a main site and proxy subdomains like api.example.com to internal ports.
- Set up SSL later – once the basic setup works, add Let’s Encrypt certificates for HTTPS; note that certificates work for domain+port combos, but you’ll need to configure SSL on each port.
- Use log files – each server block can have its own access and error log to help debug.
- Consider using Docker – if you plan to scale, containerizing each site and mapping ports is a clean alternative.
Troubleshooting
- Site not loading at all – Check that Nginx is running (
sudo systemctl status nginx), the ports are open (sudo netstat -tulpn | grep nginx), and the firewall allows them. - Wrong site appears – Make sure each server block has a unique
listenport and thatserver_namematches the domain exactly. Also check that you haven’t accidentally created two blocks with the same port. - Domain resolves but site doesn’t load – Test with the IP and port directly. If that works, the issue is DNS or the server_name config.
- Permission denied errors – Ensure the web server user (www-data) has read access to your site directories:
sudo chown -R www-data:www-data /var/www/site1. - After config changes, site still old – Always run
sudo nginx -tthensudo systemctl reload nginxafter editing configs.
Frequently Asked Questions
Can I use the same port for multiple domains?
No, each port can only be bound to one server block. If you want multiple domains on the same port, you need a reverse proxy (like Nginx) to route based on the domain name, but that requires all domains to share one port (usually 80 or 443).
Do I need to configure DNS differently for ports?
No, DNS only maps domain names to IP addresses. Ports are handled by the server software. Just point your A records to the server IP.
Can I use ports below 1024?
Yes, but only with root privileges. Nginx runs as root initially, so it can bind to ports like 80 or 443. For ports above 1024, any user can bind, but you’ll still need to open them in the firewall.
How do I make the port invisible in the URL?
Use a reverse proxy: keep your main site on port 80, and for other domains, create a server block that proxies to the internal port. For example, proxy_pass http://localhost:8081;. Then visitors only see the domain without a port.
Is there a limit to how many domains I can host?
Practically, you’re limited by your server’s resources (CPU, memory, bandwidth) and the number of available ports (65,535). But you can also host many domains on the same port using virtual hosts, which is more common for large-scale hosting.
Can I use Apache instead of Nginx?
Yes, Apache supports the same concept with Listen directives and VirtualHost blocks. The configuration syntax differs, but the principle is identical.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.