nginx 位置反向代理然后返回静态文件(尝试文件)

nginx 位置反向代理然后返回静态文件(尝试文件)

有可能做这样的事情吗

    location / {
        proxy_pass https://example.com;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        // Add proxies response headers and serve a static file
        try_files $uri $uri/ =404;
    }

答案1

好的,我明白了

    location /rp {
        internal;
        proxy_pass https://example.com/rp.php; 
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Rp-Ol $scheme://$http_host$request_uri; // get original url
    }

    location / {
        access_by_lua_block {
            local res = ngx.location.capture("/rp")
            ngx.ctx.rp_cookie = res.header["Set-Cookie"]
            return
        }
        
        header_filter_by_lua_block {
            local function merge_cookies(a, b)
                local c = a or b
                if (a and b) == nil then return c end
                if type(c) == "string" then c = {c} end
                if type(b) == "string" then table.insert(c, b) else
                    for _, v in ipairs(b) do table.insert(c, v) end
                end
                return c
            end
            ngx.header["Set-Cookie"] = merge_cookies(ngx.header["Set-Cookie"], ngx.ctx.rp_cookie) 
        }
        

        try_files $uri $uri/ =404;
    }

相关内容