Nginx修改access.log日誌時間格式
摘自:https://www.cnblogs.com/bigberg/p/7774508.html
安裝服務器:125.210.163.97
所需安裝軟件目錄:/opt/software
nginx需要模塊:
http_stub_status_module
http_ssl_module
http_gzip_static_module
http_mp4_module
安裝步驟:
1、pcre:
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure --prefix=/usr/local/src/pcre-8.35
pcre-config --version
2、zlib:
wget http://zlib.net/zlib-1.2.11.tar.gz
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install
3、nginx
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --prefix=/opt/nginx-1.14.0 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_mp4_module --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.35/
啟動:
/opt/nginx-1.14.0/sbin/nginx -c /opt/nginx-1.14.0/conf/nginx.conf
測試播放地址:
本地地址:http://125.210.163.97/mp4/hot/20180310/201803101700/20180310170000100000_QT.mp4
回源地址:
http://125.210.163.95/mp4/hot/20180801/201808010054/201808010054021002_IOS.mp4
http://125.210.163.97/mp4/hot/20180801/201808010054/201808010054021002_IOS.mp4
=================修改nginx的日期格式======================
要求需要修改nginx日誌中的時間格式20100823172711,而Nginx的默認日誌時間格式為23/Aug/2010:17:26:44 +0800
1.修改src/http/modules/ngx_http_log_module.c
第一處
修改前:
{ ngx_string("time_local"), sizeof("28/Sep/1970:12:00:00 +0600") - 1,
ngx_http_log_time },
修改後:
{ ngx_string("time_local"), sizeof("19700928120000 +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-5);
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);
效果對比:
修改前:
1|10/Oct/2018:17:39:30 +0800|CACHE|17|58.100.228.244|59190|Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A456 Safari/602.1|125.210.163.97|80|HTTP|http://125.210.163.97:80/mp4/hot/20180310/201803101700/20180310170000100000_QT.mp4?-|10/Oct/2018:17:39:30 +0800|4.372|10/Oct/2018:17:39:30 +0800|10/Oct/2018:17:39:30 +0800|1|100|8722531|426|TCP|0|8722183|0|206
修改後:
1|20181011113506|CACHE|3|58.100.228.249|53656|VLC/2.2.4 LibVLC/2.2.4|125.210.163.97|80|HTTP|http://125.210.163.97:80/mp4/hot/20180310/201803101700/20180310170000100000_QT.mp4?-|20181011113506|0.056|20181011113506|20181011113506|1|100|153638|186|TCP|0|153300|0|206
同時附帶一下nginx的平滑升級
好了,現在我們開始將Nginx平滑升級到穩定版V1.14.0。
1、下載最新的Nginx源碼安裝包
wget http://nginx.org/download/nginx-1.14.0.tar.gz
2、配置並編譯Nginx(不要執行make install步驟)
# tar zxvf nginx-1.14.0.tar.gz
# cd nginx-1.14.0
#./configure --prefix=/opt/nginx-1.14.0 --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_mp4_module --with-zlib=../zlib-1.2.11 --with-pcre=../pcre-8.35/
# make
(make編譯下就OK,不要執行make install。)
3、替換舊版本的Nginx
#mv /opt/nginx-1.14.0/sbin/nginx /opt/nginx-1.14.0//sbin/nginx.old
備份舊版本的nginx的二進制文件。
# cd objs/
在objs目錄下可以看到剛剛編譯好的新版本的nginx的二進制文件。
# cp nginx /opt/nginx-1.14.0/sbin/
復制新版本的nginx文件到你的nginx目錄。
# /opt/nginx-1.14.0/sbin/nginx -t
測試新版本的nginx是否配置正常。如果提示:
the configuration file /web/nginx/conf/nginx.conf syntax is ok
configuration file /web/nginx/conf/nginx.conf test is successful
則表示配置正確。
# kill 舊的nginx進程
killall nginx
4、重啟nginx服務
/opt/nginx-1.14.0/sbin/nginx -c /opt/nginx-1.14.0/conf/nginx.conf
# /opt/nginx-1.14.0/sbin/nginx -V
獲取Nginx版本信息,如果提示:
nginx version: nginx/1.14.0
則表示升級成功。
Nginx修改access.log日誌時間格式