How to Make Nginx Server Listen on Multiple Ports
Configuring Nginx to listen on multiple ports is essential for handling different types of requests efficiently. Whether you’re managing a website on a VPS hosting solution or running high-performance applications on a dedicated server, understanding how to configure Nginx correctly can improve flexibility and performance.
Why Configure Nginx to Listen on Multiple Ports?
- Serve different applications from the same server.
- Enable HTTPS and HTTP access simultaneously.
- Run multiple services using different ports.
- Improve security and routing by segmenting requests.
Steps to Configure Nginx to Listen on Multiple Ports
1. Open the Nginx Configuration File
Nginx’s configuration files are usually located in
/etc/nginx/nginx.confor within
/etc/nginx/sites-available/. You can open the file using:
sudo nano /etc/nginx/sites-available/default
2. Modify the Server Block to Include Multiple Ports
To make Nginx listen on multiple ports, update the
listendirective in the
serverblock as follows:
server { listen 80; listen 8080; listen 443 ssl; server_name example.com; location / { root /var/www/html; index index.html; } }
3. Test the Configuration
After modifying the configuration, check if there are any syntax errors:
sudo nginx -t
If the test is successful, you will see:
nginx: configuration file /etc/nginx/nginx.conf test is successful
4. Restart Nginx to Apply Changes
Restart the Nginx service to apply the new configuration:
sudo systemctl restart nginx
5. Verify the Ports Are Open
You can use
netstator
ssto check if Nginx is listening on the specified ports:
sudo netstat -tulnp | grep nginx
Or:
sudo ss -tulnp | grep nginx
Best Practices for Running Nginx on Multiple Ports
- Use HTTPS for secure communication (ensure valid SSL certificates).
- Restrict access to non-public ports using firewall rules.
- Optimize server performance by load balancing different services.
- Monitor logs to track performance and security threats.


