千家信息网

Nginx之隐藏版本号,优化缓存,日志分割

发表于:2024-10-27 作者:千家信息网编辑
千家信息网最后更新 2024年10月27日,nginx之隐藏版本号配置nginx[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安装环境包[root@
千家信息网最后更新 2024年10月27日Nginx之隐藏版本号,优化缓存,日志分割

nginx之隐藏版本号

配置nginx

[root@localhost ~]# yum install pcre-devel zlib-devel gcc gcc-c++ -y ##安装环境包[root@localhost ~]# useradd -M -s /sbin/nologin nginx  ##创建程序性用户[root@localhost ~]# mkdir /chen  ##创建挂载点[root@localhost ~]# mount.cifs //192.168.100.23/LNMP /chen  ##挂载Password for root@//192.168.100.23/LNMP:  [root@localhost chen]# tar zxvf nginx-1.12.2.tar.gz -C /opt/  ##解压[root@localhost chen]# cd /opt/[root@localhost opt]# lsnginx-1.12.2  rh[root@localhost opt]# cd nginx-1.12.2/[root@localhost nginx-1.12.2]# lsauto     CHANGES.ru  configure  html     man     srcCHANGES  conf        contrib    LICENSE  README./configure \  ##安装nginx组件--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module[root@localhost nginx-1.12.2]# make && make install ##编译[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ##做软链接让系统能识别nginx的所有人命令[root@localhost nginx-1.12.2]# nginx -t  ##检查语法错误nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

写nginx脚本放在系统启动脚本中方便service管理器管理

[root@localhost nginx-1.12.2]# cd /etc/init.d/ ##到系统启动脚本[root@localhost init.d]# vim nginx   ##写一个nginx脚本#!/bin/bash#chkconfig: - 99 20  #注释信息#description: Nginx Service Control ScriptPROG="/usr/local/nginx/sbin/nginx"  #这个变量,指向我的命令文件PIDF="/usr/local/nginx/logs/nginx.pid"  #这个变量,指向nginx的进程号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 1esacexit 0[root@localhost init.d]# chmod +x nginx  ##给Nginx提升权限[root@localhost init.d]# chkconfig --add nginx  ##添加nginx[root@localhost init.d]# service nginx start [root@localhost init.d]# netstat -ntap | grep nginx tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17544/nginx: master [root@localhost init.d]# systemctl stop firewalld.service[root@localhost init.d]# setenforce 0

检查现在我们nginx的版本号

[root@localhost ~]# curl -I http://192.168.136.163/HTTP/1.1 200 OKServer: nginx/1.12.2Date: Tue, 05 Nov 2019 07:30:14 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 05 Nov 2019 07:13:26 GMTConnection: keep-aliveETag: "5dc12116-264"Accept-Ranges: bytes

关闭版本号再检查一下

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confhttp {  ##在http级别下添加    include       mime.types;    default_type  application/octet-stream;    server_tokens off;  ##关闭版本号[root@localhost init.d]# service nginx stop  ##关闭服务[root@localhost init.d]# service nginx start  ##开启服务[root@localhost init.d]# curl -I http://192.168.136.163/  ##查看Nginx信息HTTP/1.1 200 OK      Server: nginx            ##版本号被隐藏了Date: Tue, 12 Nov 2019 14:22:00 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes

伪造版本号

[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.confhttp {        include       mime.types;        default_type  application/octet-stream;        server_tokens on;   ##开启版本号修改Nginx源码包文件[root@localhost init.d]# cd /opt/nginx-1.12.2/src/core/  ##切换到src源码包目录[root@localhost core]# vim nginx.h  ##修改文件#define NGINX_VERSION      "1.1.2"  ##此处版本号伪造成1.1.2重新编译安装[root@localhost core]# cd /opt/nginx-1.12.2/    ##到Nginx下[root@localhost nginx-1.12.2]# ./configure \     ##重新安装组件> --prefix=/usr/local/nginx \> --user=nginx \> --group=nginx \> --with-http_stub_status_module[root@localhost nginx-1.12.0]# make     ##重新编译...[root@localhost nginx-1.12.0]# make install   ##重新安装...重启Nginx服务,查看版本信息[root@localhost nginx-1.12.2]# service nginx stop  ##关闭[root@localhost nginx-1.12.2]# service nginx start  ##开启[root@localhost nginx-1.12.2]# curl -I http://192.168.136.163/   ##查看Nginx信息HTTP/1.1 200 OK Server: nginx/1.1.2       ##此时的版本号就是伪造的版本号Date: Tue, 12 Nov 2019 14:34:02 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 12 Nov 2019 13:46:35 GMTConnection: keep-aliveETag: "5dcab7bb-264"Accept-Ranges: bytes

Nginx优化之缓存时间

当Nginx将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度
一般针对静态网页设置,对动态网页不设置缓存时间
可在Windows客户端中使用fiddler查看网页缓存时间

我们先准备一张图片在宿主机的共享目录中

把图片放到nginx的站点中

[root@localhost ~]# cd /chen/[root@localhost chen]# lsDiscuz_X3.4_SC_UTF8.zip    nginx-1.12.0.tar.gz  php-7.1.20.tar.gzfang.png                   nginx-1.12.2.tar.gz  shu.jpgmysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2[root@localhost chen]# cp shu.jpg /usr/local/nginx/html/[root@localhost chen]# cd /usr/local/nginx/html/[root@localhost html]# ls50x.html  index.html  shu.jpg[root@localhost html]# vim index.html  ##进入网页做个图片的路径 15  ##在第15行添加图片

设置缓存时间

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf##在server服务器级别76         location ~\.(gif|jepg|jpg|icp|bmp|png)$ { ##加入能使别这些图片格式 77             root html;  ##管理员页面 78             expires 1d;  ##缓存时间1天 16   user nginx nginx; ##16行加入nginx用户和组[root@localhost html]# service nginx start

客户机去测试一下访问网站

用抓包工具看一下缓存时间

Nginx之日志切割

随着Nginx运行时间增加,日志也会增加。为了方便掌握Nginx运行状态,需要时刻关注日志文件
太大的日志文件对监控是一个大灾难
定期进行日志文件的切割
Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割,并通过Linux的计划任务周期性的进行日志切割

编写日志分割脚本文件

[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     ##执行脚本文件

查看日志分割情况

[root@localhost ~]# cd /var/log/nginx/   ##切换到Nginx的日志目录下[root@localhost nginx]# lstest.com-access.log-20191112[root@localhost nginx]# date -s 2019-11-14  ##修改日期为明天的时间2019年 11月 14日 星期四 00:00:00 CST[root@localhost nginx]# cd ~[root@localhost ~]# ./fenge.sh     ##重新执行脚本[root@localhost ~]# cd /var/log/nginx/[root@localhost nginx]# ls           ##查看日志分割日志文件test.com-access.log-20191112  test.com-access.log-20191113

设置周期性计划任务

[root@localhost nginx]# crontab -e   ##周期性计划任务0 1 * * * /opt/fenge.sh

以上就是我们全部的内容了,谢谢收看

0