Press ESC to close Press / to search

NGINX – High Performance Web Server and Reverse Proxy

What is NGINX? NGINX (pronounced “engine-x”) is a high-performance web server, reverse proxy, load balancer,...

Web Servers Linux Open Source

What is NGINX?

NGINX (pronounced “engine-x”) is a high-performance web server, reverse proxy, load balancer, and HTTP cache. Created by Igor Sysoev in 2004 to address the C10K problem (handling 10,000 concurrent connections), NGINX has grown to power over 30% of the worlds busiest websites.

Unlike traditional web servers that create a new thread for each connection, NGINX uses an event-driven, asynchronous architecture that handles thousands of simultaneous connections efficiently with minimal memory footprint.

Key Features

  • High performance: Handle thousands of concurrent connections with low memory
  • Reverse proxy: Forward requests to backend servers with load balancing
  • Load balancing: Distribute traffic across multiple servers
  • HTTP/2 and HTTP/3: Support for modern protocols including QUIC
  • SSL/TLS termination: Offload encryption from backend servers
  • Caching: Static and dynamic content caching
  • URL rewriting: Flexible request routing and rewriting
  • Hot reloading: Update configuration without downtime

NGINX vs Apache

Feature NGINX Apache
Architecture Event-driven Process/Thread-based
Static content Excellent Good
Dynamic content Via proxy (FastCGI) mod_php built-in
Configuration Centralized .htaccess support
Memory usage Lower Higher
Concurrent connections Excellent Good

System Requirements

  • Linux 2.6+ (recommended), FreeBSD, macOS, Windows
  • Minimum 512MB RAM for small sites
  • GCC compiler for building from source
  • PCRE library for regex support
  • OpenSSL for HTTPS support

Installation Guide

debian">Ubuntu/Debian

sudo apt update
sudo apt install nginx
sudo systemctl enable nginx
sudo systemctl start nginx

rhel-fedora">CentOS/RHEL/Fedora

sudo dnf install nginx
sudo systemctl enable nginx
sudo systemctl start nginx

From Official Repository (Latest Version)

# Ubuntu
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
sudo apt update
sudo apt install nginx

Basic Configuration

Static Website

# /etc/nginx/sites-available/mysite
server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/mysite;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # Cache static assets
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}

Reverse Proxy

# Proxy to backend application
server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}

Load Balancing

upstream backend {
    least_conn;  # Load balancing method
    server 192.168.1.10:8080 weight=3;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080 backup;
}

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://backend;
        proxy_next_upstream error timeout invalid_header http_500;
    }
}

SSL/TLS Configuration

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
    ssl_prefer_server_ciphers off;

    # HSTS
    add_header Strict-Transport-Security "max-age=63072000" always;

    root /var/www/example.com;
}

Useful Commands

# Test configuration syntax
sudo nginx -t

# Reload configuration without downtime
sudo nginx -s reload

# View access logs
sudo tail -f /var/log/nginx/access.log

# View error logs
sudo tail -f /var/log/nginx/error.log

# Check NGINX status
sudo systemctl status nginx

Performance Tuning

# /etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;

events {
    worker_connections 4096;
    use epoll;
    multi_accept on;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    
    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_min_length 1024;
    gzip_types text/plain text/css application/json application/javascript;
}

Download

NGINX is available in open-source and commercial (NGINX Plus) versions:

Download NGINX

Latest Version: 1.25.3
License: 2-clause BSD
Developer: NGINX, Inc. (F5 Networks)

Was this article helpful?