2018-3-14 Linux Learning Notes

Source: Internet
Author: User
Tags local time

12.10 Nginx Access Log
    • The format of the Nginx access log is defined in the master configuration file.
    • vim/usr/local/nginx/conf/nginx.conf//Search Log_format
    • Log field Meaning:
      $remote _ADDR Client IP (public IP)
      $http IP of the _x_forwarded_for proxy server
      $time _local Server local time
      $host Access host name (domain name)
      $request URL address for _uri access
      $status Status Code
      $http _referer Referer
      $http _user_agent User_agent

    • In addition to defining the log format in the Master profile nginx.conf, it is also required in the virtual host configuration file
      Vi/usr/local/nginx/conf/vhost/test.com.conf
    • Add a row to configure where the access logs are stored
      Access_log/tmp/1.log Nginx_log;
    • The nginx_log here is the name of the log format defined in nginx.conf
    • /usr/local/nginx/sbin/nginx-t
    • /usr/local/nginx/sbin/nginx-s Reload
    • Access log test results:
      Curl-x127.0.0.1:80 Test.com-i
      Curl-x127.0.0.1:80 Test2.com-i
      Cat/tmp/1.log
12.11 Nginx Log Cutting
    • As stated earlier, in order to prevent the log from taking up too much storage space, we need to cut the logs and clean them regularly. Since Nginx does not have a cutting tool, learn to use shell scripting to implement log cutting.
    • Custom shell Scripts
      Vim/usr/local/sbin/nginx_log_rotate.sh #写入如下内容
      #! /bin/bash
      #假设nginx的日志存放路径为/tmp/
      D= ' date-d '-1 day "+%y%m%d"
      Logdir= "/tmp/"
      Nginx_pid= "/usr/local/nginx/logs/nginx.pid"
      CD $logdir
      For log in ' ls *.log '
      Do
      MV $log $log-$d
      Done
      /bin/kill-hup ' Cat $nginx _pid '

    • execute the log cut script :
      Sh-x/usr/local/sbin/nginx_log_rotate.sh #加-X can see the script execution process
    • Set up a log cutting task schedule
      Crontab-e
    • Add the following line:
      0 0 * * */bin/bash/usr/local/sbin/nginx_log_rotate.sh

    • example of periodic cleanup log files (delete log files for more than 30 days):
      find/tmp/-name *.log-*-tpye f-mtime +30 | Xargs RM
12.12 static files do not log logs and expire time
    • To set how static files do not log and expire time:
    • Edit a virtual host configuration file
      Vim/usr/local/nginx/conf/vhost/test.com.conf
    • Configured as follows
      Location ~. \. (gif|jpg|jpeg|png|bmp|swf) $
      {
      Expires 7d;
      Access_log off;
      }
      Location ~.
      \. (JS|CSS) $
      {
      Expires 12h;
      Access_log off;
      }
    • /usr/local/nginx/sbin/nginx-t
    • /usr/local/nginx/sbin/nginx-s Reload
    • Test results:
      Cd/data/wwwroot/test.com
      Vim 1.gif
      Vim 2.css
      Curl-x127.0.0.1:80 Test.com/1.gif
      Curl-x127.0.0.1:80 Test.com/2.css
      Curl-x127.0.0.1:80 test.com/index.html
      Curl-x127.0.0.1:80 TEST.COM/2.CSSASDFA
      Curl-x127.0.0.1:80 Test.com/1.gif-i
      Cat/tmp/1.log

2018-3-14 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.