1.8.5 Access logs do not record static files

1.8.5 Access logs do not record static files

Most of the elements of the website are static files, such as pictures, css, js, etc. These elements can be recorded without recording
Change the virtual host configuration file to the following:

 <VirtualHost *:80>  
 DocumentRoot "/data/wwwroot/111.com"  
 ServerName www.111.com  
 ServerAlias 111.com  
 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 "logs/123.com-access_log" combined env=!img  
</VirtualHost>

Reload the configuration file -t, graceful
mkdir /data/wwwroot/www.123.com/images //Create a directory and upload an image in this directory
curl -x127.0.0.1:80 -I 123.com/images /123.jpg
tail /usr/local/apache2.4/logs/123.com-access_log
first directly access the picture:

[root@Dasoncheng ~]# ls /data/wwwroot/111.com/
admin.php  index.php  luds.jpg
[root@Dasoncheng ~]# curl www.111.com/admin.php
Welcome to the page of admin
[root@Dasoncheng ~]# curl www.111.com/luds.jpg -I
HTTP/1.1 200 OK
Date: Fri, 23 Feb 2018 10:15:46 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT
ETag: "1ac51-52dc1e0711a40"
Accept-Ranges: bytes
Content-Length: 109649
Content-Type: image/jpeg

Look at the log again:

[root@Dasoncheng ~]# true > /usr/local/apache2.4/logs/111.com-access_log
[root@Dasoncheng ~]# tail -f /usr/local/apache2.4/logs/111.com-access_log
192.168.60.11 - - [23/Feb/2018:18:14:31 +0800] "GET /admin.php HTTP/1.1" 200 29 "-" "curl/7.29.0"
192.168.60.11 - - [23/Feb/2018:18:14:42 +0800] "GET /luds.jpg HTTP/1.1" 200 109649 "-" "curl/7.29.0"
##这条访问日志里面有luds.jpg记录,我们不想要这样的静态文件记录怎么办?

Edit configuration file:

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf 
<VirtualHost *:80>
#   ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com
    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_HOST} !^www.111.com$
       RewriteRule ^/(.*)$ http://www.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 "logs/111.com-access_log" combined env=!img
</VirtualHost>
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful

Visit again:

[root@Dasoncheng ~]# curl www.111.com/admin.php
Welcome to the page of admin
[root@Dasoncheng ~]# curl www.111.com/luds.jpg -I
HTTP/1.1 200 OK
Date: Fri, 23 Feb 2018 10:15:46 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT
ETag: "1ac51-52dc1e0711a40"
Accept-Ranges: bytes
Content-Length: 109649
Content-Type: image/jpeg

Look at the log again:

[root@Dasoncheng ~]# tail -f /usr/local/apache2.4/logs/111.com-access_log
192.168.60.11 - - [23/Feb/2018:18:14:31 +0800] "GET /admin.php HTTP/1.1" 200 29 "-" "curl/7.29.0"
192.168.60.11 - - [23/Feb/2018:18:14:42 +0800] "GET /luds.jpg HTTP/1.1" 200 109649 "-" "curl/7.29.0"
192.168.60.11 - - [23/Feb/2018:18:15:39 +0800] "GET /admin.php HTTP/1.1" 200 29 "-" "curl/7.29.0"
##只多了一行admin.php的日志,图片的日志没有!ok

Some loading accesses will be recorded in the log;
in order to avoid this situation (to prevent the log from taking up too much disk space and io): define some conditions and match some files! Then add it to the rules

1.8.6 Access log cutting

The log keeps recording and will fill the entire disk one day, so it is necessary to let it automatically cut and delete the old log files.
Change the virtual host configuration file to the following:

<VirtualHost *:80>  
 DocumentRoot "/data/wwwroot/www.123.com"  
 ServerName www.123.com  
 ServerAlias 123.com  
 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/123.com-access_%Y%m%d.log 86400" combined env=!img  
</VirtualHost> 

Reload configuration file -t, graceful
ls /usr/local/apache2.4/logs

Edit configuration file:

[root@Dasoncheng ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
#   ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com
    <IfModule mod_rewrite.c>
       RewriteEngine on
       RewriteCond %{HTTP_HOST} !^www.111.com$
       RewriteRule ^/(.*)$ http://www.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
</VirtualHost>
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@Dasoncheng ~]# ls /usr/local/apache2.4/logs/
111.com-access_log  abc.com-access_log  access_log  httpd.pid
111.com-error_log   abc.com-error_log   error_log
[root@Dasoncheng ~]# curl www.111.com
111.com
[root@Dasoncheng ~]# ls /usr/local/apache2.4/logs/
111.com-access_20180223.log  111.com-error_log   abc.com-error_log  error_log
111.com-access_log           abc.com-access_log  access_log         httpd.pid
[root@Dasoncheng ~]# crontab -e
0 12 1 * * /usr/bin/find /usr/local/apache2.4/logs/ -mtime +30 -name "111.com-access_20*" -exec rm -f {} \;
crontab: installing new crontab

Purpose: To prevent the disk from being filled up; to facilitate log management.
Use Apache's own cutting tool rotatelogs, -l to use local time!
24 hours = 86400s and
do another task plan crontab, delete it after more than two months

1.8.7 Configure static element expiration time

When the browser accesses the pictures of the website, it will cache the static files in the local computer, so that the next time you visit it, you don't have to download it remotely.
Increase the configuration
<IfModule mod_expires.c>
ExpiresActive on //Turn on the switch for this function
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>
need expires_module
curl test, see cache-control: max-age

Edit configuration file:

<VirtualHost *:80>
#   ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/111.com"
    ServerName www.111.com
    ServerAlias 111.com
    <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
</VirtualHost>

Test effect:

Turn on expiration time:

[root@Dasoncheng ~]# curl www.111.com/luds.jpg -I
HTTP/1.1 200 OK
Date: Fri, 23 Feb 2018 11:16:32 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT
ETag: "1ac51-52dc1e0711a40"
Accept-Ranges: bytes
Content-Length: 109649
Cache-Control: max-age=86400
Expires: Sat, 24 Feb 2018 11:16:32 GMT
Content-Type: image/jpeg

mark
Close expiration time:

[root@Dasoncheng ~]# grep -i expire /usr/local/apache2.4/conf/httpd.conf
#LoadModule expires_module modules/mod_expires.so
[root@Dasoncheng ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@Dasoncheng ~]# curl www.111.com/luds.jpg -I
HTTP/1.1 200 OK
Date: Fri, 23 Feb 2018 11:18:04 GMT
Server: Apache/2.4.27 (Unix) PHP/5.6.30
Last-Modified: Fri, 11 Mar 2016 08:40:01 GMT
ETag: "1ac51-52dc1e0711a40"
Accept-Ranges: bytes
Content-Length: 109649
Content-Type: image/jpeg

mark

ctrl + f5 Force refresh (clean the page cache)
apache log records proxy IP and real client IP http://www.lishiming.net/thread-960-1-1.html
apache only records logs of specified URI http:/ /www.lishiming.net/thread-981-1-1.html
apache log records the domain name requested by the client http://www.lishiming.net/thread-1037-1-1.html
apache log cutting problemhttp :/ /www.lishiming.net/thread-566-1-1.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325026701&siteId=291194637