When setting up parallel Nginx instances for different users or applications, the default HTTP/HTTPS ports (80/443) become unavailable for subsequent instances. Here's a technical deep dive into proper configuration:
# Example of two separate Nginx config files # /etc/nginx/sites-available/domain1.conf server { listen 2345; server_name domain1.com; root /var/www/domain1; index index.html; } # /etc/nginx/sites-available/domain2.conf server { listen 2346; server_name domain2.com; root /var/www/domain2; index index.html; }
The core issue stems from how DNS and HTTP protocols interact. When accessing domains without ports:
- Browsers default to port 80 (HTTP) or 443 (HTTPS)
- DNS A/AAAA records don't contain port information
- The first Nginx instance binds to these privileged ports
Option 1: Reverse Proxy Configuration
# Main Nginx instance on port 80 server { listen 80; server_name domain1.com; location / { proxy_pass http://localhost:2345; proxy_set_header Host $host; } } server { listen 80; server_name domain2.com; location / { proxy_pass http://localhost:2346; proxy_set_header Host $host; } }
Option 2: Port Redirection
server { listen 80; server_name domain1.com; return 301 http://domain1.com:2345$request_uri; } server { listen 80; server_name domain2.com; return 301 http://domain2.com:2346$request_uri; }
For production environments, consider these best practices:
# Systemd service unit for secondary instance [Unit] Description=Nginx Instance - UserA After=network.target [Service] Type=forking PIDFile=/var/run/nginx-userA.pid ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx-userA/nginx.conf ExecStart=/usr/sbin/nginx -c /etc/nginx-userA/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
When running multiple instances, monitor resource allocation:
# Stress test a specific instance ab -n 1000 -c 100 http://domain1.com:2345/ # Compare with main instance ab -n 1000 -c 100 http://domain1.com/
When running multiple Nginx instances on different ports (e.g., 2345 and 2346), you might encounter a situation where accessing the domains with the specified ports (e.g., domain1.com:2345
) works fine, but accessing them without the port (e.g., domain1.com
) fails. This happens because Nginx, by default, listens on port 80 for HTTP and 443 for HTTPS. If you don't explicitly configure these ports, the domains won't respond.
To resolve this, you need to ensure each Nginx instance is properly configured to listen on its designated port. Below is an example configuration for two Nginx instances:
# First Nginx instance (port 2345)
server {
listen 2345;
server_name domain1.com;
location / {
root /var/www/domain1;
index index.html;
}
}
# Second Nginx instance (port 2346)
server {
listen 2346;
server_name domain2.com;
location / {
root /var/www/domain2;
index index.html;
}
}
If you want domain1.com
and domain2.com
to work without specifying the port, you need to set up a reverse proxy or redirect. Here’s how you can configure a default Nginx instance to redirect traffic to the appropriate ports:
# Default Nginx instance (port 80)
server {
listen 80;
server_name domain1.com;
location / {
proxy_pass http://localhost:2345;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
server {
listen 80;
server_name domain2.com;
location / {
proxy_pass http://localhost:2346;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
After making these changes, restart Nginx and test the configuration:
sudo nginx -t
sudo systemctl restart nginx
Now, accessing domain1.com
should redirect to domain1.com:2345
, and domain2.com
should redirect to domain2.com:2346
.
Another approach is to use DNS records to point subdomains to different ports. For example:
sub1.domain1.com IN A your_server_ip
sub2.domain2.com IN A your_server_ip
Then, configure Nginx to listen on these subdomains:
server {
listen 80;
server_name sub1.domain1.com;
location / {
proxy_pass http://localhost:2345;
}
}
server {
listen 80;
server_name sub2.domain2.com;
location / {
proxy_pass http://localhost:2346;
}
}
By properly configuring Nginx and using reverse proxies or DNS records, you can run multiple Nginx instances on different ports while ensuring domain access works seamlessly.