千家信息网

Nginx优化之日志分割

发表于:2025-02-13 作者:千家信息网编辑
千家信息网最后更新 2025年02月13日,Nginx优化之日志分割安装配置Nginx挂载远程源码包到本地mount.cifs //192.168.100.10/LNMP-C7 /mnt //挂载到/mnt目录下解压源码包到/op
千家信息网最后更新 2025年02月13日Nginx优化之日志分割

Nginx优化之日志分割

安装配置Nginx

挂载远程源码包到本地

mount.cifs //192.168.100.10/LNMP-C7 /mnt        //挂载到/mnt目录下

解压源码包到/opt目录下

[root@localhost ~]# cd /abc                                                       //切换到挂载点目录[root@localhost abc]# lsDiscuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gzmysql-boost-5.7.20.tar.gz  php-7.1.10.tar.gz[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt        //解压Nginx源码包到/opt下[root@localhost abc]# cd /opt/                                                  //切换到解压的目录下[root@localhost opt]# lsnginx-1.12.2  rh

安装编译需要的环境组件包

[root@localhost opt]# yum -y install \gcc \                                              //c语言gcc-c++ \                                      //c++语言pcre-devel \                                  //pcre语言工具zlib-devel                                     //数据压缩用的函式库

创建程序名为nginx的用户并编译Nginx

[root@localhost opt]# useradd -M -s /sbin/nologin nginx     //创建程序用户,限定其[root@localhost opt]# cd nginx-1.12.2/                                //切换到nginx目录下[root@localhost nginx-1.12.2]# ./configure \                        //配置nginx> --prefix=/usr/local/nginx \                                                  //安装路径> --user=nginx \                                                                   //用户名> --group=nginx \                                                                 //用户组> --with-http_stub_status_module                                       //访问状态统计模块

编译和安装

[root@localhost nginx-1.12.0]# make && make install                               //编译及安装

制作Nginx管理脚本,便于管理使用

[root@localhost nginx]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/                                                                                         //创建软连接                                                                 [root@nginx nginx-1.12.2]# vim /etc/init.d/nginx             //编辑启动脚本#!/bin/bash# chkconfig: - 99 20# description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx"PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" in  start)    $PROG    ;;  stop)    kill -s QUIT $(cat $PIDF)    ;;    restart)    $0 stop    $0 start    ;;    reload)    kill -s HUP $(cat $PIDF)    ;;    *)            echo "Usage: $0 {start|stop|restart|reload}"        exit 1  esac            exit 0[root@nginx nginx-1.12.2]# chmod +x /etc/init.d/nginx                  //给脚本执行权限[root@nginx nginx-1.12.2]# chkconfig --add nginx                        //添加到service管理器中[root@nginx nginx-1.12.2]# yum install elinks -y                           //[root@nginx nginx-1.12.2]# service nginx start                             //启动Nginx服务[root@nginx nginx-1.12.2]# netstat -ntap | grep 80tcp        0      0 0.0.0.0:80        0.0.0.0:*          LISTEN      42028/nginx: master [root@nginx nginx-1.12.2]# systemctl stop firewalld.service                   //关闭防火墙[root@nginx nginx-1.12.2]# setenforce 0                                          //关闭增强型安全功能[root@nginx nginx-1.12.2]# elinks http://192.168.131.133/                                                                                                           

编写脚本进行日志分割

[root@localhost ~]# vim fenge.sh            //编写脚本文件#!/bin/bash#Filename:fenge.shd=$(date -d "-1 day" "+%Y%m%d")                //显示一天前的时间logs_path="/var/log/nginx"                              //分割日志的保存路径pid_path="/usr/local/nginx/logs/nginx.pid"      //pid的路径[ -d $logs_path ] || mkdir -p $logs_path          //没有目录则创建目录mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d//原有日志文件生成到新路径下kill -USR1 $(cat $pid_path)                            //结束重新生成新的pid文件find $logs_path -mtime +30 | xargs rm -rf      //删除30天前的日志文件[root@localhost ~]# chmod +x fenge.sh        //给执行权限[root@localhost ~]# ./fenge.sh                      //执行脚本文件
0