我的目的是代理一个通向passport的请求,例如将/apis/passport/user的请求代理到 /api/passport/user。
我的问题出现在proxy.lua中的第9行:ngx.header["Set-Cookie"] = res.header["Set-Cookie"] 这句代码运行不是很稳定。
在运行一段时间后,这句代码无法正常的将cookie返回到浏览器,但是reload之后,又恢复正常。
该问题出现的频率与对passport的请求数量有关系,请求多的情况下,30分钟左右就会出现该问题,且无法自行恢复,必须reload配置或者restart服务后才恢复正常。
这个问题是因为我的配置问题,还是代码问题,或者是我不应该采用这种代理方式?
望指教。
nginx.conf
daemon off;
worker_processes 1;
error_log logs/error.log error;
events{
worker_connections 1024;
}
http {
lua_package_path '$prefix/lua/?.lua;$prefix/lua/resty/?.lua;D:/openresty/resty/?.lua;/blah/?.lua;;';
upstream passport {
server localhost:3478;
keepalive 2000;
}
client_max_body_size 13m;
gzip on;
include conf/*.conf;
}
site.conf
server {
listen 6699;
server_name 192.168.250.135;
proxy_pass_request_headers on;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header Authorization $http_authorization;
proxy_set_header ClientApp-ID $http_clientapp_id;
proxy_set_header ClientApp-Version $http_clientapp_version;
proxy_set_header Cookie $http_cookie;
location ~* ^/apis/passport/(.*) {
set $req_uri $1;
content_by_lua_file lua/apis/proxy.lua;
}
location /api/passport {
internal;
proxy_pass http://passport/api/;
}
}
proxy.lua
ngx.req.read_body()
local body = ngx.req.get_body_data()
local proxyUrl = "/api/passport/" ..ngx.var.req_uri
ngx.req.set_header("user-agent", "NginxClient")
local res = ngx.location.capture(proxyUrl, { method = ngx.POST, body = body })
ngx.header["Set-Cookie"] = res.header["Set-Cookie"]
ngx.status = res.status
ngx.say(res.body)
ngx.exit(res.status)