nginx修改body、cookie、重定向等小技巧

nginx+lua常见小技巧
1、路由到指定url

 location /hub/console/shaanxi-adapter/api/token/tplogin {
        proxy_pass http://debot:30900/console/shaanxi-adapter/api/token/tplogin;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

2、透传cookie,修改cookie路径
当后端返回的 Cookie 路径为子路径(如 /usermanager),需重写为根路径 / 以确保前端能正确读取。

location /usermanager/loginum.action{
            proxy_pass http://um:7200/usermanager/loginum.action;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            header_filter_by_lua_block {
   			local cookies = ngx.resp.get_headers()["Set-Cookie"]
    		if cookies then
                            if type(cookies) == 'string' then
                                local new_cookies = string.gsub(cookies,"Path=/usermanager", "Path=/")
                ngx.header["Set-Cookie"] = new_cookies
                -- ngx.header.add("Set-Cookie", cookies)
            return
        end
        for i,v in ipairs(cookies) do 
            cookies[i] = string.gsub(v,"Path=/usermanager", "Path=/")
        end
         ngx.header["Set-Cookie"] = cookies
        -- ngx.header.add("Set-Cookie", cookies)
    end
}

3、改写body,将内网url映射为外网可访问的url
某些接口(如 OAuth 重定向)会返回内网 URL,需在网关层替换为公网可访问地址。

location /hub/console/oauth2/tokens {
        # proxy_pass http://debot:30900/aihub/1d18da5cf5d641fb876b766b6498c22c;
        proxy_pass http://debot:30900/console/oauth2/tokens;
        #proxy_pass http://um:28080/login?client_id=&username=admin;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_read_timeout 360;
        proxy_buffering off;
        body_filter_by_lua_block {
                local chunk,eof = ngx.arg[1], ngx.arg[2]
                local ctx = ngx.ctx
                if not ctx.body then
                        ctx.body=""
                end
                ctx.body = ctx.body .. chunk

                ngx.arg[1] = nil

                if  ngx.arg[2] then
                        local cjson = require "cjson"
                        local ok ,json = pcall(cjson.decode, ctx.body)

                        if not ok then
                            return
                        end

                        if json.code == "302" and type(json.msg) == "string" then
                            ngx.log(ngx.ERR, "origin msg", json.msg)
                            local new_url = string.gsub(json.msg, "http://[^/]+", "http://debot:28080")
                            json.msg = new_url

                            ngx.log(ngx.ERR, "final msg", json.msg)
                            local modified_body = cjson.encode(json)

                            ngx.arg[1]  = modified_body
                        else 
                            ngx.arg[1] = ctx.body
                        end

                end
        }
}

4、部分url重定向地址
将特定前端资源请求重写到统一前缀路径,兼容内外网不同访问路径。

location / {
    rewrite_by_lua_block{
                        if ngx.var.uri:match("^/asr/index.umd.js") or ngx.var.uri:match("^/favicon.ico") then
                                ngx.req.set_uri("/hub" ..ngx.var.uri)
                        end

                }
                proxy_pass http://debot:30900/;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_read_timeout 360;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "Upgrade";
}
posted @ 2025-12-17 14:26  瞌睡虫245  阅读(1)  评论(0)    收藏  举报