Skip to content

Commit 6cbf4e4

Browse files
committed
chore: enhance nginx configuration and update environment settings
- Updated .env.production to remove hardcoded WebSocket URL for production, allowing dynamic resolution. - Modified nginx.conf to enable Brotli compression, implement rate limiting for login and API requests, and improve static resource caching. - Adjusted upstream server configurations for better clarity and performance.
1 parent af05328 commit 6cbf4e4

2 files changed

Lines changed: 65 additions & 14 deletions

File tree

.env.production

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
VITE_DEV_SERVER_PORT=8000
2-
VITE_API_PROXY_TARGET=http://localhost:9527
3-
# Netty WebSocket 端口(与后端 application.yml websocket.netty.port 一致;/api/ws 会 rewrite 到 /ws)
4-
VITE_WS_PROXY_TARGET=http://localhost:8891
5-
# 通过前端开发服务器:/api/ws 由 vite 转发到 Netty,其余 /api 仍走 Spring
6-
VITE_WS_URL=ws://localhost:8891/api/ws/syndra
1+
# 生产环境:WebSocket 走当前页面同源(由 Nginx 反代到 Netty),不要硬编码 localhost:8891
2+
# 留空时,前端 resolveWebSocketUrl 会自动拼出 ws(s)://<host>/api/ws/syndra
3+
VITE_WS_URL=
74
VITE_ENABLE_MONITOR_CONSOLE=true

nginx.conf

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# load_module /etc/nginx/modules/ngx_http_brotli_filter_module.so;
2-
# load_module /etc/nginx/modules/ngx_http_brotli_static_module.so;
1+
load_module /etc/nginx/modules/ngx_http_brotli_filter_module.so;
2+
load_module /etc/nginx/modules/ngx_http_brotli_static_module.so;
33
user www-data;
44
worker_processes auto;
55
pid /run/nginx.pid;
@@ -9,7 +9,12 @@ events {
99
}
1010

1111
http {
12+
# 限流区域
13+
limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
14+
limit_req_zone $binary_remote_addr zone=login_limit:10m rate=1r/s;
1215

16+
limit_conn_zone $binary_remote_addr zone=conn_limit:10m;
17+
limit_req_status 429;
1318
##
1419
# 基础配置
1520
##
@@ -89,11 +94,11 @@ http {
8994
##
9095

9196
upstream springboot_backend {
92-
server ip:port; # 后端 API 服务地址
97+
server 127.0.0.1:9527;
9398
}
9499

95100
upstream websocket_backend {
96-
server ip:port; # 后端 WebSocket 服务地址
101+
server 127.0.0.1:8891;
97102
}
98103

99104
##
@@ -120,13 +125,52 @@ http {
120125
try_files $uri $uri/ /index.html;
121126
}
122127

128+
# ==================================================
129+
# 静态资源缓存
130+
# ==================================================
131+
132+
location ~* \.(js|css|png|jpg|jpeg|gif|svg|woff|woff2)$ {
133+
134+
expires 30d;
135+
136+
add_header Cache-Control "public, immutable";
137+
138+
try_files $uri =404;
139+
}
140+
141+
# ==================================================
142+
# 登录接口
143+
# 严格限流
144+
# ==================================================
145+
146+
location /api/auth/login {
147+
148+
proxy_pass http://springboot_backend;
149+
150+
# 登录限流
151+
limit_req zone=login_limit burst=5 nodelay;
152+
153+
# 并发限制
154+
limit_conn conn_limit 5;
155+
156+
proxy_http_version 1.1;
157+
158+
proxy_set_header Host $host;
159+
proxy_set_header X-Real-IP $remote_addr;
160+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
161+
}
162+
123163
##
124164
# SpringBoot API 代理
125165
##
126166

127167
location /api/ {
128168

129-
proxy_pass http://springboot_backend/;
169+
proxy_pass http://springboot_backend;
170+
171+
limit_req zone=api_limit burst=20 nodelay;
172+
173+
limit_conn conn_limit 20;
130174

131175
proxy_http_version 1.1;
132176

@@ -135,18 +179,24 @@ http {
135179
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
136180
proxy_set_header X-Forwarded-Proto $scheme;
137181

182+
proxy_set_header Upgrade $http_upgrade;
183+
proxy_set_header Connection "upgrade";
184+
138185
proxy_connect_timeout 60s;
139186
proxy_send_timeout 60s;
140187
proxy_read_timeout 60s;
141-
}
188+
}
142189

143190
##
144191
# WebSocket 代理
192+
# 后端 Netty 监听路径为 /ws/syndra(见 application.yml websocket.netty.path)。
193+
# proxy_pass 带末尾路径 /ws/,Nginx 会用它替换匹配 location 的前缀:
194+
# /api/ws/syndra -> http://websocket_backend/ws/syndra
145195
##
146196

147197
location /api/ws/ {
148198

149-
proxy_pass http://websocket_backend/;
199+
proxy_pass http://websocket_backend/ws/;
150200

151201
proxy_http_version 1.1;
152202

@@ -157,7 +207,9 @@ http {
157207
proxy_set_header X-Real-IP $remote_addr;
158208
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
159209

210+
# WebSocket 长连接需要禁用读超时(或设置较大值),否则空闲后会被 Nginx 主动断开
160211
proxy_read_timeout 86400;
212+
proxy_send_timeout 86400;
161213
}
162214

163215
##
@@ -208,14 +260,16 @@ http {
208260
#
209261
# location /api/ws/ {
210262
#
211-
# proxy_pass http://websocket_backend/;
263+
# proxy_pass http://websocket_backend/ws/;
212264
#
213265
# proxy_http_version 1.1;
214266
#
215267
# proxy_set_header Upgrade $http_upgrade;
216268
# proxy_set_header Connection "upgrade";
217269
#
218270
# proxy_set_header Host $host;
271+
# proxy_read_timeout 86400;
272+
# proxy_send_timeout 86400;
219273
# }
220274
# }
221275

0 commit comments

Comments
 (0)