0

Is it possible to to something like this

    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;
    }
dj_boy
  • 1
  • 1
  • This looks a bit like XY problem. Could you clarify what is the actual problem you are trying to solve instead of the possible solution? – Tero Kilkanen Aug 14 '22 at 07:36
  • I want to forward the request to a user behavior analyzation system which examines the request and and might want to attach headers to the client @TeroKilkanen – dj_boy Aug 14 '22 at 19:11
  • https://www.nginx.com/resources/wiki/start/topics/examples/xsendfile/ might be the tool for this purpose. Your application would set in header which file nginx should send. – Tero Kilkanen Aug 14 '22 at 20:49

1 Answers1

0

Ok, I figuerd it out

    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;
    }
dj_boy
  • 1
  • 1
  • Nice solution! Won't you mind if I add a link to the [source](https://stackoverflow.com/questions/69818474/how-can-i-get-the-content-of-all-cookies-in-set-cookie-header-returned-by-upst/69860918#69860918)? ;) – Ivan Shatsky Aug 17 '22 at 21:39