11.22 access logs do not log static files
Edit the virtual host profile "httpd-vhosts.conf":
[Email protected] ~]# vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
......
<virtualhost :80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
Serveralias www.example.com
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{http_host}!^111.com$
Rewriterule ^/(.) $ http://111.com/$1 [r=301,l]
</IfModule>
Errorlog "Logs/111.com-error_log"
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
#以上为定义变量: Define all requests for pictures as variable img
Customlog "Logs/111.com-access_log" combined env=!img
# "Env=!img" represents a non-IMG variable. The meaning of our command is that the request log about the variable img is not logged.
</VirtualHost>
Description: Defines all requests to access the picture as variable img, which is excluded from 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.
Extended:
Apache Logging Client-requested domain name
Under normal circumstances, there is no need to record this, after all, we mostly based on the virtual host to set the corresponding access log, but there are individual situations, such as ServerName *.abc.com such a pan-analytic form, it is necessary to record the user requested domain name in the end. And the Apache Logformat has exactly one value that satisfies this requirement. That is,%V here is the upper case V, the lowercase v record is we set in the virtual host servername, this really does not need to record.
Apache logs only the specified URI
Requirement: www.aaa.com/aaa/a similar request to ... The log is logged only for such requests.
Method: In the httpd.conf or related virtual host configuration file, add:
Setenvif RequestURI "^/aaa/.*" aaa-request
Customlog "|/usr/local/apache/bin/rotatelogs-l/usr/local/apache/logs/aaa-access%Y%m%d.log 86400" combined env =aaa-request
The principle is the same as a log (http://www.lishiming.net/thread-561-1-1.html) that does not record static access such as pictures.
Agent IP and real client IP are recorded in Apache log
By default, the log log format is:
Logformat "%h%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined
, where%h is the IP of the record visitor, if there is a layer of proxy in the front of the web, then this%h is actually the IP of the proxy machine, this is not what we want. What we need is to record the real IP of the client, "%{x-forwarded-for}i" this field will record the client's real IP, so the log log should be formatted as:
Logformat "%h%{x-forwarded-for}i%l%u%t \"%r\ "%>s%b \"%{referer}i\ "\"%{user-agent}i\ "" combined
11.23 Access Log Cutting
Configuration
To edit a virtual host configuration file:
[[email protected] ~]# vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
...
<virtualhost : 80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
Serveralias www.example.com
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{http_host}!^111.com$
Rewriterule ^/(. ) $ http://111.com/$1 [r=301,l]
</ifmodule>
errorlog "Logs/111.com-error_log"
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
Customlog" |/usr/local/apache2.4/bin/rotatelogs-l logs/111. Com-access %y%m%d.log 86400 "combined env=!img
#使用rotatelogs工具, with system time as a baseline, cut logs once a day, and the log name is in the format" 111.com-access_ %y%m%d.log ".
#每小时切割一次日志,语法: #CustomLog "|/usr/local/apache2.4/bin/rotatelogs logs/access_%Y%m%d%H.log 3600" combined
Description: Rotatelogs is the Apache cutting log tool;-L indicates the use of the system (Cst=chinese time), if the plus-l option, will be the default (UTC) time, the log file name "%y%m%d" means date The interval is set to one day (1day=24h=1440min=86400s).
Detection
Overloaded configuration file:
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl Graceful
Access, detect logs:
[Email protected] ~]# curl-x192.168.8.131:80 111.com/http.jpg-i
[Email protected] 111.com]# ls/usr/local/apache2.4/logs/
111.com-access_20170801.log 111.com-error_log Abc.com-error_log Error_log
111.com-access_log Abc.com-access_log Access_log Httpd.pid
Description: Use with scheduled tasks to periodically clean up log files to avoid the accumulation of log files resulting in insufficient disk space.
Extended:
Rotatelogs command
Syntax: Rotatelogs [-l] logfile [rotationtime [offset]] rotatelogs [Filesizem] LogFile
Option:-L: Use local time instead of GMT time as the time base. Note: using-l in an environment that alters the GMT offset, such as daylight saving time, can cause unpredictable results. So be sure to add-l otherwise, the log time and the actual time is 8 hours difference. Rotationtime: The interval of time in seconds that the log files are scrolled. Offset: The number of minutes from the time difference from UTC. If omitted, the 0″ is assumed and UTC time is used. For example, to specify local time for a region with a UTC difference of "-5 hours", this parameter should be " -300″." Filesizem: Specifies to scroll with the Filesizem file size instead of by time or slack.
Log Cutting Method 2:
Use the Cronolog command:
One: Create a new log every day
Customlog "|bin/cronolog Logs/access_%y%m%d.log" combined
Two: Create a new log per hour
Customlog "|bin/cronolog Logs/access_%y%m%d%h.log" combined
11.24 static element Expiration time
When the browser visits the Web site will be static files (slice files, CSS, JS files, etc.) cached to the local computer, so that the next time the visit does not have to go remote download, you can customize the time to clear the partial cache, that is, set the static element expiration time. This setting can be used to optimize websites, especially in the corporate intranet.
Configuration
To edit a virtual host configuration file:
[Email protected] ~]# vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
......
<virtualhost :80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
Serveralias www.example.com
<ifmodule mod_rewrite.c>
Rewriteengine on
Rewritecond%{http_host}!^111.com$
Rewriterule ^/(.) $ http://111.com/$1 [r=301,l]
</IfModule>
<ifmodule mod_expires.c>
Expiresactive on
#打开该功能的开关
Expiresbytype image/gif "Access plus 1 days"
Expiresbytype image/jpeg "Access plus hours"
Expiresbytype image/png "Access plus 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"
ExpiresDefault "now plus 0 min"
#以上是定义不同类型的文件缓存的时间
</IfModule>
Errorlog "Logs/111.com-error_log"
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 RequestURI ". *.css$" img
Customlog "|usr/local/apache2.4/bin/rotatelogs-l logs/111.com-access%y%m%d.log 86400 "Combined env=!img
#使用rotatelogs工具, the log is cut once a day based on the system time, and the log name is formatted as "111.com-access_%y%m%d.log".
Description: Add the contents of the MOD_EXPIRES.C module in the configuration file and the rest is unchanged.
Detection
To detect syntax errors:
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK
Detects if the Apache configuration file is open expire module:
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl-m |grep expires
Note: The Expires module is not detected here, so you need to edit the Apache configuration file to load the Expires module.
Configure Apache to load the Expires module
To edit an Apache configuration file:
[Email protected] 111.com]# vim/usr/local/apache2.4/conf/httpd.conf
LoadModule Expires_module modules/mod_expires.so
To load a configuration file:
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl-t
Syntax OK
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl Graceful
Check that the module is turned on:
[Email protected] 111.com]#/usr/local/apache2.4/bin/apachectl-m |grep expires
Expires_module (Shared)
Description: Enter "/" to find the Expires module and open the command line (minus #) to load the module.
Re-Test
[Email protected] 111.com]# curl-x192.168.8.131:80 111.com/baidu.png-i
http/1.1 OK
Date:tue, 10:17:36 GMT
server:apache/2.4.27 (Unix) php/5.6.30
Last-modified:tue, 10:13:45 GMT
ETag: "e7a-555ae670b0840"
Accept-ranges:bytes
content-length:3706
cache-control:max-age=86400
expires:wed, 10:17:36 GMT
Content-type:image/png
Description: Current Time "date:tue, 10:17:36 GMT", Cache duration "cache-control:max-age=86400s", expiry Time "expires:wed, 2017 10:17:36 GMT ", that is, the PNG format Picture cache duration is 1 days.
Apache Optimization--Log management