Skip to content

Commit c70582e

Browse files
committed
refactor: overhaul nginx configuration for improved performance and structure
- Reorganized nginx.conf to enhance readability and maintainability. - Added Brotli compression support alongside Gzip for better asset delivery. - Configured upstream servers for SpringBoot API and WebSocket handling. - Updated static resource caching settings and improved error handling for API requests. - Included detailed comments in Chinese for better understanding of configuration options.
1 parent 32f5a86 commit c70582e

1 file changed

Lines changed: 221 additions & 42 deletions

File tree

nginx.conf

Lines changed: 221 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,222 @@
1-
server {
2-
listen 80;
3-
server_name _;
4-
5-
root /usr/share/nginx/html;
6-
index index.html;
7-
8-
# Gzip static assets in runtime.
9-
gzip on;
10-
gzip_min_length 1024;
11-
gzip_types
12-
text/plain
13-
text/css
14-
text/javascript
15-
application/javascript
16-
application/json
17-
application/xml
18-
image/svg+xml;
19-
20-
# Cache hashed static resources.
21-
location /static/ {
22-
expires 30d;
23-
add_header Cache-Control "public, max-age=2592000, immutable";
24-
try_files $uri =404;
25-
}
26-
27-
# Proxy API requests to backend service.
28-
location /api/ {
29-
proxy_pass ${BACKEND_API_URL}/;
30-
proxy_http_version 1.1;
31-
proxy_set_header Host $host;
32-
proxy_set_header X-Real-IP $remote_addr;
33-
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
34-
proxy_set_header X-Forwarded-Proto $scheme;
35-
proxy_set_header Upgrade $http_upgrade;
36-
proxy_set_header Connection "upgrade";
37-
}
38-
39-
# SPA fallback.
40-
location / {
41-
try_files $uri $uri/ /index.html;
42-
}
1+
# load_module /etc/nginx/modules/ngx_http_brotli_filter_module.so;
2+
# load_module /etc/nginx/modules/ngx_http_brotli_static_module.so;
3+
user www-data;
4+
worker_processes auto;
5+
pid /run/nginx.pid;
6+
7+
events {
8+
worker_connections 1024;
9+
}
10+
11+
http {
12+
13+
##
14+
# 基础配置
15+
##
16+
17+
include /etc/nginx/mime.types;
18+
default_type application/octet-stream;
19+
20+
sendfile on;
21+
tcp_nopush on;
22+
tcp_nodelay on;
23+
24+
keepalive_timeout 65;
25+
26+
server_tokens off;
27+
28+
##
29+
# 日志
30+
##
31+
32+
access_log /var/log/nginx/access.log;
33+
error_log /var/log/nginx/error.log warn;
34+
35+
##
36+
# GZIP 压缩
37+
##
38+
39+
gzip on;
40+
gzip_comp_level 6;
41+
gzip_min_length 1k;
42+
gzip_buffers 16 8k;
43+
gzip_http_version 1.1;
44+
gzip_vary on;
45+
gzip_proxied any;
46+
47+
gzip_types
48+
text/plain
49+
text/css
50+
text/xml
51+
text/javascript
52+
application/json
53+
application/javascript
54+
application/x-javascript
55+
application/xml
56+
application/xml+rss
57+
application/rss+xml
58+
image/svg+xml;
59+
60+
##
61+
# Brotli 压缩(需要 nginx 安装 brotli 模块)
62+
#
63+
# Debian 默认 nginx 通常没有启用 brotli
64+
# 需要安装:
65+
#
66+
# apt install nginx-module-brotli
67+
#
68+
# 或自行编译模块
69+
##
70+
71+
brotli on;
72+
brotli_comp_level 6;
73+
brotli_static on;
74+
75+
brotli_types
76+
text/plain
77+
text/css
78+
text/xml
79+
text/javascript
80+
application/javascript
81+
application/json
82+
application/xml
83+
application/rss+xml
84+
application/atom+xml
85+
image/svg+xml;
86+
87+
##
88+
# upstream
89+
##
90+
91+
upstream springboot_backend {
92+
server ip:port; # 后端 API 服务地址
93+
}
94+
95+
upstream websocket_backend {
96+
server ip:port; # 后端 WebSocket 服务地址
97+
}
98+
99+
##
100+
# HTTP
101+
##
102+
103+
server {
104+
105+
listen 80;
106+
server_name fusionadmin.cn integration.net.cn syndra.cn;
107+
108+
##
109+
# React 打包目录
110+
##
111+
112+
root /home/syndra/web;
113+
index index.html;
114+
115+
##
116+
# React Router 支持
117+
##
118+
119+
location / {
120+
try_files $uri $uri/ /index.html;
121+
}
122+
123+
##
124+
# SpringBoot API 代理
125+
##
126+
127+
location /api/ {
128+
129+
proxy_pass http://springboot_backend/;
130+
131+
proxy_http_version 1.1;
132+
133+
proxy_set_header Host $host;
134+
proxy_set_header X-Real-IP $remote_addr;
135+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
136+
proxy_set_header X-Forwarded-Proto $scheme;
137+
138+
proxy_connect_timeout 60s;
139+
proxy_send_timeout 60s;
140+
proxy_read_timeout 60s;
141+
}
142+
143+
##
144+
# WebSocket 代理
145+
##
146+
147+
location /api/ws/ {
148+
149+
proxy_pass http://websocket_backend/;
150+
151+
proxy_http_version 1.1;
152+
153+
proxy_set_header Upgrade $http_upgrade;
154+
proxy_set_header Connection "upgrade";
155+
156+
proxy_set_header Host $host;
157+
proxy_set_header X-Real-IP $remote_addr;
158+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
159+
160+
proxy_read_timeout 86400;
161+
}
162+
163+
##
164+
# 静态资源缓存
165+
##
166+
167+
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf)$ {
168+
169+
expires 30d;
170+
171+
add_header Cache-Control "public, no-transform";
172+
}
173+
}
174+
175+
##
176+
# HTTPS 配置(暂时注释)
177+
##
178+
179+
# server {
180+
#
181+
# listen 443 ssl http2;
182+
# server_name your-domain.com;
183+
#
184+
# root /home/syndra/web;
185+
# index index.html;
186+
#
187+
# ssl_certificate /home/syndra/ssl/fullchain.pem;
188+
# ssl_certificate_key /home/syndra/ssl/privkey.pem;
189+
#
190+
# ssl_protocols TLSv1.2 TLSv1.3;
191+
# ssl_ciphers HIGH:!aNULL:!MD5;
192+
#
193+
# location / {
194+
# try_files $uri $uri/ /index.html;
195+
# }
196+
#
197+
# location /api/ {
198+
#
199+
# proxy_pass http://springboot_backend/;
200+
#
201+
# proxy_http_version 1.1;
202+
#
203+
# proxy_set_header Host $host;
204+
# proxy_set_header X-Real-IP $remote_addr;
205+
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
206+
# proxy_set_header X-Forwarded-Proto $scheme;
207+
# }
208+
#
209+
# location /api/ws/ {
210+
#
211+
# proxy_pass http://websocket_backend/;
212+
#
213+
# proxy_http_version 1.1;
214+
#
215+
# proxy_set_header Upgrade $http_upgrade;
216+
# proxy_set_header Connection "upgrade";
217+
#
218+
# proxy_set_header Host $host;
219+
# }
220+
# }
221+
43222
}

0 commit comments

Comments
 (0)