linux cpu占用率如何看
249
2022-10-13
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]# ls nginx-1.12.2 rh [root@localhost opt]# cd nginx-1.12.2/ [root@localhost nginx-1.12.2]# ls auto CHANGES.ru configure html man src CHANGES 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 ok nginx: 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 Script PROG="/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 1 esac exit 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 OK Server: nginx/1.12.2 Date: Tue, 05 Nov 2019 07:30:14 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 05 Nov 2019 07:13:26 GMT Connection: keep-alive ETag: "5dc12116-264" Accept-Ranges: bytes
关闭版本号再检查一下
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf { ##在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 ##查看Nginx信息 HTTP/1.1 200 OK Server: nginx ##版本号被隐藏了 Date: Tue, 12 Nov 2019 14:22:00 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT Connection: keep-alive ETag: "5dcab7bb-264" Accept-Ranges: bytes
伪造版本号
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf { 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 ##查看Nginx信息 HTTP/1.1 200 OK Server: nginx/1.1.2 ##此时的版本号就是伪造的版本号 Date: Tue, 12 Nov 2019 14:34:02 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 12 Nov 2019 13:46:35 GMT Connection: keep-alive ETag: "5dcab7bb-264" Accept-Ranges: bytes
Nginx优化之缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存时间,以方便在日后进行相同内容的请求时直接返回,避免重复请求,加快了访问速度一般针对静态网页设置,对动态网页不设置缓存时间可在Windows客户端中使用fiddler查看网页缓存时间
我们先准备一张图片在宿主机的共享目录中
把图片放到nginx的站点中
[root@localhost ~]# cd /chen/
[root@localhost chen]# ls
Discuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz php-7.1.20.tar.gz
fang.png nginx-1.12.2.tar.gz https://www.apispace.com/news/zb_users/upload/2022/10/20221013070500_78181.jpg
mysql-boost-5.7.20.tar.gz php-7.1.10.tar.bz2
[root@localhost chen]# cp https://www.apispace.com/news/zb_users/upload/2022/10/20221013070500_78181.jpg /usr/local/nginx/html/
[root@localhost chen]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html https://www.apispace.com/news/zb_users/upload/2022/10/20221013070500_78181.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.sh d=$(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]# ls test.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
以上就是我们全部的内容了,谢谢收看
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~