11.22 Access logs do not record static files
Edit the virtual host configuration file "httpd-vhosts.conf":
[root@1 ~]# 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
#以上为定义变量:将所有关于图片的请求定义为变量img
CustomLog "logs/111.com-access_log" combined env=!img
#“env=!img”表示非img变量。本行命令的含义是:不记录关于变量img的请求日志。
</VirtualHost>
Description: Define all requests to access images as variable img, and exclude them in the access record (log). After reloading, using curl to access the file content of the format specified in the img variable will not generate an access record.
Extension:
apache log records the domain name requested by the client
Under normal circumstances, there is no need to record this item at all. After all, most of us set the corresponding access log according to the virtual host, but there are also individual cases, such as the form of general analysis such as ServerName *.abc.com, so it is necessary to record it. What is the domain name requested by the user? And there is exactly one value in apache's LogFormat to meet this requirement. That is, %V is an uppercase V here, and a lowercase v records the ServerName we set in the virtual host, which is indeed unnecessary to record.
apache only logs the specified URI
Requirement: Log requests like www.aaa.com/aaa/... .
Method: Add in httpd.conf or related virtual host configuration file:
SetEnvIf Request_URI "^/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 the log ( http://www.lishiming.net/thread-561-1-1.html ) that does not record static access such as pictures.
Proxy 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 recorded visitor. If there is a layer of proxy at the front end of the web, then this %h is actually the IP of the proxy machine, which is not what we want. What we need is to record the real IP of the client. The "%{X-FORWARDED-FOR}i" field will record the real IP of the client, so the format of the log log should be changed to:
LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
11.23 Access log cutting
configure
Edit the virtual host configuration file:
[root@1 ~]# 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工具,以系统时间为基准,每天切割一次日志,并且日志名字格式为“111.com-access_%Y%m%d.log”。
#每小时切割一次日志,语法:
#CustomLog "|/usr/local/apache2.4/bin/rotatelogs logs/access_%Y%m%d%H.log 3600" combined
Note: rotatelogs is a tool for Apache to cut logs; -l means to use the system (CST=Chinese time) time, if the -l option is added, the default (UTC) time will prevail; the log file name is "%Y%m%d" ” indicates the year, month, and day; the time interval is set to one day (1day=24h=1440min=86400s).
detect
- Reload configuration file:
[root@1 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@1 111.com]# /usr/local/apache2.4/bin/apachectl graceful
- Access and detection logs:
[root@1 ~]# curl -x192.168.8.131:80 111.com/http.jpg -I
[root@1 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
Note: Use with scheduled tasks to regularly clean up log files to avoid insufficient disk space due to accumulation of log files.
Extension:
rotatelogsCommand
语法: rotatelogs [ -l ] logfile [ rotationtime [ offset ]] rotatelogs [ filesizeM ] logfile
Options: -l: Use local time instead of GMT time as the time base. Note: Using -l in an environment that changes the GMT offset (such as daylight saving time) can lead to unpredictable results. So be sure to add -l, otherwise the log time and the actual time will be 8 hours different. rotationtime: The interval in seconds at which the log file is rolled. offset: Offset in minutes relative to UTC. If omitted, "0" is assumed and UTC time is used. For example, to specify the local time in a region where the UTC time difference is "-5 hours", this parameter should be "-300". filesizeM: Specifies to scroll by filesizeM file size, not by time or time difference.
Log cutting method 2:
Use the cronolog command:
一:每一天建立一个新日志
CustomLog "|bin/cronolog logs/access_%Y%m%d.log" combined
二:每小时建立一个新日志
CustomLog "|bin/cronolog logs/access_%Y%m%d%h.log" combined
11.24 Static Element Expiration Time
When the browser accesses a website, it will cache static files (such as image files, css, js files, etc.) to the local computer, so that the next time you visit, you don't have to download it remotely. You can customize the time to clear this part of the cache, that is Set the expiration time of static elements. This setting can be used to optimize websites, especially company intranets.
configure
Edit the virtual host configuration file:
[root@1 ~]# 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 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"
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 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工具,以系统时间为基准,每天切割一次日志,并且日志名字格式为“111.com-access_%Y%m%d.log”。
Description: Add the mod_expires.c module content to the configuration file, and the rest remain unchanged.
detect
检测语法错误:
[root@1 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
检测Apache配置文件是否开启expire模块:
[root@1 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 and load the expires module.
Configure Apache, load expires module
Edit the Apache configuration file:
[root@1 111.com]# vim /usr/local/apache2.4/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so
加载配置文件:
[root@1 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@1 111.com]# /usr/local/apache2.4/bin/apachectl graceful
检查模块是否开启:
[root@1 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 to load the module (just remove the #).
Check again
[root@1 111.com]# curl -x192.168.8.131:80 111.com/baidu.png -I
HTTP/1.1 200 OK
Date: Tue, 01 Aug 2017 10:17:36 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Tue, 01 Aug 2017 10:13:45 GMT
ETag: "e7a-555ae670b0840"
Accept-Ranges: bytes
Content-Length: 3706
Cache-Control: max-age=86400
Expires: Wed, 02 Aug 2017 10:17:36 GMT
Content-Type: image/png
Description: Current time "Date: Tue, 01 Aug 2017 10:17:36 GMT", cache duration "Cache-Control: max-age=86400s", expiration time "Expires: Wed, 02 Aug 2017 10:17:36 GMT" ”, that is, the PNG format image is cached for 1 day.