Apache (httpd) configuration--log control and static element expiration time configuration

Source: Internet
Author: User

One, the access log does not log static files

Many sites are static Web pages, Web pages within the image, CSS files and other links to the same URL, if not set, these invalid information will also be stored in the access log, resulting in a rapid increase in Access log file size, occupy a large amount of storage space. We can save memory resources by setting not to log certain files to reduce invalid information.
So how does it work? Follow the example below:

Step: Edit the Virtual host configuration file
[[email protected] ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf    ErrorLog "logs/123test-error_log"    #以下为定义变量:将所有关于图片,css,js的请求定义为变量img    SetEnvIf Request_URI ".*\.gif$" img    SetEnvIf Request_URI ".*\.jpg$" img     SetEnvIf Request_URI ".*\.png$" img    SetEnvIf Request_URI ".*\.bmp$" img    SetEnvIf Request_URI ".*\.swf$" img    SetEnvIf Request_URI ".*\.js$" img    SetEnvIf Request_URI ".*\.css$" img    # "env=!img"表示非img变量。意思是:不记录关于变量img的请求日志。    CustomLog "logs/123test-access_log" combined env=!img[[email protected] ~]# /usr/local/apache2/bin/apachectl -tSyntax OK[[email protected] ~]# /usr/local/apache2/bin/apachectl graceful


Define all requests to access the pictures as variable img, and exclude them in the access record (log). Accessing the file contents in the format specified in the IMG variable with curl after reloading will not produce an access record.

Second, access log cutting

With the server running, the access log file size will continue to grow, the log has been recorded one day will be full disk, so it is necessary to let it automatically cut, and delete the old log file

Step 1: Edit the virtual host configuration file
[[email protected] ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf CustomLog "|/usr/local/apache2/bin/rotatelogs -l logs/123test-access_log_%Y%m%d_log 86400" combined env=!img# /usr/local/apache2.4/bin/rotatelogs工具是apache自带的分割日志的工具# -l参数按当前系统时间为基准进行切割(我国为CST),否则默认UTC# %Y%m%d表示年月日,这样会每天记录一个带日期的日志文件,更方便# 86400(s)表示每天都进行切割,一天24小时等于86400秒 [[email protected] ~]# /usr/local/apache2/bin/apachectl -tSyntax OK[[email protected] ~]# /usr/local/apache2/bin/apachectl graceful

Do a few visits to see the effect:

Step 2: Periodically delete old logs
[[email protected] logs]# crontab -e     //指定计划任务,此仅为本人测试使用,供参考。11 21 * * * /usr/bin/find /usr/local/apache2/logs/ -regex ".*/123test-access_log_.*_log" -mmin +5 -exec rm -f {} \;//说明1:每天21点11分删除五分钟前日志文件//说明2:已经在httpd-vhosts.conf中设置每一分钟切割一次日志//说明3:使用-regex时有一点要注意:-regex不是匹配文件名,而是匹配完整的文件名(包括路径)。例如,当前目录下有一个文件"abar9",如果你用"ab.*9"来匹配,将查找不到任何结果,正确的方法是使用".*ab.*9"或者".*/ab.*9"来匹配。这里/logs/表示当前目录,所以后面日志格式写成“.*/123test-access_log_.*_log”
Third, set the static file failure time

When a browser accesses a picture of a Web site, it caches the static files on the local computer so that it doesn't have to be downloaded remotely the next time you visit.
But how long is the cache? If the site image is updated, then the new image should be accessed. So this involves the problem of static file cache duration, which is the "cache Expiration Time".

Step 1: Enable the Expires module
[[email protected] logs]# vim /usr/local/apache2/conf/httpd.confLoadModule expires_module modules/mod_expires.so   //去掉#[[email protected] logs]# /usr/local/apache2/bin/apachectl -M | grep -i expire   //检查模块是否加载 expires_module (shared)
Step 2: Modify the virtual host configuration file
[[email protected] logs]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf  //增加以下内容<IfModule mod_expires.c>    ExpiresActive on  //打开该功能的开关    ExpiresByType image/gif  "access plus 1 days"    ExpiresByType image/jpeg "access plus 24 hours"    ExpiresByType image/png "access plus 24 hours"    ExpiresByType text/css "now plus 2 hour"    ExpiresByType application/x-javascript "now plus 2 hours"    ExpiresByType application/javascript "now plus 2 hours"    ExpiresByType application/x-shockwave-flash "now plus 2 hours"#    // 按文件类型来设置自定义过期时间#    // acess表示从访问时间开始#    // now表示按当前时间开始#    // plus在前面的时间基础上加上#    // 1 hours/days 表示文件的生命周期#    // 例如 acess plus 1 days 表示该文件从访问的时间开始1天内有效,无需重新获取    ExpiresDefault "now plus 0 min"    #   //除上述外的文件指定默认的过期时间

Apache (httpd) configuration--log control and static element expiration time 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.