Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx optimization-log segmentation

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Overview

As the Nginx elapsed time, so did the logs. In order to easily grasp the running status of Nginx, you need to always pay attention to Nginx log files that are too large for monitoring-a catastrophe requires regular log file cutting.

Nginx itself does not have the function of log segmentation, but the automatic log cutting can be realized through the script of Nginx signal control function, and the log can be cut periodically through the planned task of Linux.

Step 1: compile and install the Nginx service

Step 1: remotely obtain the source code package on Windows and mount it to Linux

[root@localhost] # smbclient-L / / 192.168.235.1Enter SAMBA\ root's password: Sharename Type Comment- LNMP Disk [root@localhost ~] # mkdir / abc [root@localhost ~] # mount.cifs / / 192.168.235.1/LNMP / abcPassword for root@//192.168.235.1/LNMP: [root @ localhost ~] # ls / abcDiscuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz php-7.1.10.tar.bz2mysql-boost-5.7.20.tar.gz nginx-1.12.2.tar.gz php-7.1.20.tar.gz

Step 2: decompress the source package

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

Step 3: download and install the compiler package

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

Step 4: create program users and configure Nginx service-related components

[root@localhost opt] # useradd-M-s / sbin/nologin nginx// Creator user nginx And limit its non-login 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

Step 5: compile and install Nginx

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

Step 6: optimize the Nginx service startup script and establish a command soft connection

[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 0bat / turn off enhanced security function [root@localhost nginx-1.12.0] # nginx/ / input Nginx enables the service [root@localhost nginx-1.12.0] # netstat-ntap | grep 80 / / View port 80 of the service Shows that tcp 0 0 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 01. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. master * LISTEN 7520/nginx:

Step 7: systemctl manages the nginx script

[root@localhost ~] # vim / lib/systemd/system/nginx.service # # create a profile [Unit] Description=nginx # # describe After=network.target # # describe the service type [Service] Type=forking # # background running form PIDFile=/usr/local/nginx/logs/nginx.pid # # PID file location ExecStart=/usr/local/nginx/sbin/nginx # # start the service ExecReload=/usr/bin/kill-s HUP $MAINPID # # configure ExecStop=/usr/bin/kill-s QUIT $MAINPID # # according to PID overload to terminate the process PrivateTmp= true [install] WantedBy=multi-user.target [root@localhost ~] # chmod according to PID 754 / lib/systemd/system/nginx.service # # set execution permission [root@localhost ~] # systemctl stop nginx.service # # close nginx [root@localhost ~] # systemctl start nginx.service # # enable 2. Write a script to split the log [root@localhost nginx-1.12.0] # vim fengge.shemagram binample Bash account FilenamePartition fengge.shange# description information dwords $(date-d "- 1 day"+% Y%m%d") # # shows the time of the day before the system And generate a date string, such as "2019.11.11" logs_path= "/ var/log/nginx" # # the process number file that stores the path pid_path= "/ usr/local/nginx/logs/nginx.pid" # # Nginx after log segmentation [- d $logs_path] | | mkdir-p $logs_path## to determine whether there is a serious storage path. If it does not exist, create the path mv / usr/local/nginx/logs/access.log ${logs_path} / test.com-access.log-$d## to generate the access log from the original path to the created path, and name the generated log file kill-USR1 $(cat $log) # # to end the previous process number. It is used to generate a new process number find $logs_path-mtime + 30 | xargs rm-rf## looks for the file under the path 30 days ago and deletes it. Xargs is used to use 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 fengge.sh # # to grant the script execution permission [root@localhost nginx-1.12.0] # ls / var/log/nginxtest.com-access.log-20191112## to view the log partition file generated under the specified path [root@localhost nginx-1.12.0] # ls / usr/local/. Nginx/logsaccess.log error.log nginx.pid## view Nginx service daily directory It can be seen that an access.log log has been automatically generated. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report