我正在设置一个 openresty 反向代理,它将根据路径将流量调度到两个后端之一。
我希望来自 backend1 的某些 cookie 不会传递到 backend2。
我应该怎么做 ?
这是我的 nginx.conf:
worker_processes 1;
error_log stderr;
env BACKEND_URL;
env BACKEND2_URL;
events {
worker_connections 1024;
}
http {
resolver local=on;
server {
listen 80;
root /usr/share/nginx/html;
include /usr/local/openresty/nginx/conf/mime.types;
set_by_lua $backend_url 'return os.getenv("BACKEND_URL")';
set_by_lua $backend2_url 'return os.getenv("BACKEND2_URL")';
location / {
try_files $uri $uri/ /index.html;
}
location ~ /backend/(.*)$ {
proxy_pass $backend_url$1$is_args$args;
}
location /backend2/ {
access_by_lua_block {
}
proxy_pass $backend2_url;
}
}
}
答案1
您可以使用以下内容。
rewrite_by_lua_file /usr/local/openresty/lualib/remove_cookies.lua;
proxy_set_header Cookie $modified_cookie;
set $modified_cookie "";
在你的 remove_cookies.lua 文件中,有
local whiteList = { "cookiename1", "cookiename2" } ...
local newCookieHeader = nil
for index, value in ipairs(whiteList) do
local cookieValue = assert(loadstring("return ".. "ngx.var.cookie_" .. value))()
if cookieValue ~= nil then
if newCookieHeader == nil then
newCookieHeader = value .. "=" .. cookieRequestValue .. "; "
else
newCookieHeader = newCookieHeader .. value .. "=" .. cookieRequestValue .. "; "
end
end
end
ngx.var.modified_cookie = newCookieHeader