Nginx 配置与反向代理完全指南

Nginx 配置与反向代理完全指南

Someone Lv5

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

# 安装 Nginx
sudo apt install nginx -y

# 查看 Nginx 版本
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
# 安装 EPEL 源(如未安装)
sudo yum install epel-release -y

# 安装 Nginx
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/ # 已启用的站点配置(软链接到 sites-available)
├── conf.d/ # 额外的配置文件
├── modules-enabled/ # 启用的模块
└── mime.types # MIME 类型映射

核心概念: 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;

# WebSocket 超时设置(默认 60s,可调大)
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 {
# 负载均衡策略:默认轮询(round-robin)
server 127.0.0.1:3000 weight=3;
server 127.0.0.1:3001 weight=2;
server 127.0.0.1:3002 weight=1;

# # 其他策略(取消注释使用)
# least_conn; # 最少连接数
# ip_hash; # IP 哈希(会话保持)
}

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
# Ubuntu/Debian
sudo apt install certbot python3-certbot-nginx -y

# CentOS/RHEL
sudo yum install certbot python3-certbot-nginx -y

4.2 自动获取并配置证书

1
2
3
4
5
# 自动获取证书并修改 Nginx 配置
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 证书路径
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;

# HSTS(强制浏览器使用 HTTPS 访问)
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;
}
}

# HTTP 自动跳转到 HTTPS
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
# 开启 OCSP Stapling(在线证书状态协议装订)
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 1.1.1.1 valid=300s;
resolver_timeout 5s;

# 使用更强的 DH 参数
# 生成:openssl dhparam -out /etc/nginx/dhparam.pem 2048
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;

# 如果文件不存在,返回 404 而不是代理到后端
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.confeventshttp 块中:

1
2
3
4
5
6
7
8
9
10
11
# 设置为 auto 等于 CPU 核心数
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 {
# 定义 JSON 格式的访问日志
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
# 定义限流区域:每个 IP 每秒最多 10 个请求
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; # 内网 IP 段
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
# 限制请求方法,只允许 GET、HEAD、POST
if ($request_method !~ ^(GET|HEAD|POST)$) {
return 405;
}

# 防止点击劫持
add_header X-Frame-Options "SAMEORIGIN" always;

# 防止 MIME 类型嗅探
add_header X-Content-Type-Options "nosniff" always;

# 启用 XSS 过滤器
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
# 查看 80/443 端口占用
sudo ss -tlnp | grep -E ':80|:443'

# 或使用 lsof
sudo lsof -i :80 -i :443

8.3 常见错误及解决方法

错误信息可能原因解决方法
connect() failed (111: Connection refused)后端服务未启动或端口不对检查后端服务是否运行,确认 proxy_pass 指向正确的地址和端口
502 Bad GatewayNginx 无法连接后端检查后端服务状态和防火墙规则,确认 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
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;
}

# 反向代理到 Hexo 或 Node.js 博客
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;
}

# HTTP 重定向到 HTTPS
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辅助生成,内容仅供参考

  • 标题: Nginx 配置与反向代理完全指南
  • 作者: Someone
  • 创建于 : 2026-06-07 06:43:00
  • 更新于 : 2026-06-18 08:39:57
  • 链接: https://demo-blog.qusite.cn/2026-06-07-nginx-config-guide/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。