Nginx Core Concepts and Practical Configuration Guide
Nginx is a lightweight, high-performance web server and reverse proxy server developed by Igor Sysoev from Russia. It excels in handling high concurrency, with reports indicating support for up to 50,000 simultaneous connections.
Nginx Features
Nginx is widely adopted in internet projects due to several key strengths:
- Compared to Apache, it uses less memory, offers higher stability, and handles more concurrent connections effectively.
- Its event-driven architecture supports millions of TCP connections.
- High modularity, excellent extensibility, and a free software license encourage a rich ecosystem of third-party modules.
- Nginx is cross-platform, running on Linux, Windows, and macOS.
Nginx Funcsional Applications
1. Forward Proxy
The key characteristic of a forward proxy is that the client clearly knows the target server's address. The server only sees requests from the proxy, not the actual client, thus masking the real client's identity. For example, accessing www.google.com from mainland China requires a proxy server.
2. Reverse Proxy
Clients are unaware of the proxy; they don't need any configuration to access services. Requests are sent to the reverse proxy server, which forwards them to backend servers and returns responses. The proxy and backend servers appear as a single server, exposing only the proxy's address and hiding real server IPs.
3. Load Balancing
When a single server can't handle traffic, add more servers and distribute requests to them using load balancing algorithms. This ensures traffic is evenly spread across the server cluster.
4. Static/Dynamic Separation
To speed up website parsing, serve static and dynamic content from different servers. This reduces the load on a single server and improves response times.
Nginx Usage
1. Download
Visit https://nginx.org/en/download.html to download the stable version, then upload the package to your Linux server.
2. Common Commands
nginx -s stop # Fast shutdown, may not save state
nginx -s quit # Graceful shutdown, saving state
nginx -s reload # Reload configuration after changes
nginx -s reopen # Reopen log files
nginx -c filename # Specify a custom configuration file
nginx -t # Test configuration file syntax without running
nginx -v # Display Nginx version
nginx -V # Display Nginx version, compiler details, and configuration parameters
3. Directory Structure
├── conf # All Nginx configuration files
│ ├── fastcgi.conf # FastCGI parameter configuration
│ ├── fastcgi.conf.default # Original backup of fastcgi.conf
│ ├── fastcgi_params # FastCGI parameter definitions
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types # Supported media types
│ ├── mime.types.default
│ ├── nginx.conf # Main Nginx configuration
│ ├── nginx.conf.default
│ ├── scgi_params # SCGI parameter file
│ ├── scgi_params.default
│ ├── uwsgi_params # uWSGI parameter file
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp # FastCGI temporary data directory
├── html # Default Nginx site directory
│ ├── 50x.html # Error page for 5xx status codes
│ └── index.html # Default homepage
├── logs # Log directory
│ ├── access.log # Access logs
│ ├── error.log # Error logs
│ └── nginx.pid # Process ID file
├── proxy_temp # Temporary directory for proxy operations
├── sbin # Nginx commands
│ └── nginx # Startup command
├── scgi_temp # SCGI temporary directory
└── uwsgi_temp # uWSGI temporary directory
4. Configuration Structure
main # Global configuration
├── events # Network connection settings
├── http # Most features (proxy, caching, logging, third-party modules)
│ ├── upstream # Backend server definitions for load balancing
│ ├── server # Virtual host configuration (multiple per http block)
│ ├── server
│ │ ├── location # URI matching and request handling
│ │ ├── location
│ │ └── ...
│ └── ...
└── ...
5. Configuration Example
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name 192.168.71.167;
location / {
root html;
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
This example configures Nginx to listen on port 80 for requests to 192.168.71.167 and proxy them to http://127.0.0.1:8080.