lua-nginx-module icon indicating copy to clipboard operation
lua-nginx-module copied to clipboard

In order to set the headers of the upstream request, I use `ngx.req.set_header` in `rewrite_by_lua_block`, but my upstream endpoint cannot receive such header.

Open Hanoboo opened this issue 4 months ago • 5 comments

          > @agentzh I get a problem here.In order to set the headers of the upstream request, I use `ngx.req.set_header` in `rewrite_by_lua_block`, but my upstream endpoint cannot receive such header. here is my conf.
upstream foo {
        server 127.0.0.1:6666 weight=1;
    }

    server {
        listen 8001;
        location / {
            rewrite_by_lua_block {
                ngx.req.set_header("Connection", "Upgrade")
            }
            proxy_pass http://foo;
        }
    }
    server {
        listen 8002;
        location / {
            proxy_set_header Connection '';
            rewrite_by_lua_block {
                ngx.req.set_header("Connection", "Upgrade")
            }
            proxy_pass http://foo;
        }
    }

request send by 8001 port still cannot get the header "Connection: Upgrade"

Have you solved this problem?I have a similar problem,

Originally posted by @djfsb in https://github.com/openresty/lua-nginx-module/issues/437#issuecomment-948387694

Hanoboo avatar Feb 19 '24 02:02 Hanoboo

I don't know if proxy_pass_header helps.

zhuizhuhaomeng avatar Feb 19 '24 15:02 zhuizhuhaomeng

I don't know if proxy_pass_header helps.

It works,but I want to set the header dynamically in lua code.

Hanoboo avatar Feb 20 '24 08:02 Hanoboo

You should do something like this:

    upstream foo {
        server 127.0.0.1:6666 weight=1;
    }

    server {
        listen 8001;
        set $dynamicConnectionHeader ''; # default value
        location / {
            rewrite_by_lua_block {
                ngx.var.dynamicConnectionHeader = "Upgrade"
            }
            proxy_pass http://foo;
            proxy_set_header Connection $dynamicConnectionHeader;
        }
    }

    server {
        listen 8002;
        set $dynamicConnectionHeader 'Upgrade'; # default value
        location / {
            rewrite_by_lua_block {
                ngx.var.dynamicConnectionHeader = ""
            }
            proxy_pass http://foo;
            proxy_set_header Connection $dynamicConnectionHeader;
        }
    }

antpts avatar Feb 20 '24 20:02 antpts

You should do something like this:

    upstream foo {
        server 127.0.0.1:6666 weight=1;
    }

    server {
        listen 8001;
        set $dynamicConnectionHeader ''; # default value
        location / {
            rewrite_by_lua_block {
                ngx.var.dynamicConnectionHeader = "Upgrade"
            }
            proxy_pass http://foo;
            proxy_set_header Connection $dynamicConnectionHeader;
        }
    }

    server {
        listen 8002;
        set $dynamicConnectionHeader 'Upgrade'; # default value
        location / {
            rewrite_by_lua_block {
                ngx.var.dynamicConnectionHeader = ""
            }
            proxy_pass http://foo;
            proxy_set_header Connection $dynamicConnectionHeader;
        }
    }

The request header to be set is obtained dynamically through Lua and cannot be hard-coded with proxy_set_header.

Hanoboo avatar Feb 29 '24 08:02 Hanoboo

The connection header will be specially processed by nginx proxy (the default header value is "close"). You must use proxy_set_header to set the value of the connection header. You can use Lua to set other headers normally.

HanadaLee avatar Mar 31 '24 11:03 HanadaLee