Nginx Overview
Nginx is a high-performance HTTP server and reverse proxy known for low memory usage and strong concurrency support. It serves static content efficiently and handles dynamic content via CGI protocols like Perl and PHP, but requires Tomcat for Java applications. Optimized for high-load environments, Nginx supports up to 50,000 concurrent connections.
Key Advantages
- Cross-platform compatibility: Runs on Unix/Linux and Windows
- High-performance web and reverse proxy capabilities
- Exceptional concurrency handling
Rationale for Nginx
Single Tomcat instances typically handle 150-200 concurrent users. Scaling to nationla-level services requires distributing load across multiple servers. Nginx provides software-based load balancing at near-zero cost, widely adopted by major e-commerce platforms.
Proxy Concepts
Forward Proxy Acts as intermediary for clients accessing external resources. Benefits include:
- Accessing otherwise restricted resources
- Caching for acccelerated access
- Anonymizing client identity
Reverse Proxy Clients interact with proxy unaware of backend servers. Benefits include:
- Hiding server infrastructure
- Distributing client requests
- Providing unified access point
Load Balancing Fundamentals
Vertical scaling (hardware upgrades) becomes impractical at scale. Horizontal scaling via server clusters distributes requests across multiple nodes:
flowchart LR
Client --> ReverseProxy
ReverseProxy --> Server1
ReverseProxy --> Server2
ReverseProxy --> ServerN
Installation Methods
Windows Installation
Download executable from nginx.org and run nginx.exe
Linux Installation
Prerequisites
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
Procedure
mkdir /usr/local/nginx
cd /usr/local/nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
./configure
make
make install
Service Management
# Start
/usr/local/nginx/sbin/nginx
# Graceful shutdown
/usr/local/nginx/sbin/nginx -s quit
# Immediate stop
/usr/local/nginx/sbin/nginx -s stop
# Reload configuration
/usr/local/nginx/sbin/nginx -s reload
Docker Deployment
docker pull nginx
mkdir -p /home/nginx/{www,conf,log}
docker run -p 80:80 --name nginx --restart always \
-v /home/nginx/www:/usr/share/nginx/html \
-v /home/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /home/nginx/log:/var/log/nginx -d nginx
Configuration Guide
Virtual Host Setup
Modify /usr/local/nginx/conf/nginx.conf for:
- IP-based hosting
- Domain-based hosting
- Port configuration
Reverse Proxy Implementation
Route www.localhost.com:80 to 127.0.0.1:8080:
http {
upstream backend {
server localhost:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
}
}
}
Load Balancing Methods
Round Robin (Default)
upstream app_servers {
server localhost:8080;
server localhost:9999;
}
server {
location / {
proxy_pass http://app_servers;
}
}
Weighted Distribution
upstream app_servers {
server localhost:8080 weight=5;
server localhost:9999 weight=1;
}
IP Hash Session Persistence
upstream app_servers {
ip_hash;
server localhost:8080;
server localhost:9999;
}
Least Connections
upstream app_servers {
least_conn;
server localhost:8080;
server localhost:9999;
}