How to write only magazines with 200 status

I am trying to figure out how to do the following:

  • A request arrives.

  • HttpLuaModule takes some action against the request. If the request is valid, then Lua will complete the processing using ngx.exit(202) . But there are some conditions that can (and will) occur during processing, and nginx can return 403, 404, 503 Errors.

What I want to do is write only requests with a 200 status code to the access logs. Basically I would like to do something like this:

 location /foo { content_by_lua_file "/opt/nginx/lua/process.lua"; if (status == 200) { access_log "/path/to/the/access_log" } 

I am very new to nginx and lua, so for me it is a difficult task to find out where to place and if the instruction (broadcast after content_by_lua_file or in the side lua file), and what this statement should look like if.

+6
source share
4 answers

This is the solution I came across:

auth.lua

 -- Some logic goes here -- .... -- .... ngx.var.return_status = 200 

nginx.conf

 http { lua_package_path .....; lua_package_cpath ....; rewrite_by_lua_no_postpone on; server { set $return_status 1; location /foo { rewrite_by_lua_file "<apth_to_aut.lua"; if ($return_status = 200) { access_log <path_to_access_log> format; return 200; } } } } 
+2
source

nginx 1.7.0+ allows you to use the if condition in the access_log directive.

 access_log path [format [buffer=size [flush=time]] [if=condition]]; The if parameter (1.7.0) enables conditional logging. A request will not be logged if the condition evaluates to "0" or an empty string 

In combination with the map directive, you can send log events to different logs based on various conditions.

 http { map $status $normal { ~^2 1; default 0; } map $status $abnormal { ~^2 0; default 1; } map $remote_addr $islocal { ~^127 1; default 0; } server { access_log logs/access.log combined if=$normal; access_log logs/access_abnormal.log combined if=$abnormal; access_log logs/access_local.log combined if=$islocal; } } 

http://nginx.org/en/docs/http/ngx_http_log_module.html
http://nginx.org/en/docs/http/ngx_http_map_module.html

+17
source

you can do this using the ngx.log and log_by_lua .

 location /conditional_log{ log_by_lua 'if ngx.status == 200 then ngx.log(ngx.ERR, "It is 200") end'; content_by_lua 'ngx.say("I am ok") ngx.exit(200)'; } 

In the above code, we use log_by_lua , which is called during operation in the log phase. In this case, if ngx.status == 200 , we use ngx.log to start the log using ngx.log .

This will be written on error_log . Not sure how to write it to access_log .

For reference

http://wiki.nginx.org/HttpLuaModule#ngx.log

http://wiki.nginx.org/HttpLuaModule#log_by_lua

+3
source

Each question has a part of the answer. You were very close:

 if ($status != "200") { access_log off; } 

Check version information. http://nginx.org/en/docs/http/ngx_http_core_module.html#variables

In addition, almost all access log log formats are available in the β€œmodern” versions: http://nginx.org/en/docs/http/ngx_http_log_module.html

+2
source

Source: https://habr.com/ru/post/954622/


All Articles