![]()
常见 Nginx 配置的解析和使用说明,包括基本配置、反向代理、负载均衡、缓存等常用场景
1. 基础配置
配置文件路径:
- 主配置文件:/etc/nginx/nginx.conf
- 站点配置:通常在 /etc/nginx/conf.d/*.conf 或 /etc/nginx/sites-available/ 中。
常见结构:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| worker_processes auto; events { worker_connections 1024; }
http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;
server { listen 80; server_name example.com;
location / { root /usr/share/nginx/html; index index.html index.htm; }
error_page 404 /404.html; } }
|
2. 反向代理
Nginx 常用作反向代理,将请求转发到后端服务。
1 2 3 4 5 6 7 8 9 10 11
| server { listen 80; server_name example.com;
location / { proxy_pass http://127.0.0.1:8080; 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_pass:指定后端服务地址。
- proxy_set_header:自定义请求头转发。
- proxy_connect_timeout:连接超时时间。
- proxy_read_timeout:后端响应超时时间。
- 负载均衡
Nginx 支持多种负载均衡策略。
配置示例:
1 2 3 4 5 6 7 8 9 10 11 12 13
| upstream backend { server 127.0.0.1:8080; server 127.0.0.1:8081; }
server { listen 80; server_name example.com;
location / { proxy_pass http://backend; } }
|
负载均衡策略:
- 轮询(默认): 请求按顺序分配。
- 权重: 设置每个后端的请求分配权重。
1 2 3 4
| upstream backend { server 127.0.0.1:8080 weight=3; server 127.0.0.1:8081 weight=1; }
|
IP 哈希: 按客户端 IP 分配请求,保持会话一致性。
1 2 3 4 5
| upstream backend { ip_hash; server 127.0.0.1:8080; server 127.0.0.1:8081; }
|
4. 静态文件服务
为网站提供高效的静态文件服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| server { listen 80; server_name static.example.com;
location / { root /var/www/static; index index.html; }
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|ttf|svg|eot)$ { expires 30d; access_log off; } }
|
5. 缓存配置
Nginx 支持代理缓存和本地缓存。
代理缓存:
1 2 3 4 5 6 7 8 9 10 11
| proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m inactive=60m max_size=1g;
server { location / { proxy_cache my_cache; proxy_pass http://backend; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; } }
|
静态文件缓存:
1 2 3 4 5
| location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|ttf|svg|eot)$ { expires max; log_not_found off; access_log off; }
|
6. HTTPS 配置
为站点配置 SSL 加密。
配置示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 443 ssl; server_name example.com;
ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key;
location / { proxy_pass http://127.0.0.1:8080; } }
server { listen 80; server_name example.com;
return 301 https://$host$request_uri; }
|
7. 日志管理
Nginx 提供灵活的访问和错误日志配置。
1 2 3 4 5 6 7
| http { 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; error_log /var/log/nginx/error.log warn; }
|
总结
| 功能 |
核心指令 |
| 静态文件服务 |
root、index、expires |
| 反向代理 |
proxy_pass、proxy_set_header |
| 负载均衡 |
upstream、负载均衡策略(轮询、权重、IP 哈希等) |
| 缓存配置 |
proxy_cache_path、proxy_cache |
| HTTPS 支持 |
ssl_certificate、ssl_certificate_key、强制 HTTPS 跳转 |
| 日志管理 |
log_format、access_log、error_log |