Nginx优化实战(日志分割、图片缓存、隐藏版本号)
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,Nginx日志分割实例:[root@nginx nginx-1.12.2]# cd /usr/local/nginx/logs/[root@nginx logs]# lsaccess.log err
千家信息网最后更新 2026年11月01日Nginx优化实战(日志分割、图片缓存、隐藏版本号)
Nginx日志分割实例:
[root@nginx nginx-1.12.2]# cd /usr/local/nginx/logs/[root@nginx logs]# lsaccess.log error.log nginx.pid[root@nginx logs]# date2019年 11月 14日 星期四 13:49:11 CST[root@nginx logs]# date -d "0 day" "+%Y%m%d"20191114 //以字符串形式显示[root@nginx logs]# date -d "-1 day" "+%Y%m%d"20191113 //统计的是前一天[root@nginx logs]# cd /opt/[root@nginx opt]# lsnginx-1.12.2 rh[root@nginx opt]# touch aaa.txt[root@nginx opt]# find /opt -name ".txt" //按名字进行查找/opt/aaa.txt[root@nginx opt]# find /opt -name ".txt" | rm -rf //后面跟删除命令可以删除吗?[root@nginx opt]# lsaaa.txt nginx-1.12.2 rh //此时无法删除[root@nginx opt]# find /opt -name "*.txt" | xargs rm -rf //使用传递命令[root@nginx opt]# lsnginx-1.12.2 rh//以上内容为Shell脚本中的常用手法:前面一条命令的执行结果,作为后面一条命令的参数//创建日志分割脚本[root@nginx opt]# 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"[ -d $logs_path ] || mkdir -p $logs_pathmv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$dkill -USR1 $(cat $pid_path)find $logs_path -mtime +30 | xargs rm -rf//按Esc退出插入模式,输入:wq保存退出[root@nginx opt]# chmod +x fenge.sh[root@nginx opt]# ./fenge.sh [root@nginx opt]# cd /var/log/[root@nginx log]# lsanaconda glusterfs rhsm vmware-vmusr.logaudit grubby_prune_debug sa wpa_supplicant.logboot.log lastlog samba wtmpbtmp libvirt secure Xorg.0.logchrony maillog speech-dispatcher Xorg.0.log.oldcron messages spooler Xorg.1.logcups nginx sssd Xorg.9.logdmesg ntpstats tallylog yum.logdmesg.old pluto tunedfirewalld ppp vmware-vgauthsvc.log.0gdm qemu-ga vmware-vmsvc.log[root@nginx log]# cd nginx/[root@nginx nginx]# lstest.com-access.log-20191113[root@nginx nginx]# date -s 2019-11-132019年 11月 13日 星期三 00:00:00 CST[root@nginx nginx]# date2019年 11月 13日 星期三 00:00:15 CST[root@nginx nginx]# lstest.com-access.log-20191113 test.com-access.log-20191115[root@nginx nginx]# cd /opt/[root@nginx opt]# lsfenge.sh nginx-1.12.2 rh[root@nginx opt]# ./fenge.sh [root@nginx opt]# cd /var/log/nginx/[root@nginx nginx]# lstest.com-access.log-20191112 test.com-access.log-20191113[root@nginx nginx]# cd /usr/local/nginx[root@nginx nginx]# lsclient_body_temp fastcgi_temp logs sbin uwsgi_tempconf html proxy_temp scgi_temp[root@nginx nginx]# cd logs/[root@nginx logs]# lsaccess.log error.log nginx.pid//日志文件在启动时自动产生Nginx缓存时间实例:
[root@nginx logs]# umount /aaa[root@nginx logs]# mount.cifs //192.168.10.193/rpm /aaaPassword for root@//192.168.10.193/rpm: [root@nginx logs]# ls /aaa/rpmls: 无法访问/aaa/rpm: 没有那个文件或目录[root@nginx logs]# ls /aaaapr-1.6.2.tar.gz error.png nginx-1.12.2.tar.gzapr-util-1.6.0.tar.gz httpd-2.4.29.tar.bz2 php-7.1.10.tar.bz2awstats-7.6.tar.gz lf.jpg php-7.1.20.tar.gzcronolog-1.6.2-14.el7.x86_64.rpm mysql-5.6.26.tar.gzDiscuz_X3.4_SC_UTF8.zip mysql-boost-5.7.20.tar.gz[root@nginx html]# vim index.html Welcome to nginx!
//在welcome下一行插入图片行,格式如上,修改完后输入:wq保存退出此时再刷新之前的网页就会出现我们链接进去的图片:
[root@nginx html]# vim /usr/local/nginx/conf/nginx.conf//76行做如下修改:location ~\.(gif|jepg|jpg|ico|bmp|png)$ { root html; expires 1d; } }//在default_type下行插入以下内容:http { include mime.types; default_type application/octet-stream; server_tokens on;//在worker_connections下行插入以下内容:events { worker_connections 1024; }user nginx nginx;//修改完成后按Esc退出插入模式,输入:wq保存退出[root@nginx html]# service nginx stop[root@nginx html]# service nginx start此时哦我们刷新网页,对图片进行抓包信息的查询,可以看到图片的缓存信息为:从2019年11月12日,到2019年的11月13日,缓存时间为一天
Nginx隐藏版本实例:
####方法一:隐藏版本号
[root@nginx ~]# curl -I http://192.168.18.136/HTTP/1.1 200 OKServer: nginx/1.12.2 //此处显示Nginx版本号Date: Tue, 12 Nov 2019 20:59:15 GMTContent-Type: text/htmlContent-Length: 632Last-Modified: Tue, 12 Nov 2019 16:39:13 GMTConnection: keep-aliveETag: "5dcae031-278"Accept-Ranges: bytes[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confhttp { include mime.types; default_type application/octet-stream; server_tokens off;//在default_type下一行插入以上内容,修改完成后按Esc后输入:wq保存退出[root@nginx ~]# service nginx stop[root@nginx ~]# service nginx start[root@nginx ~]# curl -I http://192.168.18.136/HTTP/1.1 200 OKServer: nginx //此时版本号被隐藏Date: Tue, 12 Nov 2019 21:07:13 GMTContent-Type: text/htmlContent-Length: 632Last-Modified: Tue, 12 Nov 2019 16:39:13 GMTConnection: keep-aliveETag: "5dcae031-278"Accept-Ranges: bytes方法二:伪造版本号
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confserver_tokens on; //把off改为on//修改完成后按Esc退出插入模式,输入:wq保存退出[root@nginx conf]# cd /opt[root@nginx opt]# lsfenge.sh nginx-1.12.2 rh[root@nginx opt]# cd nginx-1.12.2/[root@nginx nginx-1.12.2]# lsauto CHANGES.ru configure html Makefile objs srcCHANGES conf contrib LICENSE man README[root@nginx nginx-1.12.2]# cd src/[root@nginx src]# lscore event http mail misc os stream[root@nginx src]# cd core/[root@nginx core]# lsnginx.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.cnginx.h ngx_file.c ngx_palloc.c ngx_rwlock.hngx_array.c ngx_file.h ngx_palloc.h ngx_sha1.cngx_array.h ngx_hash.c ngx_parse.c ngx_sha1.hngx_buf.c ngx_hash.h ngx_parse.h ngx_shmtx.cngx_buf.h ngx_inet.c ngx_parse_time.c ngx_shmtx.hngx_conf_file.c ngx_inet.h ngx_parse_time.h ngx_slab.cngx_conf_file.h ngx_list.c ngx_proxy_protocol.c ngx_slab.hngx_config.h ngx_list.h ngx_proxy_protocol.h ngx_spinlock.cngx_connection.c ngx_log.c ngx_queue.c ngx_string.cngx_connection.h ngx_log.h ngx_queue.h ngx_string.hngx_core.h ngx_md5.c ngx_radix_tree.c ngx_syslog.cngx_cpuinfo.c ngx_md5.h ngx_radix_tree.h ngx_syslog.hngx_crc32.c ngx_module.c ngx_rbtree.c ngx_thread_pool.cngx_crc32.h ngx_module.h ngx_rbtree.h ngx_thread_pool.hngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.cngx_crypt.c ngx_murmurhash.h ngx_regex.h ngx_times.hngx_crypt.h ngx_open_file_cache.c ngx_resolver.cngx_cycle.c ngx_open_file_cache.h ngx_resolver.h//修改nginx.h内核文件,但是后期需要重新编译安装[root@nginx core]# vim nginx.h#define NGINX_VERSION "1.1.5"//修改完成后按Esc退出插入模式,输入:wq保存退出[root@nginx core]# cd ../../[root@nginx nginx-1.12.2]# lsauto CHANGES.ru configure html Makefile objs srcCHANGES conf contrib LICENSE man README[root@nginx nginx-1.12.2]# ./configure \--prefix=/usr/local/nginx \--user=nginx \--group=nginx \--with-http_stub_status_module[root@nginx nginx-1.12.2]# make && make install[root@nginx nginx-1.12.2]# service nginx stop[root@nginx nginx-1.12.2]# service nginx start[root@nginx nginx-1.12.2]# curl -I http://192.168.18.136/HTTP/1.1 200 OKServer: nginx/1.1.5Date: Tue, 12 Nov 2019 21:38:05 GMTContent-Type: text/htmlContent-Length: 632Last-Modified: Tue, 12 Nov 2019 16:39:13 GMTConnection: keep-aliveETag: "5dcae031-278"Accept-Ranges: bytes//此时显示的版本号就是我们修改过的1.1.5
版本
输入
图片
内容
命令
模式
日志
缓存
实例
文件
星期
一行
信息
方法
时间
网页
脚本
内核
参数
名字
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技有限公司
互联网金融科技
云信互联网科技有限公司
科技平台互联网
西安互联网科技
银优互联网科技有限公司
深圳天众互联网科技公司
科技类的互联网平台
济南华为互联网科技产业总部基地
全球顶尖互联网科技公司
互联网金融中的现代科技
民营互联网公司国防科技
乌鲁木齐新华智原互联网科技学校
元气玛特互联网科技公司是外包吗
智帮互联网科技公司最近动向
上海创道互联网络科技有限公司
半导体是互联网高科技吗
张家口科技产业互联网平台
数字互联网科技有限公司
伍佰万互联网科技是干嘛的
瑰夏咖啡
互联网科技海报
互联网科技工作委员会主任
互联网公司黑科技创始人
互联网方面的科技前沿
北京市互联网新科技
江苏互联网科技小贷
昆明中奇联合互联网科技
科技信息时代互联网发展
现代互联网科技公司