Nginx is a versatile web server and reverse proxy that can be configured to listen on multiple ports, making it suitable for serving different websites, applications, or services from the same server. Configuring Nginx to listen on multiple ports can be useful for serving different content on different ports, or for supporting services like HTTP (port 80) and HTTPS (port 443). This guide will show you how to configure Nginx to listen on multiple ports.
Prerequisites
- A server with Nginx installed and running.
- Root or sudo access to modify Nginx configuration files.
- A basic understanding of Nginx configuration files (nginx.conf and site-specific configuration files).
Basic Nginx Configuration Overview
Nginx’s configuration files are typically located at /etc/nginx/nginx.conf or in /etc/nginx/sites-available/ for specific site configurations, depending on the Linux distribution. The main directive for listening on a port is listen, which specifies the IP address and port that Nginx should listen to.
Step 1: Open Nginx Configuration File
To configure Nginx to listen on multiple ports, you need to edit the Nginx configuration file. Open the file using your preferred text editor:
Or, if you are editing a specific site configuration:
Step 2: Add Multiple listen Directives
To make Nginx listen on multiple ports, you can add multiple listen directives within a server block. Here’s an example of a server block listening on ports 80 and 8080:
listen 80;
listen 8080;server_name example.com;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
In this configuration:
- listen 80; instructs Nginx to listen for HTTP requests on port 80.
- listen 8080; instructs Nginx to listen for HTTP requests on port 8080.
This means that requests made to http://example.com:80 and http://example.com:8080 will both be handled by this server block.
Step 3: Configuring HTTPS with Multiple Ports
If you want to enable HTTPS on a different port (e.g., 8443), you can configure it like this:
listen 443 ssl;
listen 8443 ssl;server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt;
ssl_certificate_key /etc/nginx/ssl/example.com.key;
root /var/www/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
In this configuration:
- listen 443 ssl; makes Nginx listen for HTTPS requests on the default port 443.
- listen 8443 ssl; makes Nginx listen for HTTPS requests on port 8443.
- The SSL certificate and key paths (ssl_certificate and ssl_certificate_key) are used to enable HTTPS.
Step 4: Verifying Configuration
After making changes, it’s crucial to verify the Nginx configuration for syntax errors before applying the changes. Run:
If the output says syntax is ok, the configuration is correct.
Step 5: Restart Nginx
To apply the changes, restart Nginx:
Or, if your system uses service:
Step 6: Testing the Configuration
To verify that Nginx is correctly listening on multiple ports:
- Check Listening Ports: Use netstat or ss to verify that Nginx is listening on the specified ports:sudo netstat -tuln | grep nginx
Or:
sudo ss -tuln | grep nginxThis will display a list of ports that Nginx is listening to. You should see entries for ports 80, 8080, 443, and 8443.
- Access the Server: Open your web browser and try accessing the server using the different ports:
- http://example.com
- http://example.com:8080
- https://example.com
- https://example.com:8443
You should see the same content being served if the configuration was done correctly.
Advanced: Setting Different Content for Different Ports
You can configure different content to be served based on the port. For example, serving different root directories for each port:
listen 80;
server_name example.com;
root /var/www/html80;
index index.html;location / {
try_files $uri $uri/ =404;
}
}
server {
listen 8080;
server_name example.com;
root /var/www/html8080;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
In this example:
- Requests to http://example.com (port 80) will serve content from /var/www/html80.
- Requests to http://example.com:8080 will serve content from /var/www/html8080.
Conclusion
Configuring Nginx to listen on multiple ports allows you to serve different services, support custom setups, or provide flexibility in your network architecture. Whether you need Nginx to handle multiple HTTP and HTTPS ports or direct different ports to different content, Nginx’s listen directive makes it easy to set up. Just be sure to verify your configurations, restart Nginx, and test each port to ensure everything works as expected.