-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx.conf
124 lines (110 loc) · 4.54 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
worker_processes 1;
#error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name localhost;
# 定义Lua脚本
location / {
access_by_lua_block {
-- 获取请求头中的IP地址
local headers = ngx.req.get_headers()
-- 定义变量
local client_ip = ngx.var.remote_addr or headers["X-Real-IP"] or headers["X-Forwarded-For"]
local secret = headers["X-Secret"]
local allowed_ip = "${bind_ip}"
local allowed_secret = "${secret}"
-- 判断IP地址是否匹配
if allowed_ip and allowed_ip ~= "" then
if client_ip ~= allowed_ip then
ngx.exit(444)
end
end
-- 判断X-Secret值是否匹配
if allowed_secret and allowed_secret ~= "" then
if secret ~= allowed_secret then
ngx.exit(444)
end
end
}
proxy_pass https://api.openai.com;
proxy_ssl_name api.openai.com;
proxy_ssl_server_name on;
proxy_set_header Host api.openai.com;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
chunked_transfer_encoding on;
proxy_set_header X-Real-IP 8.8.8.8;
proxy_buffering off;
proxy_cache off;
proxy_redirect off;
proxy_hide_header Cache-Control;
# 不缓存,支持流式输出
#proxy_cache off; # 关闭缓存
#proxy_buffering off; # 关闭代理缓冲
#chunked_transfer_encoding on; # 开启分块传输编码
tcp_nopush on; # 开启TCP NOPUSH选项,禁止Nagle算法
tcp_nodelay on; # 开启TCP NODELAY选项,禁止延迟ACK算法
keepalive_timeout 300; # 设定keep-alive超时时间为65秒
}
location /status {
# 设置 Lua 代码的路径和参数
content_by_lua_block {
-- 获取请求头信息
local headers = ngx.req.get_headers()
-- 获取发送请求的 IP
local client_ip = ngx.var.remote_addr
local secret = headers["X-Secret"]
local allowed_ip = "${bind_ip}"
local allowed_secret = "${secret}"
-- 获取请求 URL 和 URI
local request_url = ngx.var.scheme .. "://" .. ngx.var.host .. ngx.var.request_uri
local request_uri = ngx.var.uri
-- 获取请求体
ngx.req.read_body()
local request_body = ngx.req.get_body_data()
-- 获取响应头
local response_headers = ngx.resp.get_headers()
-- 设置响应头和状态码
ngx.header["Content-Type"] = "text/html"
ngx.status = 200
-- 打印请求信息和响应信息
ngx.say("<html><head><title>Status</title></head><body>")
ngx.say("<h1>Request Information</h1>")
ngx.say("<pre>")
ngx.say("Request URL: ", request_url)
ngx.say("Request URI: ", request_uri)
ngx.say("Client IP: ", client_ip)
ngx.say("Setting IP: ", allowed_ip)
ngx.say("X-Secret: ", secret)
ngx.say("Setting Secret: ", allowed_secret)
ngx.say("Request Method: ", ngx.var.request_method)
ngx.say("</pre>")
ngx.say("<h1>Request Headers</h1>")
ngx.say("<pre>")
for k, v in pairs(headers) do
ngx.say(k .. ": " .. v)
end
ngx.say("</pre>")
ngx.say("<h1>Request Body</h1>")
ngx.say("<pre>")
ngx.say(request_body)
ngx.say("</pre>")
ngx.say("<h1>Response Information</h1>")
ngx.say("<pre>")
ngx.say("Response Headers: ")
for k, v in pairs(response_headers) do
ngx.say(k .. ": " .. v)
end
ngx.say("</pre>")
ngx.say("</body></html>")
ngx.exit(ngx.OK)
}
}
}
}