Nginx修改版本号显示和access.log日志时间格式
一、需求
默认时间格式: [02/Nov/2017:20:48:25 +0800]
需求格式: [2017-11-02 20:52:06]
默认版本号显示:
HTTP/1.1 200 OK
Server: nginx/1.10.2 **需要隐藏**
Date: Mon, 02 Apr 2018 07:13:35 GMT
Content-Type: text/html
Content-Length: 45345
Connection: keep-alive
Last-Modified: Thu, 07 Sep 2017 11:19:49 GMT
ETag: "59b12b55-b121"
Cache-Control: no-store
Accept-Ranges: bytes
二、实现
下载源码包修改如下配置文件(蓝字部分修改为红字部分),重新编译
1. 修改src/http/modules/ngx_http_log_module.c
1) { ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
ngx_http_log_time }, 修改后: { ngx_string("time_local"), sizeof("1970-09-28 12:00:00 +0600") - 1, ngx_http_log_time }, return ngx_cpymem(buf, ngx_cached_http_log_time.data, ngx_cached_http_log_time.len); 修改后: return ngx_cpymem(buf, ngx_cached_err_log_time.data, ngx_cached_err_log_time.len); 2. 修改 src/core/ngx_times.c 140行
(void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec); 修改后 (void) ngx_sprintf(p1, "%4d-%02d-%02d %02d:%02d:%02d", tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday, tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec); 3. 也可以通过如下命令行实现配置文件的修改
# sed -i -e 's/1.12.2//g' -e 's/nginx\//xxx(你想更改的版本名称)/g' -e 's/"NGINX"/"xxx(你想更改的版本名称)"/g' src/core/nginx.h
# sed -i -e "s#28\/Sep\/1970#1970-09-28#g" -e "s#ngx_cached_http_log_time#ngx_cached_err_log_time#g" src/http/modules/ngx_http_log_module.c
# sed -i "s#%4d\/%02d\/%02d#%4d-%02d-%02d#g" src/core/ngx_times.c
4. 重新编译安装
# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
# make && make install
征途漫漫,惟有奋斗!
浙公网安备 33010602011771号