Nginx optimization log segmentation

Log segmentation

1. Too many log files in the enterprise server cause the following problems:
1. It is inconvenient for development and operation and maintenance to query too large log files, especially when overtime workers are tired;
2. Long time ago, log files were almost worthless, but manual cleaning was too cumbersome.
At this time, a solution is needed to automatically split the logs. The divided logs are not only "clean", but also convenient for regular log cleaning.

Experiment

2. Compile and install Nginx service
1. Obtain the source package on Windows remotely and mount it on Linux

[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password: 
Sharename       Type      Comment
---------       ----      -------
LNMP            Disk  

[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:  
[root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz  nginx-1.12.2.tar.gz  php-7.1.20.tar.gz

2. Decompress the package

[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0  rh

3. Install and compile component package

[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \             //C language
> gcc-c++ \         //c++ language
> pcre-devel \      //pcre language tools
> zlib-devel        //Compress function library

4. Create program users and configure related components of Nginx service

[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//Create program user nginx and restrict it to not log in terminal
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \            
//Configure nginx
> --prefix=//usr/local/nginx \      
//Specify installation path                        
> --user=nginx \
//Specify user name
> --group=nginx \
//Specify the group to which the user belongs
> --with-http_stub_status_module
//Installation status statistics module

5. Compilation and installation

[root@localhost nginx-1.12.0]# make && make install

6. Optimize the startup script of Nginx service and establish the command soft link

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
//Create nginx service command soft link to system command
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service 
//Turn off firewall
[root@localhost nginx-1.12.0]# setenforce 0
//Turn off enhanced security
[root@localhost nginx-1.12.0]# nginx 
//Enter nginx to start the service
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80 / / check port 80 of the service, and it is shown that it is enabled
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7520/nginx: master  

7. System CTL management nginx script

[root@localhost ~]# vim /lib/systemd/system/nginx.service      ##create profile

[Unit]
Description=nginx                                            ##describe
After=network.target                                        ##Describe service type
[Service]
Type=forking                                                    ##Background operation form
PIDFile=/usr/local/nginx/logs/nginx.pid            ##PID file location
ExecStart=/usr/local/nginx/sbin/nginx              ##Startup service
ExecReload=/usr/bin/kill -s HUP $MAINPID    ##Overload configuration according to PID
ExecStop=/usr/bin/kill -s QUIT $MAINPID       ##Terminate process according to PID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service     ##Set execution permission
[root@localhost ~]# systemctl stop nginx.service       ##Close nginx 
[root@localhost ~]# systemctl start nginx.service       ##open

8. Write a log splitting script

[root@localhost nginx-1.12.0]# vim fenge.sh

#!/bin/bash
#Filename:fengge.sh                  ##Descriptive information

d=$(date -d "-1 day" "+%Y%m%d" )           ##Display the time one day before the system and generate a date string, such as "November 11, 2019"
logs_path="/var/log/nginx"                          ##Storage path after log segmentation
pid_path="/usr/local/nginx/logs/nginx.pid"       ##The process number file of Nginx
[ -d $logs_path ] || mkdir -p $logs_path         ##Determine whether there is a log split storage path. If not, create the path
mv      /usr/local/nginx/logs/access.log     ${logs_path}/test.com-access.log-$d
##Move out the access log under the original path and generate it to the created path, and generate a log file named after the date
kill   -USR1   $(cat $pid_path)                       ##End the previous process number to generate a new process number
find  $logs_path  -mtime  +30  |  xargs  rm  -rf      ##Find and delete the file 30 days ago under the path. xargs is used to take the processing result of the previous command as the parameter of the command after the pipe symbol
[root@localhost nginx-1.12.0]# chmod  +x  fenge.sh              ##Grant script execution permission
[root@localhost nginx-1.12.0]# ./ fenge.sh
[root@localhost nginx-1.12.0]#  ls  /var/log/nginx
test.com-access.log-20191112                    ##View the log partition file generated under the specified path
[root@localhost nginx-1.12.0]# ls  /usr/local/nginx/logs
access.log  error.log  nginx.pid             ##Check the Nginx log directory, and another access.log is generated automatically

This method greatly strengthens the enterprise server log management, and improves the efficiency!!

Keywords: Linux Nginx PHP vim Windows

Added by Hard Styler on Thu, 14 Nov 2019 22:22:12 +0200