Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Add the requested response log to the nginx log (recommended)

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Because this function is not included in the built-in functions of nginx, the third-party module ngx_lua needs to be installed. Because this module requires Lua language, the corresponding Lua language package needs to be installed.

1. Download and install LuaJIT

# cd / usr/local/src# wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz# tar-xzvf LuaJIT-2.0.2.tar.gz# cd LuaJIT-2.0.2# make

The following indicates that the compilation is successful

OK Successfully built LuaJITmake [1]: Leaving directory `/ usr/local/src/LuaJIT-2.0.2/src'==== Successfully built LuaJIT 2.0.2 = = # make install

The following appears to indicate that the installation is successful

= Successfully installed LuaJIT 2.0.2 to / usr/local = =

two。 Download and prepare nginx lua module

# cd / usr/local/src# wget https://github.com/chaoslawful/lua-nginx-module/archive/v0.8.6.tar.gz# tar-xzvf v0.8.6

3. Install nginx

# cd / usr/local/src/# wget http://nginx.org/download/nginx-1.4.2.tar.gz# tar-xzvf nginx-1.4.2.tar.gz# cd nginx-1.4.2// imports environment variables first Tell nginx where to find luajit# export LUAJIT_LIB=/usr/local/lib# export LUAJIT_INC=/usr/local/include/luajit-2.0#. / configure-- prefix=/usr/local/nginx-1.4.2-- add-module=../lua-nginx-module-0.8.6# make-jacked make install

4. Test whether the installation is successful

# cd / usr/local/nginx-1.4.2/conf/# vi nginx.conf

Lua instruction mode

Add a localtion to server

Location / hello {default_type 'text/plain'; content_by_lua' ngx.say ("hello, lua")';}

Then start nginx

# cd / usr/local/nginx-1.4.2/sbin#. / nginx

Browser access:

Http://127.0.0.1/hello

Display: hello, lua

Note:

1. Note that there can be no errors in the installation directory of each module

two。 If you have previously installed nginx through apt-get, you need to remove nginx first, because the software downloaded in this way cannot be compiled

3.

This indicates that the third party installation was successful.

5. Let's start adding logs.

Http {log_format mylog 'response_body:$resp_body'; server {# record nginx request return value lua_need_request_body on; set $resp_body ""; body_filter_by_lua' local resp_body = string.sub (ngx.arg [1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or "").. Resp_body if ngx.arg [2] then ngx.var.resp_body = ngx.ctx.buffered end'; location / {proxy_pass http://127.0.0.1:5000; access_log / var/log/nginx/access.log mylog;}

At this point, the response log has been successfully added.

If you understand the Lua language, you can modify the following code to better meet your requirements.

Body_filter_by_lua 'local resp_body = string.sub (ngx.arg [1], 1, 1000) ngx.ctx.buffered = (ngx.ctx.buffered or "").. Resp_body if ngx.arg [2] then ngx.var.resp_body = ngx.ctx.buffered end'

When the log is output, it will be found that the language response result is unrecognizable when it is converted to hexadecimal when the characters are involved.

Some of the response logs are as follows:

Response_body: {\ x22code\ x22: 404,\ x22message\ X22:\ X22\ xE8\ xAF\ xB7\ xB1\ X82\ xE7\ X9A\ X84\ xE8\ xB5\ x84\ xBA\ xE4\ xB8\ xE5\ xAD\ x98\ xE5\ x9C\ xA8\ X22,\ x22data\ x22: {}

This can be solved by copying to a python script.

Str1=''' {\ x22code\ x22: 404,\ x22message\ x22:\ X22\ xE8\ xAF\ xB7\ xE6\ xB1\ X82\ xE7\ X9A\ X84\ xE8\ xB5\ x84\ xE6\ xBA\ xE4\ xB8\ x8D\ xAD\ x98\ xE5\ x9C\ xA8\ X22,\ x22data\ x22: {} 'print (str1.encode (' raw_unicode_escape'). Decode ('utf-8'))

The input result is:

{"code": 404, "message": "requested resource does not exist", "data": {}}

Summary

The above is the nginx log introduced by the editor to add the requested response log. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply you in time. Thank you very much for your support to the website!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 214

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report