NGINX: How to listen on multiple ports

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

NGINX is a powerful, high-performance web server and reverse proxy known for its high concurrency, high performance, and low memory usage. An essential feature of NGINX is its ability to listen on multiple ports, which can be useful in various scenarios including load balancing, traffic management, and service segregation on the same server. In this tutorial, we’ll explore how to configure an NGINX server to listen on multiple ports.

Prerequisites

  • A server with NGINX installed
  • Root or sudo user access
  • A text editor of your choice

Basic Configuration of Multiple Ports

To begin, we’ll examine a simple scenario where NGINX listens on two HTTP ports for the same website or application.

server {
    listen 80;
    listen 8080;
    server_name example.com;
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
}

This configuration snippet indicates that NGINX is set to listen for HTTP traffic on both port 80 and port 8080 for the domain ‘example.com’.

Listening on Multiple Ports with SSL

Beyond simple HTTP traffic, it’s also common for NGINX to handle HTTPS connections. The following configuration block demonstrates NGINX listening on both port 443 (the standard port for HTTPS) and an alternative port with SSL enabled.

server {
    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;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    # ... additional SSL parameters
    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
}

This configuration will accept SSL connections for ‘example.com’ on both the standard HTTPS port and an additional higher port.

Advanced Configuration Scenarios

One advanced scenario is having separate NGINX server blocks for different ports, which can be used to serve different applications or content.

# Application on port 80
server {
    listen 80;
    server_name app1.example.com;
    # Configuration specific for application 1
}

# Different application on port 8080
server {
    listen 8080;
    server_name app2.example.com;
    # Configuration specific for application 2
}

This configuration differentiates the applications based on the domain and the port they’re accessed with.

Redirecting Traffic Between Different Ports

Sometimes you will want NGINX to listen on a port and redirect traffic to another port. This kind of redirection can be useful for temporary port changes or enforcing the use of SSL.

server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name:8443$request_uri;
}

server {
    listen 8443 ssl;
    server_name example.com;
    #... SSL Configuration
    location / {
        # Actual content here
    }
}

The first server block listens on port 80 and issues a 301 redirect to port 8443, where the second server block is configured to handle the SSL connections.

Handling IPv6 Traffic on Multiple Ports

Aside from IPv4, NGINX can also listen for IPv6 traffic. The format for specifying an IPv6 address in a listen directive involves wrapping the address in square brackets.

server {
    listen [::]:80;
    listen [::]:8080;
    server_name example.com;
    #... other configuration
}

This configures NGINX to listen on both port 80 and 8080 over IPv6 for ‘example.com’.

Conclusion

In conclusion, NGINX’s versatility allows it to easily handle multiple ports within its configurations which facilitates greater control over your server’s traffic and security. By applying the concepts and examples discussed in this tutorial, you should now feel confident about expanding NGINX’s web server capabilities to meet the needs of your projects.