Sling Academy
Home/DevOps/NGINX: How to listen on multiple ports

NGINX: How to listen on multiple ports

Last updated: January 20, 2024

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.

Next Article: NGINX Error: The plain HTTP request was sent to HTTPS port

Previous Article: NGINX Error 413: Request Entity Too Large – Causes and Solutions

Series: NGINX Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide