Linux Learning Summary (41) Nginx Access log configuration, Web cache expiration date Configuration

Source: Internet
Author: User

When we were learning about Apache, we had access to the log, remember where the log format is defined, and in HTTPD's main configuration file, /usr/lcoal/apache2.4/conf/httpd.confSearch Logformat can see, the system gave us two formats, combined and common we chose to use the combined, it recorded more comprehensive information. Next we define the path and type of the log in the virtual host configuration file. /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

1 Access logs

Search Log_format in nginx config file to get log format information
cat /usr/local/nginx/conf/nginx.conf |grep -A2 log_format

The meanings of each field are:

The nginxlog that follows Log_format is the name of the log format, which is called later.
Next we go to the virtual host configuration file to specify the path to the access log. That is, in the original virtual host configuration file to add a line of content: Acces_log/tmp/nginx_accesslog Nginxlog;

vim /usr/local/nginx/conf/vhost/test.com.confserver{    listen 80;    server_name test.com test1.com test2.com;    index index.html index.htm index.php;    root /data/wwwroot/test.com;    if ($host != ‘test.com‘ ) {        rewrite  ^/(.*)$  http://test.com/$1  permanent;    }    acces_log /tmp/nginx_access.log nginxlog;}

-t-s Reload Test
To access a nonexistent Web page
curl -x127.0.0.1:80 test.com/sdfsdf

2nd Log Cutting

NIGNX's access logs are not like Apache's own cutting tools, so we're going to define a log cut script to implement log cutting.

vim /usr/local/sbin/nginx_log_rotate.sh#! /bin/bashd=`date -d "-1 day" +%Y%m%d`logdir="/tmp/"nginx_pid="/usr/local/nginx/logs/nginx.pid"cd $logdirfor log in `ls *.log`do    mv $log $log-$ddone/bin/kill -HUP `cat $nginx_pid`

Note:
Kill-hup PID
The PID is the process identity. Use this command if you want to change your configuration without stopping and restarting the service. After making the necessary changes to the configuration file, issue the command to dynamically update the service configuration. By convention, when you send a pending signal (signal 1 or HUP), most server processes (all common processes) perform a reset operation and reload their configuration files
We run the next script to see if the cut

sh /usr/local/sbin/nginx_log_rotate.shls /tmp/*log*

3 Configure static files do not log logs, configure cache expiration time

The requirements are the same as httpd, but the configuration is much simpler. Change the virtual host configuration file to the following:

server{    listen 80;    server_name test.com test1.com test2.com;    index index.html index.htm index.php;    root /data/wwwroot/test.com;    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$     {          expires      7d;          access_log off;    }   location ~ .*\.(js|css)$    {          expires      12h;          access_log off;    }}

echo "1111111111" >/data/wwwroot/test.com/1.js//Simulate a JS file
echo "22222222222" >/data/wwwroot/test.com/2.jpg//simulate a JPG file
TOUCH/DATA/WWWROOT/TEST.COM/1.JSS//Create a contrast file that does not belong to the above configuration
Curl-x127.0.0.1:80 Test.com/1.js-i

Curl-x127.0.0.1:80 Test.com/2.jpg-i

Curl-x127.0.0.1:80 Test.com/1.jss-i

We can see from the previous two access results that there is a max-age, that is, the cache that we defined earlier is valid for the duration, in seconds. One of the JSS files is not in the configuration, so there is no such item.
Finally, we look at the access log, found that JS and jpg file access is not recorded.

Linux Learning Summary (41) Nginx Access log configuration, Web cache expiration date Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.