File size: 994 Bytes
7eff83b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
-- 解码URL函数
local function decode_uri(uri)
    local decoded = ngx.unescape_uri(uri)
    return decoded
end

-- 直接从请求URI获取完整URL
local request_uri = ngx.var.request_uri
ngx.log(ngx.DEBUG, "完整请求URI: ", request_uri)

-- 提取/proxy/后面的部分
local _, _, target_path = string.find(request_uri, "^/proxy/(.*)")
ngx.log(ngx.DEBUG, "提取的目标路径: ", target_path or "nil")

if not target_path or target_path == "" then
    ngx.status = 400
    ngx.say("错误: 未提供目标URL")
    return ngx.exit(400)
end

-- 解码URL
local target_url = decode_uri(target_path)
ngx.log(ngx.DEBUG, "解码后的目标URL: ", target_url)

if not target_url or target_url == "" then
    ngx.status = 400
    ngx.say("错误: 无法解析目标URL")
    return ngx.exit(400)
end

-- 记录日志
ngx.log(ngx.STDERR, "代理请求: ", target_url)

-- 设置目标URL变量供Nginx使用
ngx.var.target_url = target_url

-- 继续执行Nginx配置的其余部分
return