切换主题
🔧 Nginx 反向代理配置
📖 配置说明
本文档提供了断点服务的 Nginx 反向代理配置示例,支持 HTTPS 和 WebSocket 连接。
⚙️ 完整配置示例
HTTPS 配置(推荐)
nginx
server {
listen 443 ssl http2;
server_name breakpoint.demo.wueasy.com;
charset utf-8;
# SSL 证书配置
ssl_certificate /opt/ssl/wueasy/fullchain.crt;
ssl_certificate_key /opt/ssl/wueasy/cert.key;
# SSL 协议配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!aNULL:!MD5:!RC4:!DHE;
ssl_ecdh_curve secp384r1;
ssl_prefer_server_ciphers on;
# SSL 会话缓存
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
# OCSP Stapling
ssl_stapling on;
ssl_stapling_verify on;
# 访问日志(生产环境建议启用)
# access_log /var/log/nginx/breakpoint_access.log main;
access_log /dev/null;
# 错误页面
error_page 404 /404.html;
# 主应用代理
location / {
proxy_pass http://127.0.0.1:9888;
# 请求体大小限制
client_max_body_size 200M;
# 代理头设置
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
# 超时设置
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# WebSocket 代理配置
location /ws {
proxy_pass http://127.0.0.1:9888;
# WebSocket 必需配置
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# 超时设置(WebSocket 需要较长时间)
proxy_connect_timeout 3600s;
proxy_send_timeout 3600s;
proxy_read_timeout 3600s;
# 客户端信息传递
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
# HTTP 自动跳转 HTTPS
server {
listen 80;
server_name breakpoint.demo.wueasy.com;
charset utf-8;
# 强制跳转到 HTTPS
return 301 https://$server_name$request_uri;
}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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
nginx
# 如果不需要 HTTPS,可以使用此简化配置
server {
listen 80;
server_name breakpoint.demo.wueasy.com;
charset utf-8;
# 访问日志
access_log /var/log/nginx/breakpoint_access.log main;
# 错误页面
error_page 404 /404.html;
# 主应用代理
location / {
proxy_pass http://127.0.0.1:9888;
client_max_body_size 200M;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
# WebSocket 代理
location /ws {
proxy_pass http://127.0.0.1:9888;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 3600s;
proxy_send_timeout 3600s;
}
}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
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
🔑 配置要点说明
1. 后端服务地址
nginx
proxy_pass http://127.0.0.1:9888;1
重要
- 将
127.0.0.1:9888替换为你的断点服务实际地址和端口 - 如果服务在其他服务器,使用内网IP(如
10.0.12.11:9888) - 确保 Nginx 服务器能够访问到后端服务
2. SSL 证书配置
nginx
ssl_certificate /opt/ssl/wueasy/fullchain.crt;
ssl_certificate_key /opt/ssl/wueasy/cert.key;1
2
2
证书路径
- 修改为你的实际证书路径
fullchain.crt:完整证书链文件cert.key:私钥文件- 确保证书文件权限正确(建议 600 或 644)
3. 域名配置
nginx
server_name breakpoint.demo.wueasy.com;1
域名设置
- 替换为你的实际域名
- 支持多个域名:
server_name domain1.com domain2.com; - 确保域名已正确解析到服务器IP
4. WebSocket 支持
nginx
location /ws {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}1
2
3
4
5
2
3
4
5
WebSocket 必需配置
proxy_http_version 1.1:必须使用 HTTP/1.1Upgrade和Connection头:WebSocket 握手必需- 超时时间建议设置较长(3600s)
5. 请求体大小限制
nginx
client_max_body_size 200M;1
根据需求调整
- 默认 200M,适合大文件上传
- 如果不需要上传大文件,可以降低到 10M 或 50M
- 过大的值可能导致内存占用过高
📝 部署步骤
1. 创建配置文件
bash
# 创建配置文件
sudo vim /etc/nginx/conf.d/breakpoint.conf
# 或者在 sites-available 目录
sudo vim /etc/nginx/sites-available/breakpoint.conf1
2
3
4
5
2
3
4
5
2. 修改配置参数
根据实际情况修改以下参数:
- ✏️
server_name:你的域名 - ✏️
proxy_pass:后端服务地址 - ✏️
ssl_certificate:SSL 证书路径(如使用 HTTPS) - ✏️
ssl_certificate_key:SSL 私钥路径(如使用 HTTPS)
3. 测试配置
bash
# 测试 Nginx 配置语法
sudo nginx -t
# 预期输出:
# nginx: configuration file /etc/nginx/nginx.conf test is successful1
2
3
4
5
2
3
4
5
4. 重载配置
bash
# 重载 Nginx 配置
sudo nginx -s reload
# 或者重启 Nginx
sudo systemctl restart nginx1
2
3
4
5
2
3
4
5
5. 验证部署
bash
# 检查 Nginx 状态
sudo systemctl status nginx
# 测试访问
curl -I https://your-domain.com1
2
3
4
5
2
3
4
5
🔍 常见问题
Q1: 502 Bad Gateway 错误?
原因: Nginx 无法连接到后端服务
解决方案:
- 检查后端服务是否正常运行:
netstat -tlnp | grep 9888 - 检查防火墙是否阻止连接
- 确认
proxy_pass地址正确 - 查看 Nginx 错误日志:
tail -f /var/log/nginx/error.log
Q2: WebSocket 连接失败?
原因: WebSocket 配置不正确或超时
解决方案:
- 确保包含
Upgrade和Connection头设置 - 增加超时时间:
proxy_read_timeout 3600s; - 检查后端服务是否支持 WebSocket
- 使用浏览器开发者工具查看 WebSocket 握手过程
Q3: SSL 证书错误?
原因: 证书配置不正确或证书过期
解决方案:
- 检查证书文件路径是否正确
- 验证证书有效期:
openssl x509 -in cert.crt -noout -dates - 确保证书链完整(使用 fullchain.crt)
- 检查证书文件权限:
chmod 644 cert.crt
Q4: 上传文件失败?
原因: 请求体大小限制
解决方案:
nginx
# 增加上传大小限制
client_max_body_size 500M;
# 同时可能需要调整超时时间
proxy_send_timeout 300s;
proxy_read_timeout 300s;1
2
3
4
5
6
2
3
4
5
6
🚀 性能优化建议
1. 启用 Gzip 压缩
nginx
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;1
2
3
4
2
3
4
2. 启用缓存
nginx
# 静态资源缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, immutable";
}1
2
3
4
5
2
3
4
5
3. 连接池优化
nginx
upstream breakpoint_backend {
server 127.0.0.1:9888 max_fails=3 fail_timeout=30s;
keepalive 32;
}
location / {
proxy_pass http://breakpoint_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
}1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10