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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36" ""

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

4、更多

下面是也是本人近期在用 nginx 做方向代理的时候遇到的问题,我希望在日志中的我方向代理之后实际访问的地址,这种需求就需要用到和上面一样的思路,我们需要在 http > location 块中自定义相应的变量,然后将自定义的变量添加到打印日志所用的日志模板中即可,下面是相关配置,各位可做参考:

# 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;
}

如果觉得本文对您又帮助,欢迎收藏点赞哦!!!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值