Nginx website service optimization (hide version number, modify users and groups, cache time, log segmentation)

One, hide the version number

1.1 View version number

  • You can use the Fiddler tool to grab data packets and check the Nginx version
  • You can also use the command curl -I http://192.168.153.20 in CentOs to display the header information of the response message
    curl -I http://192.168.153.10
    Insert picture description here
    Insert picture description here
    Insert picture description here

Insert picture description here

1.2 Hide version number

  • Method 1: Modify the configuration file (hidden)
vim /usr/local/nginx/conf/nginx.conf
http {
     include       mime.types;
	 default_type  application/octet-stream;
	 server_tokens off;                        #添加,关闭版本号
	 ......
}

systemctl restart nginx.service
curl -I http://192.168.153.10

Insert picture description here
Insert picture description here

  • Method 2: Modify the source file, recompile and install
vim /opt/nginx-1.12.0/src/core/nginx.h
#define NGINX_VERSION "1.1.1"          #修改版本号
#define NGINX_VER "IIS" NGINX_VERSION  #修改服务器类型

cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
vim /usr/local/nginx/conf/nginx.conf 
http {
      include     mime.types;
   default_type application/octet-stream;
   server_tokens on;
   ......
   }
   
systemctl restart nginx
curl -I http://192.168.153.10

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Method one is equivalent to hiding the version number, method one is equivalent to changing the displayed version number

Two, modify users and groups

vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;                          #取消注释,修改用户为 nginx ,组为nginx

systemctl restart nginx

ps aux | grep nginx
主进程由root创建,子进程由nginx创建

Insert picture description here
Insert picture description here

Insert picture description here

Three, cache time

vim /usr/local/nginx/conf/nginx.conf
http {
......
    server {
 ......
     location / {
      root html;
   index index.html index.htm;
  }
  
  location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {  #加入新的 location.以图片作为缓存对象
     root html;
     expires 1d;    #指定缓存时间,1天
  }
......
    }
}
systemctl restart nginx

浏览器验证
http://www/wt.com/hua.jpg

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here

在Linux系统中,打开火狐浏览器,右击点查看元素
选择网络 ---> 选择 HTML、WS、其他
访问 http://196.168.153.10 ,双击200响应消息查看头包含 Cahce-Control:Max-age=86400 
表示缓存时间是86400秒。也就是缓存一天时间,一天之内浏览器访问这个界面,都是用缓存中的数据,
而不需要向 Nginx 服务器重新发出请求,减少了服务器的使用带宽

Four, log split

vim /opt/fenge.sh
#!/bin/bash
# Filename:fenge.sh
-------------脚本内容--------------
day=$(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_path                                          #创建日志文件目录
mv /usr/local/nginx/logs/access.log ${logs_path}/kgc.com-access.log-$day          #移动并重命名日志文件
kill -USR1 $(cat $pid_path)                                                       #重建新日志文件
find $logs_path -mtime +30 -exec rm -rf {} \;                                     #删除30天之前的日志文件
#find $logs_path -mtime +30 | xargs rm -rf
---------------------------------

. /opt/fenge.sh
ls /var/log/nginx
ls /usr/local/nginx/logs/access.log        

crontab -e
0 1 * * * . /opt/fenge.sh             #设置周期性任务,每个小时执行一次这个脚本

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51613313/article/details/112568797