Skip to content
Tutorials
Esc
  • Type at least two characters.
Browse Tutorials

History: NGINX - A High Performance Web Server

Import from Docusaurus (tutorial/Web Servers/02 - NGINX.mdx) (first version)

Back to history
Added: # NGINX - A High Performance Web Server
Added:
Added: NGINX (pronounced as "engine-x") is an open-source web server that is also used as a **reverse proxy**, **load balancer**, **mail proxy**, and **HTTP cache**. It was originally developed by **Igor Sysoev** in 2004 to solve the **C10k problem**—handling 10,000 simultaneous client connections on a single server.
Added:
Added: NGINX is renowned for its high performance, stability, rich feature set, simple configuration, and low resource consumption.
Added:
Added: ---
Added:
Added: ## Why NGINX?
Added:
Added: - **Event-driven architecture**: Handles multiple connections with a single thread using non-blocking I/O.
Added: - **Efficient resource usage**: Uses less memory and CPU even under heavy load.
Added: - **High concurrency**: Easily handles thousands of simultaneous connections.
Added: - **Modular architecture**: Supports load balancing, reverse proxying, media streaming, and more.
Added:
Added: ---
Added:
Added: ## Core Use Cases of NGINX
Added:
Added: NGINX is extremely versatile. Here's a breakdown of its primary use cases:
Added:
Added: ### 1. Web Server
Added:
Added: NGINX can serve static content (HTML, CSS, JS, images, etc.) with blazing speed. It is optimized for performance, making it a preferred choice for high-traffic websites.
Added:
Added: **Example Configuration:**
Added:
Added: ```nginx
Added: server {
Added: listen 80;
Added: server_name example.com;
Added:
Added: root /var/www/html;
Added: index index.html;
Added: }
Added: ```
Added:
Added: ### 2. Reverse Proxy
Added:
Added: Forward client requests to backend servers (e.g., Node.js, Django) while hiding the backend details.
Added:
Added: ```nginx
Added: server {
Added: listen 80;
Added: server_name example.com;
Added:
Added: location / {
Added: proxy_pass http://localhost:3000;
Added: proxy_http_version 1.1;
Added: proxy_set_header Upgrade $http_upgrade;
Added: proxy_set_header Connection 'upgrade';
Added: proxy_set_header Host $host;
Added: proxy_cache_bypass $http_upgrade;
Added: }
Added: }
Added: ```
Added:
Added: ### 3. Load Balancer
Added:
Added: NGINX can distribute traffic across multiple servers using different algorithms.
Added:
Added: #### Round Robin Load Balancing
Added:
Added: ```nginx
Added: upstream backend {
Added: server backend1.example.com;
Added: server backend2.example.com;
Added: }
Added:
Added: server {
Added: listen 80;
Added: server_name example.com;
Added:
Added: location / {
Added: proxy_pass http://backend;
Added: }
Added: }
Added: ```
Added:
Added: #### Least Connections Load Balancing
Added:
Added: ```nginx
Added: upstream backend {
Added: least_conn;
Added: server backend1.example.com;
Added: server backend2.example.com;
Added: }
Added:
Added: server {
Added: listen 80;
Added: server_name example.com;
Added:
Added: location / {
Added: proxy_pass http://backend;
Added: }
Added: }
Added: ```
Added:
Added: ### 4. SSL/TLS Termination
Added:
Added: Terminate HTTPS at NGINX, then forward to internal HTTP services.
Added:
Added: ```nginx
Added: server {
Added: listen 443 ssl;
Added: server_name example.com;
Added:
Added: ssl_certificate /etc/nginx/ssl/example.com.crt;
Added: ssl_certificate_key /etc/nginx/ssl/example.com.key;
Added:
Added: location / {
Added: proxy_pass http://localhost:3000;
Added: proxy_set_header Host $host;
Added: proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Added: }
Added: }
Added: ```
Added:
Added: Redirect HTTP to HTTPs:
Added:
Added: ```nginx
Added: server {
Added: listen 80;
Added: server_name example.com;
Added: return 301 https://$host$request_uri;
Added: }
Added: ```
Added:
Added: ### 5. HTTP Caching
Added:
Added: Improve performance and reduce backend load using proxy caching.
Added:
Added: ```nginx
Added: proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
Added:
Added: server {
Added: listen 80;
Added: server_name example.com;
Added:
Added: location /api/ {
Added: proxy_pass http://api_backend;
Added: proxy_cache my_cache;
Added: proxy_cache_valid 200 302 10m;
Added: proxy_cache_valid 404 1m;
Added: add_header X-Cache-Status $upstream_cache_status;
Added: }
Added: }
Added: ```
Added:
Added: ### 6. Mail Proxy
Added:
Added: NGINX can act as a proxy for email protocols like SMTP, IMAP, and POP3.
Added:
Added: ```nginx
Added: mail {
Added: server {
Added: listen 587;
Added: protocol smtp;
Added: proxy on;
Added: }
Added:
Added: auth_http localhost:9000/cgi-bin/auth;
Added: smtp_capabilities "SIZE 10485760" "STARTTLS";
Added: }
Added: ```
Added:
Added: ### 6. Media streaming
Added:
Added: NGINX can stream video/audio using modules like RTMP.
Added:
Added: ```nginx
Added: rtmp {
Added: server {
Added: listen 1935;
Added: chunk_size 4096;
Added:
Added: application live {
Added: live on;
Added: record off;
Added: }
Added: }
Added: }
Added:
Added: http {
Added: server {
Added: listen 8080;
Added:
Added: location /stat {
Added: rtmp_stat all;
Added: rtmp_stat_stylesheet stat.xsl;
Added: }
Added:
Added: location /stat.xsl {
Added: root /usr/local/nginx/html;
Added: }
Added: }
Added: }
Added: ```