-
Notifications
You must be signed in to change notification settings - Fork 1
/
nginx.conf
106 lines (87 loc) · 3.48 KB
/
nginx.conf
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
# DNS 解析器配置
resolver
223.5.5.5 # 阿里 DNS
223.6.6.6 # 阿里 DNS 备用
8.8.8.8 # Google DNS
8.8.4.4 # Google DNS 备用
114.114.114.114 # 114 DNS
ipv6=off # 禁用 IPv6
valid=300s # DNS 缓存时间
;
resolver_timeout 5s; # 解析超时时间
server {
listen 1199;
server_name localhost;
# 静态文件目录
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
# 通用API代理配置
location ~ ^/api/(https?)/(.*?)(/.*)?$ {
# $1 是协议 (http 或 https)
# $2 是主机部分 (host:port 或仅 host)
# $3 是路径部分 (包括第一个斜杠)
# 解析主机和路径
set $upstream_protocol $1;
set $upstream_host $2;
set $upstream_path $3;
# 如果没有路径,设置为/
if ($upstream_path = '') {
set $upstream_path /;
}
# 解决重定向问题
proxy_redirect off;
proxy_buffering off;
# 增加超时时间
proxy_connect_timeout 10s;
proxy_send_timeout 10s;
proxy_read_timeout 10s;
# 允许重定向
proxy_intercept_errors on;
# 处理 SSL
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1.2 TLSv1.3;
# 完整的请求头设置
proxy_set_header Host $upstream_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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Accept-Encoding '';
proxy_set_header Connection "keep-alive";
proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36";
# 保留原始请求头
proxy_pass_request_headers on;
# 错误处理
error_page 301 302 307 = @handle_redirect;
# 代理到实际地址
proxy_pass $upstream_protocol://$upstream_host$upstream_path$is_args$args;
}
# 添加重定向处理
location @handle_redirect {
set $saved_redirect_location '$upstream_http_location';
# 如果重定向地址是相对路径,添加主机名
if ($saved_redirect_location !~ '^\w+://') {
set $saved_redirect_location '$upstream_protocol://$upstream_host$saved_redirect_location';
}
proxy_pass $saved_redirect_location;
proxy_ssl_server_name on;
proxy_ssl_protocols TLSv1.2 TLSv1.3;
# 完整的请求头设置
proxy_set_header Host $upstream_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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header Accept-Encoding '';
proxy_set_header Connection "keep-alive";
proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36";
# 保留原始请求头
proxy_pass_request_headers on;
}
}
error_log /var/log/nginx/error.log debug;