2018-3-5 Linux Learning Notes

Source: Internet
Author: User
Tags curl apache log

11.22 access logs do not log static files
  • background: Because most of the elements of the site are static files, tablets, CSS, JS, and so on, these elements of the log tracking role is not small, in order to save the log space, you can not record these files.
  • Setting the access log does not log static file methods:
  • Vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
  • Change the virtual host configuration file to the following:
    <virtualhost :80>
    DocumentRoot "/data/wwwroot/123.com"
    ServerName 123.com
    Serveralias www.example.com 2111.com
    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 #博客格式原因, not shown here, correct please see
    Customlog "Logs/123.com-access_log" combined env=!img
    </VirtualHost>
  • The above setenvif means defining an environment variable to identify the records that end with Gif,jpg,png in the request URI, and then exclude them through the ENV=!IMG statement. In this way, the log file does not record these static files that occupy large storage space and do not help the analytic log.

  • Reload the configuration file:
    /usr/local/apache2.4/bin/apachectl-t
    /usr/local/apache2.4/bin/apachectl Graceful
  • Test results:
  • Upload Real file test:
  • Mkdir/data/wwwroot/123.com/images//Create a directory and upload a picture in this directory baidu.png
  • Curl-x127.0.0.1:80-i 123.com/images/baidu.png
  • Tail/usr/local/apache2.4/logs/123.com-access_log
11.23 Access Log Cutting
  • background : The site log has been recorded one day will be full of disk, so it is necessary to let it automatically cut, and periodically delete or transfer old log files (can do a task to make the system regularly automatically delete/transfer old log files), So that the disk is not fully occupied by the log file (the purpose ).
  • To set access log cutting methods:
  • Vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
    Change the virtual host configuration file to the following:
    <virtualhost :80>
    DocumentRoot "/data/wwwroot/23.com"
    ServerName 123.com
    Serveralias www.example.com 2111.com
    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# blog format reason, not shown here, correct please see
    Customlog "|/usr/local/apache2.4/bin/rotatelogs-l logs/123.com-access_%y%m%d.log 86400" combined env=!img
    </VirtualHost>
  • Above the Customlog line,rotatelogs is Apache's own log cutting tool; The- L parameter is to have it cut at the current system time (for example, CST in China, UTC in the United States, and UTC as the reference); The %y%m%d variable is the log file name of the cut with the current date in order to differentiate; 86400 is the number of seconds in aday (60), which means cutting at a 1-day frequency.
  • Reload the configuration file
    /usr/local/apache2.4/bin/apachectl-t
    /usr/local/apache2.4/bin/apachectl Graceful
  • To view the generated log files:
    curl-x127.0.0.1:80 123.com/xxx.php #访问以让系统生成日志文件
    Ls/usr/local/apache2.4/logs

Precautions:
In the actual working situation, it is not complete, there is a more critical step, is the need to set up a task plan for the system to automatically delete or transfer the old log files regularly, otherwise, although the log files have been cut, but all remain on the disk, After a long time, it will fill the disk. So don't forget to do a more important step in the Mission plan!!!

11.24 static element Expiration time
    • background : When a browser accesses a picture of a website, it caches the static files on the local computer so that it does not have to be downloaded remotely the next time you visit. The advantage of this is that you can save bandwidth and improve access speed. But the downside is that these static file caches are always on the local computer, Does not automatically purge periodically. Therefore, there are sometimes requirements for setting the static element expiration time.
    • To set the static element expiration Time method:
    • Vim/usr/local/apache2.4/conf/extra/httpd-vhosts.conf
      Add Configuration:
      <ifmodule mod_expires.c>
      Expiresactive on//switch to turn on the function
      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>
    • Need to edit the master profile to load the Expires_module module
    • Test with Curl
      Curl-x127.0.0.1:80-i 123.com/images/baidu.png
    • Test results (Cache-control:max-age,expires)
    • Knowledge Points:
      In the browser, press CTRL+F5 to force the refresh and empty the local cache.

Extended Learning:
Apache logging proxy IP and real client IP http://ask.apelearn.com/question/960
Apache records only the log of the specified URI http://ask.apelearn.com/question/981
Apache Logging Client-requested domain name http://ask.apelearn.com/question/1037
Apache Log Cutting Problem http://ask.apelearn.com/question/566

2018-3-5 Linux Learning Notes

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.