httpd static file cache and log not logged

Source: Internet
Author: User
Tags browser cache custom name

HTTPD does not log access logs for the specified element

When visiting a Web page, httpd will record the picture on this page, Js/css page or video access to a class of record information, and store this information in the log, logging is very detailed, but the log size will grow very fast, Because each access request is likely to record hundreds of log messages, and these log information is useless, so we need to let httpd do not record access to some of the resulting logging information
Configured under Logging configuration items, the access to pictures and Css/js is not logged, which is recorded in the virtual host configuration file, if configured in the Master profile, and is configured under the configuration of the path of the specified log

[[email protected] local]# vim httpd/conf/extra/httpd-vhosts.conf  ? ErrorLog "logs/123.com-error_log" ?  SetEnvIf Request_URI ".*\.gif$" imges ?  SetEnvIf Request_URI ".*\.jpg$" imges ?  SetEnvIf Request_URI ".*\.png$" imges ?  SetEnvIf Request_URI ".*\.bmp$" imges ?  SetEnvIf Request_URI ".*\.swf$" imges ?  SetEnvIf Request_URI ".*\.js$" imges ?  SetEnvIf Request_URI ".*\.css$" imges ?  CustomLog "logs/123.com-access_log" combined env=!imges</VirtualHost>

Check the correctness of the configuration file

[[email protected] local]# /usr/local/httpd/bin/apachectl -tSyntax OK[[email protected] local]# /usr/local/httpd/bin/apachectl graceful

我们需要在虚拟主机的一个网页根目录添加一个图片,然后用浏览器访问这个图片,观察日志中是否记录访问这个图片的信息,在/usr/local/httpd/docs/123.com/目录下使用rz命令上传一个图片,然后访问该域名并在后面添加片名称,如www.123.com/girl.png

[[email protected] local]# vim httpd/conf/extra/httpd-vhosts.conf <VirtualHost *:80> ?  DocumentRoot "/usr/local/httpd/docs/123.com" ?  ServerName 123.com ?  ServerAlias www.123.com ? ErrorLog "logs/123.com-error_log" ?  SetEnvIf Request_URI ".*\.gif$" imges ?  SetEnvIf Request_URI ".*\.jpg$" imges ?  SetEnvIf Request_URI ".*\.png$" imges ?  SetEnvIf Request_URI ".*\.bmp$" imges ?  SetEnvIf Request_URI ".*\.swf$" imges ?  SetEnvIf Request_URI ".*\.js$" imges ?  SetEnvIf Request_URI ".*\.css$" imges ?  CustomLog "logs/123.com-access_log" combined env=!imges</VirtualHost>
Test section

Do not log the configuration is written, first let the configuration does not take effect, comment out env=!imges? (Imges here is a custom name, referring to a Word table variable configured above), graceful re-entry into force, and then observe whether access logs

Log information to access small files configuration:
Use Tail-f to dynamically display logs generated by log, refresh access to view record information more than once

[[email protected] extra]# tail -f /usr/local/httpd/logs/123.com-access_log 192.168.1.112 - - [30/Jul/2018:09:38:10 +0800] "GET /girl.png HTTP/1.1" 200 4666274 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"192.168.1.112 - - [30/Jul/2018:09:43:27 +0800] "GET /girl.png HTTP/1.1" 200 4666274 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"

Remove the comment add env=!imges configuration and then request this image again, the log information is no longer added, the log no longer records the small file access information
----------------Partial Configuration---------------------

# authuserfile/usr/local/httpd/conf/htpasswd# require valid-user# </Directory>?  Errorlog "Logs/123.com-error_log"?  Setenvif Request_uri ". *\.gif$" Imges?  Setenvif Request_uri ". *\.jpg$" Imges?  Setenvif Request_uri ". *\.png$" Imges?  Setenvif Request_uri ". *\.bmp$" Imges?  Setenvif Request_uri ". *\.swf$" Imges?  Setenvif Request_uri ". *\.js$" Imges?  Setenvif Request_uri ". *\.css$" Imges? Customlog "Logs/123.com-access_log" combined env=!imges</virtualhost>[[email protected] extra]#/usr/ Local/httpd/bin/apachectl graceful[[email protected] extra]# tail-f/usr/local/httpd/logs/123.com-access_log 192.168.1.112--[30/jul/2018:09:38:10 +0800] "get/girl.png http/1.1" "4666274"-"" mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/62.0.3202.94 safari/537.36 "192.168.1.112--[30/jul/2018:09:43:27 +0800] "get/girl.png http/1.1" 4666274 "-" "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/62.0.3202.94 safari/537.36 " 
httpd Access Log Cutting

HTTPD cut the daily log, the cut log can be cut with a custom length of time, and the cut can refer to the timestamp as a name

[[email protected] extra]# tail -n 15 httpd-vhosts.conf # require valid-user# </Directory> ?  ErrorLog "logs/123.com-error_log" ?  SetEnvIf Request_URI ".*\.gif$" imges ?  SetEnvIf Request_URI ".*\.jpg$" imges ?  SetEnvIf Request_URI ".*\.png$" imges ?  SetEnvIf Request_URI ".*\.bmp$" imges ?  SetEnvIf Request_URI ".*\.swf$" imges ?  SetEnvIf Request_URI ".*\.js$" imges ?  SetEnvIf Request_URI ".*\.css$" imges  ?  CustomLog "|/usr/local/httpd/bin/rotatelogs -l logs/123.com-access_%Y%mYdlog 86400" combined env=!imges</VirtualHost>[[email protected] extra]# /usr/local/httpd/bin/apachectl -tSyntax OK

Configuration Description:
/usr/local/httpd/bin/rotatelogs? This is Apache's own cutting tool, you can use a timestamp to name the log file, log files can be cut in time slots
Cut logs are saved in the log directory by date, but these logs are not automatically deleted, they need to be written to a scheduled task or executed periodically in a script, and the main commands for deleting a specified time log are as follows:
find/usr/local/httpd/logs/?-mtime +15 f-name *.log-exec rm-rf {}? \;?

Browser cache static element file

When a browser accesses a Web site, a static file such as GIF, JPG, or JavaScript is downloaded, and the files are downloaded from the server each time the user accesses them, and the files are downloaded again each time they are accessed by the server side. There is no need to pull these files from the server for a short period of time, and configure the server to keep the files in the user's browser for a period of time, and then download them again
Configure the/usr/local/httpd/conf/extra/httpd-vhosts.conf virtual host configuration file to add the following module configuration

[[email protected] extra]# vim 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 hours" ?  ExpiresByType application/x-javascript "now plus 2 hours" ?  ExpiresByType application/javascript "now plus 2 hours" ?  ExpiresByType application/x-shockwave-flash "now plus 2 hours" ?  ExpiresDefault "now puls 0 min"</IfModule>

Expiresactuve? open browser cache static files feature
Expiresbytype image/gif\jpeg\png\css? Let the browser cache these types of static files
Expiresbytype application/x-javascript? caching JavaScript static web files
ExpiresDefault? Files of other elements are not cached

Open the Expires module in httpd.conf

[[email protected] extra]# vim /usr/local/httpd/conf/httpd.conf---------------------------------------------------LoadModule expires_module modules/mod_expires.so

Reload the httpd configuration

[[email protected] extra]# /usr/local/httpd/bin/apachectl graceful[[email protected] extra]# /usr/local/httpd/bin/apachectl -M |grep expires expires_module (shared)

The Access status code for the viewing client in the first access Web in the browser is 200, and the status code for the second access is a 304,304 status code indicating that the file has been cached in the user's browser
Server-side view the state of access, where Cache-control represents the cache for 86,400 seconds, that is, the picture in the user's browser cache data will be retained for a day, the day to visit the site will not go to download the picture file from the server

[[email protected] 123.com]# curl -x127.0.0.1:80 123.com/girl.png -IHTTP/1.1 200 OKDate: Mon, 30 Jul 2018 05:24:44 GMTServer: Apache/2.4.33 (Unix) PHP/5.6.37Last-Modified: Mon, 30 Jul 2018 05:24:44 GMTETag: W/"4733a2-57247be175d40"Accept-Ranges: bytesContent-Length: 4666274Cache-Control: max-age=86400Expires: Tue, 31 Jul 2018 05:24:44 GMTContent-Type: image/png

httpd static file cache and log not logged

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.