nginx 配置详解
NGINX(“Engine-X”)是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。以下是 NGINX 配置文件的详细说明,包括基本结构、主要指令和示例配置。
基本结构
NGINX 配置文件通常位于 /etc/nginx/nginx.conf。它的基本结构包括全局设置、HTTP 设置、服务器设置和位置设置:
# 全局设置 user www-data; worker_processes auto;事件模块
events { worker_connections 1024; }HTTP 设置
http { include mime.types; default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on; keepalive_timeout 65;
# 服务器设置 server { listen 80; server_name example.com;
# 位置设置 location / { root /usr/share/nginx/html; index index.html index.htm; }
# 反向代理示例 location /api/ { proxy_pass http://backend_server; 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; }
# 错误页面 error_page 404 /404.html; location = /40x.html { }
error_page 500 502 503 504 /50x.html; location = /50x.html { } } }
主要指令
全局设置
user: 指定运行 NGINX 的用户和用户组。worker_processes: 指定工作进程的数量,auto会根据 CPU 核心数自动设置。pid: 指定 PID 文件的位置。worker_rlimit_nofile: 设置每个 worker 进程可以打开的最大文件数。
事件模块
worker_connections: 指定每个 worker 进程的最大连接数。
HTTP 模块
include: 包含其他配置文件。log_format: 自定义日志格式。access_log: 指定访问日志的位置和格式。sendfile: 启用高效文件传输。keepalive_timeout: 设置 keepalive 超时时间。
服务器设置
listen: 指定监听的端口。server_name: 指定服务器名称(域名)。root: 指定根目录。index: 指定默认索引文件。location: 用于匹配请求的 URI。
反向代理
proxy_pass: 指定代理的后端服务器。proxy_set_header: 设置代理请求的头部信息。
错误页面
error_page: 自定义错误页面。
负载均衡
NGINX 可以使用多种方式进行负载均衡,包括轮询、最少连接和 IP 哈希。
轮询 (Round Robin)
这是默认的负载均衡方法,每个请求依次分配给每个服务器。
http { upstream backend { server backend1.example.com; server backend2.example.com; }server { listen 80; server_name example.com;
location / { proxy_pass http://backend; } } }
最少连接 (Least Connections)
请求分配给当前连接数最少的服务器。
http { upstream backend { least_conn; server backend1.example.com; server backend2.example.com; }server { listen 80; server_name example.com;
location / { proxy_pass http://backend; } } }
IP 哈希 (IP Hash)
请求分配给客户端 IP 地址的哈希值对应的服务器,保证同一 IP 的请求总是分配给同一服务器。
http { upstream backend { ip_hash; server backend1.example.com; server backend2.example.com; }server { listen 80; server_name example.com;
location / { proxy_pass http://backend; } } }
缓存
NGINX 支持多种缓存机制,包括代理缓存和快速缓存。
代理缓存
用于缓存后端服务器的响应。
http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;server { listen 80; server_name example.com;
location / { proxy_cache my_cache; proxy_pass http://backend; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; } } }
快速缓存 (FastCGI Cache)
用于缓存 FastCGI 服务器的响应。
http { fastcgi_cache_path /data/nginx/fastcgi_cache levels=1:2 keys_zone=my_fastcgi_cache:10m max_size=1g inactive=60m use_temp_path=off;server { listen 80; server_name example.com;
location / { fastcgi_cache my_fastcgi_cache; fastcgi_pass backend; fastcgi_cache_valid 200 302 10m; fastcgi_cache_valid 404 1m; include fastcgi_params; } } }
限流
NGINX 可以通过限制请求速率来防止滥用和保护服务器资源。
限制请求速率
限制每个客户端 IP 的请求速率。
http { limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;server { listen 80; server_name example.com;
location / { limit_req zone=mylimit burst=5 nodelay; } } }
限制连接数
限制每个客户端 IP 的并发连接数。
http { limit_conn_zone $binary_remote_addr zone=addr:10m;server { listen 80; server_name example.com;
location / { limit_conn addr 10; } } }
高级示例
综合配置示例
以下是一个综合配置示例,结合了负载均衡、缓存和限流功能:
http { # 定义后端服务器组,并使用最少连接策略进行负载均衡 upstream backend { least_conn; server backend1.example.com; server backend2.example.com; }# 配置代理缓存 proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;
# 定义请求速率限制区域 limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;
# 定义连接数限制区域 limit_conn_zone $binary_remote_addr zone=addr:10m;
# 服务器块配置 server { listen 80; server_name example.com;
location / { # 使用定义的后端服务器组进行反向代理 proxy_pass http://backend;
# 启用代理缓存,并设置缓存的有效期 proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
# 启用请求速率限制,每秒允许1个请求,突发5个请求,超过限制时立即返回错误 limit_req zone=mylimit burst=5 nodelay;
# 启用连接数限制,每个客户端 IP 最多允许10个并发连接 limit_conn addr 10; } } }
缓存
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=1g inactive=60m use_temp_path=off;: 定义了代理缓存的存储路径和缓存区域参数。/data/nginx/cache: 缓存存储路径。levels=1:2: 缓存文件目录结构。keys_zone=my_cache:10m: 定义一个名为my_cache的缓存区域,大小为 10MB。max_size=1g: 最大缓存大小为 1GB。inactive=60m: 60 分钟内未被访问的缓存将被删除。use_temp_path=off: 禁用临时路径,直接写入最终路径。
请求速率限制
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=1r/s;: 定义了一个请求速率限制区域。
$binary_remote_addr: 以客户端 IP 地址作为键。zone=mylimit:10m: 定义一个名为mylimit的限制区域,大小为 10MB。rate=1r/s: 每秒允许 1 个请求。
连接数限制
limit_conn_zone $binary_remote_addr zone=addr:10m;: 定义了一个连接数限制区域。
$binary_remote_addr: 以客户端 IP 地址作为键。zone=addr:10m: 定义一个名为addr的限制区域,大小为 10MB。
服务器块配置
server { listen 80; server_name example.com;
location / { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; limit_req zone=mylimit burst=5 nodelay; limit_conn addr 10; } }
listen 80;: 监听端口 80。server_name example.com;: 服务器名称为example.com。location / {}: 定义根路径的配置。proxy_pass http://backend;: 将请求转发给backend服务器组。proxy_cache my_cache;: 启用my_cache代理缓存。proxy_cache_valid 200 302 10m;: 缓存 200 和 302 响应 10 分钟。proxy_cache_valid 404 1m;: 缓存 404 响应 1 分钟。limit_req zone=mylimit burst=5 nodelay;: 启用mylimit请求速率限制,每秒允许 1 个请求,突发 5 个请求,超过限制时立即返回错误。limit_conn addr 10;: 启用addr连接数限制,每个客户端 IP 最多允许 10 个并发连接。