您好,欢迎来到汇智旅游网。
搜索
您的当前位置:首页nginx 通过配置文件配置自定义日期格式

nginx 通过配置文件配置自定义日期格式

来源:汇智旅游网

1、介绍

在使用 nginx 打印日志的时候,默认的日志日期格式不是我们习惯使用的 yyyy-MM-dd HH:mm:ss 的日期格式,而修改日期格式网上大部分的方法是需要修改源代码的,不用修改源代码的方式我也找到了一篇,大家可以参看  的实现方式,可惜我电脑是直接使用 windows 版本的 nginx(1.19.6),time_iso8601 这个内置变量没有,而且我也不想折腾到改源码(需要重新编译,麻烦,尤其是我这种懒人),或者安装 lua 模块的方式修改,只想直接在配置文件里面使用配置的方式修改就能实现,岂不美哉?

于是在我多番百度之下,终于摸索出了下面一个行之有效的方法(在本人 windows 版本的 nginx-1.19.6 版本没有问题!其他版本和平台未做实验,下面的配置大家可以作为参考

2、nginx 配置

2.1 相关配置

在 nginx 配置文件中的 server 块中加入如下配置:

# 使用 nginx 的 if 语句正则匹配 $time_local 中的各个部分
if ( $time_local ~ "^(\d+)\/(\w+)\/(\d+):(\d+):(\d+):(\d+) \+(\d+)" ) {
    # 日
	set $day $1;
    # 月
	set $month $2;
    # 年
	set $year $3;
    # 时
	set $hour $4;
    # 分
	set $minute $5;
    # 秒
	set $second $6;
}

# 由于 nginx 自带配置解析中我只发现了 if 判断语句,所以下面也只能一个一个的对应了
if ( $month = "Jan" ) {
	set $month "1";
}
if ( $month = "Feb" ) {
	set $month "2";
}
if ( $month = "Mar" ) {
	set $month "3";
}
if ( $month = "Apr" ) {
	set $month "4";
}
if ( $month = "May" ) {
	set $month "5";
}
if ( $month = "Jun" ) {
	set $month "6";
}
if ( $month = "Jul" ) {
	set $month "7";
}
if ( $month = "Aug" ) {
	set $month "8";
}
if ( $month = "Sep" ) {
	set $month "9";
}
if ( $month = "Oct" ) {
	set $month "10";
}
if ( $month = "Nov" ) {
	set $month "11";
}
if ( $month = "Dec" ) {
	set $month "12";
}

在 http 块下面添加自定义的日志格式,如下:

# escape=none 表示出现中文不转码(高版本 nginx 有此问题)
#  [$year-$month-$day $hour:$minute:$second] 中的各个变量是上面配置自定义的变量
log_format my_log_format escape=none  '$remote_addr - $remote_user [$year-$month-$day $hour:$minute:$second] "$request" '
										  '$status $body_bytes_sent "$http_referer" '
										  '"$http_user_agent" "$http_x_forwarded_for"';

之后在需要记录日志的 server 下添加日志访问配置

# logs/9999.access.log 表示日志存储路径
# my_log_format 代表格式化模板名称
access_log  logs/9999.access.log  my_log_format;

2.2 nginx 完整配置

为了方便在多个 server 中复用如期格式配置,我将日期转换配置抽离到 log-format.confnginx.conf 导入相应配置即可使用


#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"';
	# escape=none 表示出现中文不转码(高版本 nginx 有此问题)
	log_format my_log_format escape=none  '$remote_addr - $remote_user [$year-$month-$day $hour:$minute:$second] "$request" '
										  '$status $body_bytes_sent "$http_referer" '
										  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        # 导入日期格式转换配置,此种方式可以方便其他 server 复用
		include date-format.conf;
	
        listen       9999;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/9999.access.log  my_log_format;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
   
   # server {
   # 
   # }
}

 

3、验证

启动 nginx,访问 http://localhost:9999,查看 logs/9999.access.log,内容如下:

127.0.0.1 -  [2021-1-24 23:18:10] "GET /?%E5%92%8C HTTP/1.1" 304 0 "" "Mozilla/5.0 (Windows NT 10.0; Win; x) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36" ""

日期格式已经是我们自定义的日期格式了!!!

4、更多

# location 块中也可以使用 if 语句,下面不做展示,大家可以自行百度,
# 当 proxy 配置比较复杂的时候很有帮助哦
location ~* /xxx/xxx-xxxx-server/(v)/(.*) {
	# 使用变量 $my_upstream_url  反向代理地址,在日志模板中将此变量照葫芦画瓢加上就行
	set $my_upstream_url /xxx/xxx-xxx-server/v10/$2?$args;
	proxy_pass $proxy_server$my_upstream_url;
	proxy_set_header Host $host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header Cookie $http_cookie;
	proxy_set_header X-real-target-host $proxy_server;
	proxy_redirect off;
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- hzar.cn 版权所有 赣ICP备2024042791号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务