最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

配置Nginx日志url encode問題及解決

 更新時間:2026年05月06日 09:38:18   作者:這個饕字怎么讀  
文章介紹了在Nginx中配置自定義日志輸出格式,處理URL參數(shù)中文編碼的問題,方法一是安裝Lua模塊并使用`escape=json`;方法二是安裝`set-misc-nginx-module`模塊,這兩種方法均能解決中文輸出為十六進制字符串的問題

問題描述

當自定義日志輸出格式,需要輸出http請求中url參數(shù)時,如果參數(shù)中包含中文,是會進行url encode的,所以輸出都是編碼后的字符串,比如我配置的:

log_format test_log escape=json '{ "timestamp": "$msec", '
    '"request": "$request",'
    '"name": "$arg_name",'
    '"uuid": "$http_uuid",'
    '"remoteAddr": "$remote_addr" }';

請求 http://192.168.108.130:80/lua?name=我是中文

arg_name輸出時就會是這樣子:

{ “timestamp”: “1740829638.783”, “request”: “GET /lua?name=%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87 HTTP/1.1”,“name”: “%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87”,
“uuid”: “-”,“remoteAddr”: “192.168.108.1” }

所以目的是需要把它url decode再輸出

方法1-lua

首先需要安裝Lua,具體可以網(wǎng)上找教程文章看下,這里只提及簡要流程

安裝lua所需模塊包括:

  1. lua-nginx-module
  2. ngx_devel_kit
./configure --prefix=/usr/local/nginx \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi \
    --with-http_stub_status_module \
    --with-http_ssl_module \
    --with-file-aio \
    --with-http_realip_module \
    --with-openssl=/usr/local/software/openssl-1.1.1w \
    --add-module=/usr/local/software/lua-nginx-module-0.10.27rc1 \
    --add-module=/usr/local/software/ngx_devel_kit-0.3.3

然后 make(注意,如果無需替換原nginx目錄的內(nèi)容,則只需要make,不需要make install,然后編譯后的objs目錄里,把nginx可執(zhí)行文件copy過去即可,可以先把原來nginx可執(zhí)行文件備份下)

另外需要本地先安裝luajit,比如我安裝的 luajit2-2.1-20240626

另外有可能會報錯 resty 相關(guān)的錯誤,網(wǎng)上文章有提到用 lua_load_resty_core off; 解決,但是我實際測試無效,故按照錯誤提示又下載了相關(guān)的包:

  1. lua-resty-core
  2. lua-resty-lrucache

nginx.conf 中添加配置:lua_package_path “/usr/local/software/lua-resty-core-0.1.29/lib/?.lua;”;

安裝完成之后,可以測試下lua:

location /lua {
    default_type 'text/plain';

    content_by_lua 'ngx.say("hello, lua")';
}

之后加上我們的url解析配置:

set $decoded_arg_name  '';
location /lua {
    default_type 'text/plain';

    content_by_lua 'ngx.say("hello, lua")';
    access_by_lua_block {
            local args = ngx.req.get_uri_args()
                if args.name then
                  local decoded_name = ngx.unescape_uri(args.name)
                   ngx.var.decoded_arg_name = decoded_name
                   ngx.log(ngx.ERR, "Decoded name: ", decoded_name) # 打印日志調(diào)試用
            end
        }
}

完整nginx配置代碼如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    log_format test_log escape=json '{ "timestamp": "$msec", '
    '"request": "$request",'
    '"name": "$decoded_arg_name",'
    '"uuid": "$http_uuid",'
    '"remoteAddr": "$remote_addr" }';
    #access_log  logs/access.log  main;
    access_log logs/test_access.log test_log;

    sendfile        on;
    #tcp_nopush     on;
    lua_load_resty_core off;
    lua_package_path "/usr/local/software/lua-resty-core-0.1.29/lib/?.lua;";

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        set $decoded_arg_name  '';
        #charset koi8-r;
        charset utf-8;
       
        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        } 
        location /lua {
            default_type 'text/plain';
            
            content_by_lua 'ngx.say("hello, lua")';
            access_by_lua_block {
                    local args = ngx.req.get_uri_args()
                        if args.name then
                          local decoded_name = ngx.unescape_uri(args.name)
                           ngx.var.decoded_arg_name = decoded_name
                           ngx.log(ngx.ERR, "Decoded name: ", decoded_name)
                    end
                } 
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

但加上上面這一部還不夠,lua的輸出日志,如果是非ascii碼,會輸出為十六進制字符串:像是:

{ “timestamp”: “1740835690.677”, “request”: “POST /lua?name=%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87 HTTP/1.1”,“name”: “\xE6\x88\x91\xE6\x98\xAF\xE4\xB8\xAD\xE6\x96\x87”,“uuid”: “testuuid”,“remoteAddr”: “192.168.108.1” }

對于這個問題,偏新版的nginx可以通過加上 escape=json解決:

log_format test_log escape=json '{ "timestamp": "$msec", '
    '"request": "$request",'
    '"name": "$decoded_arg_name",'
    '"uuid": "$http_uuid",'
    '"remoteAddr": "$remote_addr" }';

至此打印的日志則是中文了:

{ “timestamp”: “1740834963.209”, “request”: “POST /lua?name=%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87 HTTP/1.1”,“name”: “我是中文”,“uuid”: “testuuid”,“remoteAddr”: “192.168.108.1” }

方法2-set-misc-nginx-module

在nginx中添加此模塊,下載模塊包 set-misc-nginx-module-0.33 解壓后,configure命令如下:

./configure --prefix=/usr/local/nginx \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi \
    --with-http_stub_status_module \
    --with-http_ssl_module \
    --with-file-aio \
    --with-http_realip_module \
    --with-openssl=/usr/local/software/openssl-1.1.1w \
    --add-module=/usr/local/software/lua-nginx-module-0.10.27rc1 \
    --add-module=/usr/local/software/ngx_devel_kit-0.3.3 \
    --add-module=/usr/local/software/set-misc-nginx-module-0.33

以上添加模塊的lua相關(guān)的兩個模塊,可能可以不用加,但我測試的時候沒有去掉,這里可以看大家需求

然后 make(注意,如果無需替換原nginx目錄的內(nèi)容,則只需要make,不需要make install,然后編譯后的objs里,把nginx可執(zhí)行文件copy過去即可,可以先把原來nginx可執(zhí)行文件備份)

更改 nginx.conf

set $decoded_arg_name  '';
set_unescape_uri $decoded_arg_name $arg_name;

再同樣加上 escape=json

其他什么的都不用配置了,實現(xiàn)效果是一樣的。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

莱西市| 余江县| 冕宁县| 重庆市| 饶平县| 申扎县| 项城市| 达孜县| 东方市| 新沂市| 周宁县| 江永县| 泌阳县| 东至县| 青岛市| 绍兴市| 金坛市| 新晃| 泾阳县| 财经| 彰化市| 和硕县| 株洲市| 怀安县| 北京市| 广饶县| 西青区| 都匀市| 衡山县| 合水县| 肇庆市| 山东省| 拉萨市| 霍邱县| 康马县| 德州市| 惠水县| 府谷县| 苏尼特左旗| 新竹市| 武城县|