Repository navigation
Expand file tree
/
Copy pathnginx.conf
More file actions
200 lines (169 loc) · 5.92 KB
/
Copy pathnginx.conf
File metadata and controls
200 lines (169 loc) · 5.92 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# Docker DNS 解析器
resolver 127.0.0.11 valid=10s ipv6=off;
resolver_timeout 5s;
# 增大请求头和Cookie限制
large_client_header_buffers 8 32k;
client_header_buffer_size 8k;
client_max_body_size 100m;
proxy_buffer_size 128k;
proxy_buffers 8 256k;
proxy_busy_buffers_size 256k;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# 负载均衡配置(使用静态配置,nginx启动时解析一次)
# 容器启动顺序确保后端服务先启动
upstream user_service {
server user-service:8124;
}
upstream app_service {
server app-service:8126;
}
upstream screenshot_service {
server screenshot-service:8127;
}
# 前端服务
upstream frontend_service {
server frontend:80;
}
# Gzip压缩配置
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_proxied any;
gzip_comp_level 6;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/xml+rss
application/json
image/svg+xml;
server {
listen 80;
server_name localhost;
# 前端静态文件 - 代理到frontend容器
location / {
proxy_pass http://frontend_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 部署的静态应用(一键部署生成的应用)
location /deploy/ {
alias /usr/share/nginx/html/deploy/;
try_files $uri $uri/ /deploy/$1/index.html;
autoindex off;
expires 1y;
add_header Cache-Control "public, immutable";
}
location /api/static/ {
proxy_pass http://app_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
# 静态资源缓存
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# API反向代理(主应用服务)
location /api/ {
proxy_pass http://app_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
# 增加超时时间
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
# 支持WebSocket
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# 用户服务API
location /api/user/ {
proxy_pass http://user_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 截图服务API
location /api/screenshot {
proxy_pass http://screenshot_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 部署的子应用访问(格式:/随机字符串/)
location ~ "^/(?!assets/|api/|static/|user-api/|screenshot-api/|deploy/)[A-Za-z0-9]{6,}/" {
root /usr/share/nginx/html/deploy;
index index.html;
try_files $uri $uri/ =404;
expires 1y;
add_header Cache-Control "public, immutable";
}
# 处理子应用的前端路由(SPA fallback)
location ~ "^/(?!assets/|api/|static/|user-api/|screenshot-api/|deploy/)[A-Za-z0-9]{6,}/.*" {
root /usr/share/nginx/html/deploy;
try_files $uri /$1/index.html;
}
# 处理不带末尾斜杠的子应用URL
location ~ "^/(?!assets/|api/|static/|user-api/|screenshot-api/|deploy/)[A-Za-z0-9]{6,}$" {
return 301 $scheme://$host:$server_port$uri/;
}
# 静态资源访问(兼容旧路径)
location /static/ {
proxy_pass http://app_service;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
# 健康检查端点
location /health {
access_log off;
return 200 "healthy\n";
add_header Content-Type text/plain;
}
# 禁止访问隐藏文件
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
}