Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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. #2282

Open
Hanoboo opened this issue Feb 19, 2024 · 5 comments

Comments

@Hanoboo
Copy link

Hanoboo commented Feb 19, 2024

          > @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 #437 (comment)

@Hanoboo Hanoboo changed the title > @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. 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. Feb 19, 2024
@Hanoboo Hanoboo changed the title 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. 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. Feb 19, 2024
@zhuizhuhaomeng
Copy link
Contributor

I don't know if proxy_pass_header helps.

@Hanoboo
Copy link
Author

Hanoboo commented Feb 20, 2024

I don't know if proxy_pass_header helps.

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

@antpts
Copy link

antpts commented Feb 20, 2024

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;
        }
    }

@Hanoboo
Copy link
Author

Hanoboo commented Feb 29, 2024

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.

@HanadaLee
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants