Hi~
Would anyone tell me why openresty not supporting modify ngx.var.host?Will openresty consider supporting modify ngx.var.host in the future?

use ngx.req.set_header('Host', 'yourhost')

garoll
I try it and it works, thank you very much!
by the way, is there any way to modify the ngx.var.request_uri?

ccxhwmy
Anything can be modified,include uri parameter head body
ngx.req.set_method(method_id) method_id=ngx.HTTP_POST ngx.HTTP_GET
ngx.req.set_uri(uri, jump?)
ngx.req.set_uri_args("a=3&b=hello%20world") == ngx.var.args="a=3&b=hello%20world"
ngx.req.set_body_data("a=1&b=2")

also, you can use some nginx directive to sth
proxy_set_header field value;
proxy_set_body value;

ccxhwmy ngx.var.host is a constant variable which is provided by NGINX $host , and it is READ ONLY.

    Mickle this right,if uri like this "/test/aa?aa=1"
    modify /test/aa use ngx.req.set_uri()
    modify aa=1 use ngx.req.set_uri_args()

    Mickle
    Thank you for your answer, But I found that neither ngx.req.set_uri nor ngx.req.set_uri_args can modify the ngx.var.request_uri.
    The ngx.req.set_uri(uri, true) can jump to a new location, but the ngx.var.request_uri is old in the new location. It can only mofidy the uri which to upstream. Is there some way to modify the value of ngx.var.request_uri?

    garoll
    I found that the function ngx.req.set_uri() can not modify the value of ngx.var.request_uri. it can only modify the uri to upstream.

    garoll
    I want to modify the ngx.var.request_uri, so that can fetch the modified value from ngx.var.request_uri later.

    ccxhwmy I still don't quite understand what you mean by doing this. Can you explain it with a practical example?

    ccxhwmy
    ngx.req.set_uri_args(args)
    ngx.req.set_uri(uri,jump) --uri only ,Do not contain parameter
    The optional boolean jump argument can trigger location rematch (or location jump) as ngx_http_rewrite_module's rewrite directive, that is, when jump is true (default to false), this function will never return and it will tell Nginx to try re-searching locations with the new URI value at the later post-rewrite phase and jumping to the new location.

    if you want to give off all parameter,use follow method
    ngx.var.args=nil

      garoll
      Our project fetch the value of ngx.var.request_uri in many place to do something, but we want to modify the value of ngx.var.request_uri in the begin of a request recently.
      So we want to find out a way to modify it.
      Maybe openresty does not support this way.

        I config the nginx.conf like:

                location /request_aaa {
                    rewrite_by_lua_block {
                       ngx.log(ngx.ERR, "request_uri in location request_aaa: ", ngx.var.request_uri)
                       ngx.req.set_uri("/request_bbb", true)
                    }
                }
        
                location /request_bbb {
                    rewrite_by_lua_block {
                        ngx.log(ngx.ERR, "request_uri in location request_bbb: ", ngx.var.request_uri)
                        ngx.log(ngx.ERR, "uri in location request_bbb: ", ngx.var.uri)
                    }
                    proxy_pass http://127.0.0.1:8888;
                }

        I make a request like:

        curl "http://127.0.0.1/request_aaa"

        The error.log is:

        2022/08/05 22:47:26 [error] 18673#0: *25 [lua] rewrite_by_lua(nginx.conf:64):2: request_uri in location request_aaa: /request_aaa, client: 192.168.18.128, server: localhost, request: "GET /request_aaa HTTP/1.1", host: "192.168.18.128"
        2022/08/05 22:47:26 [error] 18673#0: *25 [lua] rewrite_by_lua(nginx.conf:71):2: request_uri in location request_bbb: /request_aaa, client: 192.168.18.128, server: localhost, request: "GET /request_aaa HTTP/1.1", host: "192.168.18.128"
        2022/08/05 22:47:26 [error] 18673#0: *25 [lua] rewrite_by_lua(nginx.conf:71):3: uri in location request_bbb: /request_bbb, client: 192.168.18.128, server: localhost, request: "GET /request_aaa HTTP/1.1", host: "192.168.18.128"

        We can find that ngx.var.request_uri does not change in the location /request_bbb

        show response from server 127.0.0.1:8888

        Mickle Hi~
        Unfortunately no response from server, there is no server 127.0.0.1:8888 in fact.
        Because I just want to display the error.log.

        ccxhwmy
        Use a nginx varible replice $request_uri in log format,for example $myrequest_uri ,declear it in nginx configure file
        set $myrequest_uri $request_uri;
        lua
        local tmp=ngx.var.uri
        if ngx.var.args then tmp=tmp.."?"..args end
        ngx.var.myrequest_uri=tmp

          ccxhwmy
          yes,The built-in variables of nginx is readonly.Because that's the behavior of the user's browser.Unless you change it with an external redirect.

          Write a Reply...