Nginx 是当今最流行的 Web 服务器和反向代理软件之一,以其高性能、低资源消耗和灵活的配置能力著称。无论你是运行个人博客、小型应用还是企业级服务,掌握 Nginx 配置都是一项必备技能。本文将从安装开始,逐步深入到反向代理、SSL 配置、性能优化等实用场景。
一、Nginx 安装
1.1 Ubuntu/Debian 系列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| sudo apt update
sudo apt install nginx -y
nginx -v
sudo systemctl start nginx sudo systemctl enable nginx
sudo systemctl status nginx
|
1.2 CentOS/RHEL 系列
1 2 3 4 5 6 7 8 9
| sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl start nginx sudo systemctl enable nginx
|
安装完成后,访问 http://你的服务器IP 即可看到 Nginx 默认欢迎页面。
二、配置文件结构
Nginx 的配置文件通常位于 /etc/nginx/ 目录下。理解其结构是进行配置的基础。
1 2 3 4 5 6 7
| /etc/nginx/ ├── nginx.conf ├── sites-available/ ├── sites-enabled/ ├── conf.d/ ├── modules-enabled/ └── mime.types
|
核心概念: sites-available 存放所有站点配置,sites-enabled 通过符号链接引用需要启用的配置。这样可以在不删除配置的情况下禁用某个站点。
1 2 3 4 5 6 7 8
| sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
|
三、反向代理配置
反向代理是 Nginx 最强大的功能之一。它可以接收客户端的请求,然后转发给内部服务器(如 Node.js、Python Flask、Tomcat 等),并将响应返回给客户端。
3.1 基本反向代理
以下配置将所有 /api/ 路径的请求转发到本地的 Node.js 服务(端口 3000):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { listen 80; server_name example.com;
location /api/ { proxy_pass http://127.0.0.1:3000/; 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; }
location / { root /var/www/html; index index.html; } }
|
关键指令说明:
| 指令 |
作用 |
proxy_pass |
指定后端服务器地址,注意结尾的 / 会移除匹配到的 location 前缀 |
proxy_set_header Host $host |
传递原始请求的 Host 头 |
proxy_set_header X-Real-IP |
传递客户端真实 IP |
proxy_set_header X-Forwarded-For |
传递客户端 IP 链 |
proxy_set_header X-Forwarded-Proto |
传递请求协议(http/https) |
3.2 带路径重写的反向代理
有时你不需要保留原始路径前缀,可以利用正则匹配和路径重写:
1 2 3 4 5 6 7 8 9 10 11 12
| server { listen 80; server_name blog.example.com;
location ~ ^/blog/(.*)$ { proxy_pass http://127.0.0.1:4000/$1; 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; } }
|
这样访问 blog.example.com/blog/article/1 时,Nginx 会转发到 http://127.0.0.1:4000/article/1。
3.3 WebSocket 反向代理
对于需要 WebSocket 支持的应用(如实时聊天、WebSocket API),需要添加以下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| server { listen 80; server_name ws.example.com;
location /ws/ { proxy_pass http://127.0.0.1:8080/; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr;
proxy_read_timeout 86400s; } }
|
3.4 负载均衡
当后端有多个服务器实例时,可以使用 upstream 模块实现负载均衡:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| upstream backend { server 127.0.0.1:3000 weight=3; server 127.0.0.1:3001 weight=2; server 127.0.0.1:3002 weight=1;
}
server { listen 80; server_name app.example.com;
location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }
|
| 策略 | 说明 | 适用场景 |
| 轮询(默认) | 依次将请求分发到每个后端服务器 | 后端服务性能均匀的场景 |
| weight(权重) | 按权重比例分配请求 | 后端服务器配置不同时使用 |
| least_conn | 优先分配到当前活跃连接数最少的服务器 | 请求处理时间差异较大的场景 |
| ip_hash | 根据客户端 IP 哈希分配,同一 IP 始终访问同一台后端 | 需要会话粘滞(Session Stickiness)的场景 |
四、SSL/HTTPS 配置
Let’s Encrypt 提供免费的 SSL 证书,配合 Certbot 工具可以轻松为站点启用 HTTPS。
4.1 安装 Certbot
1 2 3 4 5
| sudo apt install certbot python3-certbot-nginx -y
sudo yum install certbot python3-certbot-nginx -y
|
4.2 自动获取并配置证书
1 2 3 4 5
| sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run
|
4.3 手动 SSL 配置
如果你想自己管理证书(例如使用 CDN 或其他证书颁发机构),可以手动配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| server { listen 443 ssl http2; server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
location / { proxy_pass http://127.0.0.1:4000; 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; } }
server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; }
|
4.4 SSL 安全评分优化
可以使用 SSL Labs 测试你的站点 SSL 安全等级。以下是获取 A+ 评级的关键配置:
1 2 3 4 5 6 7 8 9
| ssl_stapling on; ssl_stapling_verify on; resolver 8.8.8.8 1.1.1.1 valid=300s; resolver_timeout 5s;
ssl_dhparam /etc/nginx/dhparam.pem;
|
五、性能优化配置
5.1 静态文件缓存
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|webp)$ { expires 30d; add_header Cache-Control "public, no-transform"; access_log off;
try_files $uri =404; }
location ~* \.(woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, no-transform"; access_log off; }
|
5.2 Gzip 压缩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 256; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/atom+xml image/svg+xml;
|
5.3 连接超时优化
1 2 3 4 5 6 7 8 9 10 11 12 13
| client_body_timeout 12; client_header_timeout 12; keepalive_timeout 30; send_timeout 10;
proxy_connect_timeout 10; proxy_read_timeout 30; proxy_send_timeout 30;
client_max_body_size 50m;
|
5.4 工作进程优化
在 nginx.conf 的 events 和 http 块中:
1 2 3 4 5 6 7 8 9 10 11
| worker_processes auto; worker_rlimit_nofile 65535;
events { worker_connections 4096; use epoll; multi_accept on; }
|
六、日志配置与管理
6.1 自定义日志格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| http { log_format json escape=json '{' '"timestamp":"$time_iso8601",' '"remote_addr":"$remote_addr",' '"request":"$request",' '"status":$status,' '"body_bytes_sent":$body_bytes_sent,' '"request_time":$request_time,' '"http_referer":"$http_referer",' '"http_user_agent":"$http_user_agent",' '"upstream_addr":"$upstream_addr",' '"upstream_response_time":"$upstream_response_time"' '}';
access_log /var/log/nginx/access.log json; error_log /var/log/nginx/error.log warn; }
|
6.2 日志轮转(Logrotate)
Nginx 默认已经配置了 logrotate,位于 /etc/logrotate.d/nginx:
1 2 3 4 5 6 7 8 9 10 11 12 13
| /var/log/nginx/*.log { daily missingok rotate 14 compress delaycompress notifempty create 640 www-data adm sharedscripts postrotate [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript }
|
七、安全加固
7.1 隐藏 Nginx 版本号
1 2 3
| http { server_tokens off; }
|
7.2 限制请求速率
1 2 3 4 5 6 7 8 9
| limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
server { location /api/ { limit_req zone=mylimit burst=20 nodelay; proxy_pass http://backend; } }
|
7.3 IP 黑白名单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| location /admin/ { allow 192.168.1.0/24; allow 10.0.0.0/8; deny all; proxy_pass http://backend; }
location / { deny 123.45.67.89; deny 203.0.113.0/24; satisfy any; }
|
7.4 防止常见攻击
1 2 3 4 5 6 7 8 9 10 11 12 13
| if ($request_method !~ ^(GET|HEAD|POST)$) { return 405; }
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
|
八、常见问题排错
8.1 检查配置语法
1 2 3 4 5
| sudo nginx -t
sudo nginx -T
|
8.2 端口冲突排查
1 2 3 4 5
| sudo ss -tlnp | grep -E ':80|:443'
sudo lsof -i :80 -i :443
|
8.3 常见错误及解决方法
| 错误信息 | 可能原因 | 解决方法 |
| connect() failed (111: Connection refused) | 后端服务未启动或端口不对 | 检查后端服务是否运行,确认 proxy_pass 指向正确的地址和端口 |
| 502 Bad Gateway | Nginx 无法连接后端 | 检查后端服务状态和防火墙规则,确认 Nginx 能访问后端地址 |
| 413 Request Entity Too Large | 上传文件超过默认限制(1M) | 在 http/server/location 块中增大 client_max_body_size |
| 504 Gateway Timeout | 后端响应超时 | 增大 proxy_read_timeout,或优化后端处理速度 |
| open() "/etc/nginx/..." failed (13: Permission denied) | 目录或文件权限不正确 | 检查 Nginx 用户(www-data/nginx)对相关文件/目录的读写权限 |
九、完整站点配置示例
下面是一个集成了上述多项配置的完整示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| server { listen 443 ssl http2; server_name blog.example.com;
ssl_certificate /etc/letsencrypt/live/blog.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/blog.example.com/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_session_cache shared:SSL:10m;
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
access_log /var/log/nginx/blog.access.log; error_log /var/log/nginx/blog.error.log warn;
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|webp|woff2?)$ { root /var/www/blog/public; expires 30d; add_header Cache-Control "public, immutable"; access_log off; }
location / { proxy_pass http://127.0.0.1:4000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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; }
client_max_body_size 50m; }
server { listen 80; server_name blog.example.com; return 301 https://$server_name$request_uri; }
|
十、常用命令速查
| 命令 | 作用 |
nginx -t | 测试配置语法是否正确 |
nginx -s reload | 重新加载配置(不会中断请求) |
nginx -s restart | 重启 Nginx |
nginx -s stop | 快速停止 Nginx |
nginx -s quit | 优雅停止 Nginx(处理完当前请求) |
systemctl start/stop/restart/status nginx | 通过 systemd 管理 Nginx 服务 |
journalctl -u nginx -f | 实时查看 Nginx 日志 |
nginx -T | 打印完整的解析后配置 |
总结
Nginx 的配置虽然看似复杂,但核心逻辑是清晰的:server 块定义虚拟主机,location 块定义路径匹配规则,upstream 块定义后端服务器组。掌握本文介绍的反向代理、SSL 配置、性能优化和安全加固技巧,足以应对绝大多数日常运维场景。
建议从简单的站点配置开始,逐步添加需要的功能模块,每次修改配置后务必使用 nginx -t 进行检查。多阅读官方文档(nginx.org/docs)也是提升技能的最佳途径。
本文由AI辅助生成,内容仅供参考