0

the nginx log format is :

log_format  main  '$remote_addr [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$cookie_logintoken"';

I set the log_by_lua_file

log_by_lua_file xxxxx/ngx_lua_waf/log.lua;

and log.lua content:

ngx.req.set_header("User-Agent", "this is testing User-Agent")
ngx.req.set_header("Referer", "this is testing Referer")

and access.log change

127.0.0.1 [17/Dec/2016:16:21:47 +0800] "GET /test/client.php HTTP/1.1" 200 1370 "this is testing Referer" "this is testing User-Agent" "-" "-"

how can I change the nginx build-in var like $request ? I want to change the "GET /test/client.php HTTP/1.1" before nginx log to access.log

ngx.var.request = "xxxx" will be error :

failed to run log_by_lua*: xxxx/ngx_lua_waf/log.lua:15: variable "request" not changeable

but I dont konw how to change it with ngx.req.set_header

can anyone tell me how to change it?

1 Answer 1

1

You can modify several embedded Nginx variables using the Nginx Lua but the embedded request variable cannot be modified.

Actually, your starting assumption should be that embedded variables cannot, or, perhaps more accurately, should not, be modified.

Whenever you need a modified version of an embedded variable, define a custom variable, make your changes to that custom variable, and use this instead.

In your specific case:

    ### 
    ### Rewrite Phase ###
    ###

    #  Create your custom variable with a default value
    #  Runs before Log Phase so variable is available in Log Phase
    set $changed_request "-";


    ### 
    ### Log Phase ###
    ###

    ## log_by_lua* directives (Runs before inbuilt Log Phase directives)
    #  Update the custom variable etc
    log_by_lua_block {
        ngx.req.set_header("User-Agent", "this is testing User-Agent")
        ngx.req.set_header("Referer", "this is testing Referer")

        ngx.var.changed_request = ngx.var.request 
        -- Now do whatever changes you want to $changed_request.
    };

    ## Inbuilt Log Phase directives
    #  Define a custom log format with your custom variable
    log_format  customlogformat  '$remote_addr [$time_local] "$changed_request"'
        ' $status $body_bytes_sent "$http_referer" "$http_user_agent" '
        ' "$http_x_forwarded_for" "$cookie_logintoken"';

    #  Use your custom log format
    access_log /path/to/access.log customlogformat;
Sign up to request clarification or add additional context in comments.

2 Comments

seems only way to do
it's worth to mention that set is not valid in http context, you should use it elsewhere (e.g location). As log_by_lua_block runs at nginx log phase, wherever you use set to define variable should work. nit: there is no ; after log_by_lua_block.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.