账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    关于 nginx 日志格式“乱码”问题
    39
    0

    nginx 版本 1.14.0,使用 lua-nginx-module 输出 resp body ;

    # 默认情况下日志输出 resp_body 里面的引号都被转成\x22
    "xxx.xxx.xx.xx" "-" "23/Aug/2019:02:34:23 +0000" "POST /tes/api HTTP/1.1" "200" "67" "-" "Python/3.6 aiohttp/3.5.4" "-" "0.004" "{\x22success\x22: \x22true\x22, \x22sysTime\x22: 1566527663202, \x22data\x22: \x220976604866\x22}"
    # 在 log_format 加上 escape=none 后,resp_body 输出正常,但是空字段变为“”,我想要的是“-”
    "xxx.xxx.xx.xx" "" "23/Aug/2019:02:18:21 +0000" "POST /tes/api HTTP/1.1" "200" "67" "" "Python/3.6 aiohttp/3.5.4" "" "0.004" "{"success": "true", "sysTime": 1566526701825, "data": "0976604866"}"
    # 我想要输出的结果是这样
    "xxx.xxx.xx.xx" "-" "23/Aug/2019:02:35:21 +0000" "POST /tes/api  HTTP/1.1" "200" "67" "-" "Python/3.6 aiohttp/3.5.4" "-" "0.005" "{"success": "true", "sysTime": 1566527721618, "data": "0976604866"}"

    通过修改 ngx_http_log_module.c 源码可以实现第三个结果,我想知道有没有办法不改源码,只通过配置实现?

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • @祸害苍生 普通会员 1楼

      Nginx 日志文件默认会使用UTF-8编码格式,如果你的日志文件中包含非UTF-8编码的内容,那么在输出的日志文件中就会出现乱码问题。

      要解决这个问题,你需要在Nginx配置文件中启用输出编码。在基本配置文件中,你可以通过在http部分的server部分添加以下代码来实现:

      ```nginx server { listen 80; server_name example.com;

      location / {
          root /var/www/example.com;
          index index.html index.htm;
          try_files $uri $uri/ /index.html;
      }
      
      location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass 127.0.0.1:9000;
      }
      

      } ```

      在上面的配置中,我们指定了根目录为/var/www/example.com,并且在.php$块中包含了FastCGI服务器的配置。

      如果你想在输出日志文件时使用另一种编码,你可以在access.logerror.log部分添加相应的配置:

      nginx access_log /var/log/access.log; error_log /var/log/error.log;

      在上面的配置中,我们指定了日志文件的输出路径。

      注意:不同的日志文件有不同的输出格式,你可以根据需要修改配置文件中的server_namerootindex等变量。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部