-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnginx.conf
More file actions
159 lines (131 loc) · 4.91 KB
/
Copy pathnginx.conf
File metadata and controls
159 lines (131 loc) · 4.91 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
# events {
# worker_connections 1024;
# }
# http {
# 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;
# error_log /var/log/nginx/error.log warn;
# sendfile on;
# tcp_nopush on;
# tcp_nodelay on;
# keepalive_timeout 65;
# types_hash_max_size 2048;
# # 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/json
# application/javascript
# application/xml+rss
# application/atom+xml
# image/svg+xml;
# server {
# listen 80;
# server_name _; # 默认匹配所有域名(与参考项目一致)
# root /usr/share/nginx/html;
# index index.html;
# # 静态文件长缓存(与参考项目一致,添加了 woff2/ttf 等常见字体)
# location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot)$ {
# expires 1y;
# add_header Cache-Control "public, immutable";
# }
# # gRPC 明文代理(根路径,客户端用 http://domain 连接 gRPC)
# location / {
# grpc_pass grpc://backend:50051;
# grpc_set_header X-Real-IP $remote_addr;
# grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# grpc_set_header X-Forwarded-Proto $scheme;
# }
# # API 代理(Hermes 的 /api/v1 开头路径)
# location ^~ /api/v1/ {
# proxy_pass http://backend:8080/;
# 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;
# }
# # WebSocket 代理(Hermes 的 /api/v1/ws)
# location /api/v1/ws {
# proxy_pass http://backend:8080/api/v1/ws;
# 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;
# }
# # SPA 路由回退(放在最后,避免覆盖代理路径)
# location / {
# try_files $uri $uri/ /index.html;
# }
# }
# }
events {
worker_connections 1024;
}
http {
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;
error_log /var/log/nginx/error.log warn;
sendfile on;
keepalive_timeout 65;
# Gzip 开启
gzip on;
gzip_types text/plain text/css application/json application/javascript;
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
index index.html;
# 1. 静态资源 (前端打包文件)
location / {
try_files $uri $uri/ /index.html;
}
# 2. 静态资源缓存优化
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
# 3. WebSocket 代理
# 后端路径: /api/v1/ws
location /api/v1/ws {
proxy_pass http://backend:8080; # 注意:backend 是 docker-compose 中的服务名
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;
}
# 4. API 代理
# 后端 Group: /api/v1
location ^~ /api/v1/ {
# 关键点:这里千万不要加结尾的斜杠 /
proxy_pass http://backend:8080;
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_read_timeout 300s;
proxy_connect_timeout 75s;
}
}
}