1. nginx access diary:
Diary format: search log_format in the main configuration file nginx.conf;
[root@localhost_001 conf]# vim nginx.conf log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]' ' $host "$request_uri" $status' ' "$http_referer" "$http_user_agent"';
Note: combined_realip represents the name of the diary format, which can be defined at will. What name is defined here, and what name is referenced later, determines the type of virtual host reference diary;
Note: Each section of the nginx configuration file ends with a semicolon. If there is no semicolon in the configuration section, it means that the section is not finished.
$remote_addr | Client IP (Public Network IP) |
---|---|
$http_x_forwarded_for | IP of Proxy Server |
$time_local | Server local time |
$host | Access Host Name (Domain Name) |
$request_uri | url address accessed |
$status | Status code |
$http_referer | Referer (jump page) |
$http_user_agent | User_agent (identity) |
(2): The virtual host configuration file in the virtual host vhost directory is also needed to define the path to access the diary.
[root@localhost_001 vhost]# vim test.com.conf [root@localhost_001 vhost]# cat test.com.conf server { listen 80; server_name www.test.com bbs.test.com test1.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'www.test.com' ) { rewrite ^/(.*)$ http://www.test.com/$1 permanent; } access_log /tmp/test.com.log combined_realip; #Define the path to access the diary; }
Note: If you do not write the log format, it will follow the default log format;
(3): Check if there is an error in the configuration file and reload the configuration file;
[root@localhost_001 conf]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost_001 conf]# /usr/local/nginx/sbin/nginx -s reload
(4): Test, curl command access;
[root@localhost_001 conf]# curl -x127.0.0.1:80 www.test.com -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 08:54:49 GMT Content-Type: text/html Content-Length: 15 Last-Modified: Tue, 16 Oct 2018 06:36:04 GMT Connection: keep-alive ETag: "5bc586d4-f" Accept-Ranges: bytes [root@localhost_001 conf]# curl -x127.0.0.1:80 bbs.test.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 08:55:09 GMT Content-Type: text/html Content-Length: 184 Connection: keep-alive Location: http://www.test.com/
(6): Check the access diary;
[root@localhost_001 vhost]# tail /tmp/test.com.log 127.0.0.1 - [16/Oct/2018:16:54:49 +0800] www.test.com "/" 200 "-" "curl/7.29.0" 127.0.0.1 - [16/Oct/2018:16:55:09 +0800] bbs.test.com "/" 301 "-" "curl/7.29.0" 192.168.149.135 - [16/Oct/2018:16:56:16 +0800] www.test.com "/" 304 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
2. nginx diary cutting;
Note: nginx does not have its own log cutting tool, it can only be implemented by means of log cutting tools of the system or scripts written by itself.
[root@localhost_001 vhost]# vim /usr/local/sbin/nginx_log_rotate.sh cat /usr/local/sbin/nginx_log_rotate.sh #! /bin/bash d=`date -d "-1 day" +%Y%m%d` logdir="/tmp/" nginx_pid="/usr/local/nginx/logs/nginx.pid" cd $logdir for log in `ls *.log` do mv $log $log-$d done /bin/kill -HUP `cat $nginx_pid`
Note: d=`date-d','-1 day', +% Y%m%d': Generate yesterday's diary in the form of year, month and day;
logdir="/tmp":: Define a directory;
nginx_pid="/usr/local/nginx/logs/nginx.pid": Find the PID of nginx for execution of / bin/kill-HUP `cat $nginx_pid`
/ bin/kiall-HUP `cat $nginx_pid': reload to generate a new / nginx_pid='/usr/local/nginx/logs/nginx.pid'
(2): execute the shell script, and add x to view the execution process;
[root@localhost_001 vhost]# sh -x /usr/local/sbin/nginx_log_rotate.sh ++ date -d '-1 day' +%Y%m%d + d=20181015 + logdir=/tmp/ + nginx_pid=/usr/local/nginx/logs/nginx.pid + cd /tmp/ ++ ls test.com.log + for log in '`ls *.log`' + mv test.com.log test.com.log-20181015 ++ cat /usr/local/nginx/logs/nginx.pid + /bin/kill -HUP 6959
(3): Look at the cut diary file;
[root@localhost_001 vhost]# ls -la /tmp/test* -rw-r--r-- 1 root root 0 10 Month 1617:28 /tmp/test.com.log -rw-r--r-- 1 root root 349 10 Month 1616:56 /tmp/test.com.log-20181015
Note: It also needs to be written into crontab.
[root@localhost_001 vhost]# crontab -l 0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh
(4) Sometimes the diary needs to be deleted regularly after it is cut.
[root@localhost_001 vhost]# find /tmp/ -name *.log-* -type f -mtime +30 |xargs rm -fr
3. Static files do not record diary and expiration time.
Edit the virtual host configuration file: / usr/local/nginx/conf/vhost/test.com.conf
[root@localhost_001 vhost]# vim test.com.conf server { listen 80; server_name www.test.com bbs.test.com test1.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'www.test.com' ) { rewrite ^/(.*)$ http://www.test.com/$1 permanent; } access_log /tmp/test.com.log combined_realip; #The following is the configuration that does not record static files, does not record diaries and expiration times. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #Files that match the suffix gif|jpg|jpeg|png|bmp|swf { expires 7d; #Overdue 7 days later access_log off; #Match ". *. (gif|jpg|jpeg|png|bmp|swf)" to close the log } location ~ .*\.(js|css)$ { expires 12h; #Overdue 12 hours later access_log off; #Match ". *. (js|css)" to close the log } }
(2): Detecting and reloading configuration files;
[root@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful [root@localhost_001 vhost]# /usr/local/nginx/sbin/nginx -s reload
(3): Testing: using curl command to test;
First, upload A. jpg image and. js file using the rz command of xhell; as follows:
[root@localhost_001 test.com]# ls 11.js admin.php index.html kaola.jpg
(4): Next, use the curl command to do the access test.
[root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com/kaola.jpg -I HTTP/1.1 200 OK Server: nginx/1.4.7 Content-Type: image/jpeg Last-Modified: Tue, 14 Jul 2009 05:32:31 GMT [root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com/11.js -I HTTP/1.1 200 OK Server: nginx/1.4.7 Content-Type: application/x-javascript Content-Length: 46122 [root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com test.com fast
(5): Look at the diary and see only one access diary.
[root@localhost_001 test.com]# tail /tmp/test.com.log 127.0.0.1 - [16/Oct/2018:17:57:13 +0800] www.test.com "/" 200 "-" "curl/7.29.0"
(6): Test expiration time, plus - I option;
[root@localhost_001 test.com]# curl -x127.0.0.1:80 www.test.com/kaola.jpg -I HTTP/1.1 200 OK Server: nginx/1.4.7 Date: Tue, 16 Oct 2018 09:58:42 GMT Content-Type: image/jpeg Content-Length: 780831 Last-Modified: Tue, 14 Jul 2009 05:32:31 GMT Connection: keep-alive ETag: "4a5c186f-bea1f" Expires: Tue, 23 Oct 2018 09:58:42 GMT Cache-Control: max-age=604800 Accept-Ranges: bytes
Note: max-age=604800 expiration time;
Note: If expires are removed from the configuration file, the expire time will not be displayed.