Nginx服务优化(1)——隐藏版本号、修改用户与组、网页缓存时间、日志切割、连接超时实验—实验步骤+理论超详细

一、Nginx服务优化

1.1、配置Nginx隐藏版本号

  • 隐藏Nginx版本号,避免安全漏洞泄露
  • Nginx隐藏版本号的方法
    ■修改配置法
    ■修改源码法

1.1.1、修改配置法

  • 先启动Nginx,再查看当前Nginx版本号
[root@localhost ~]# nginx  ## 启动nginx
[root@localhost ~]# curl -I http://20.0.0.25  ## 查看 nginx 版本号
HTTP/1.1 200 OK
Server: nginx/1.15.9  ## 版本号
Date: Sun, 06 Sep 2020 05:59:52 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 03 Sep 2020 02:47:02 GMT
Connection: keep-alive
ETag: "5f505926-264"
Accept-Ranges: bytes

在这里插入图片描述

  • 修改配置文件
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;  ##找到这个地方
server_tokens off;  ## 手动添加这一行,隐藏版本号
==>> wq 保存
  • 刷新Nginx配置,再次查看版本号
[root@localhost ~]# killall -s HUP nginx  ## 刷新nginx 配置
[root@localhost ~]# curl -I http://20.0.0.25 ##再次测试
HTTP/1.1 200 OK
Server: nginx  ## 版本号已隐藏
Date: Sun, 06 Sep 2020 06:10:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 03 Sep 2020 02:47:02 GMT
Connection: keep-alive
ETag: "5f505926-264"
Accept-Ranges: bytes

在这里插入图片描述

  • 我们也可以用 Wireshark 进行抓包查看,版本号已隐藏

在这里插入图片描述

1.1.2、修改源码法

  • 先进入目录修改原配置信息
[root@localhost ~]# vi /opt/nginx-1.15.9/src/core/nginx.h

原来的配置文件

在这里插入图片描述
我们进行修改后

在这里插入图片描述

  • 重新编译安装
[root@localhost ~]# cd /opt/nginx-1.15.9  ## 重新编译安装
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[root@localhost nginx-1.15.9]# make && make install 
  • 关闭隐藏版本号,重启 Nginx 服务
[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf  ## 打开
server_tokens on  ##把 off 改成 on
[root@localhost nginx-1.15.9]# killall -s QUIT nginx   ## 退出 nginx 服务
[root@localhost nginx-1.15.9]# nginx  ## 打开 nginx服务
  • 再次验证,版本号修改成功
[root@localhost nginx-1.15.9]# curl -I http://20.0.0.25
HTTP/1.1 200 OK
Server: IIS/1.1.1.1  ## 修改成功!
Date: Sun, 06 Sep 2020 06:29:05 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Thu, 03 Sep 2020 02:47:02 GMT
Connection: keep-alive
ETag: "5f505926-264"
Accept-Ranges: bytes

1.2、修改Nginx 用户与组

  • Nginx运行时进程需要有用户与组的支持,站文件读取时进行访问控制
  • Nginx默认使用nobody用户账号与组账号
  • 修改的方法
    ■编译安装时指定用户与组
    ■修改配置文件指定用户与组

1.2.1、编译安装时指定用户与组

  • 我们在编译安装 nginx 的时候也可以指定用户与组,指定安装目录
[root@localhost nginx-1.15.9]#  
./configure \ 
--prefix=/usr/local/nginx \   ##指定安装位置
--user=nginx \  ## 指定用户
--group=nginx \  ## 指定组账户
--with-http_stub_status_module

1.2.2、修改配置文件指定用户与组

  • 修改配置文件
[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
找到 #user nobody   ==>>  修改成 user nobody  ##打开配置文件后就在第一行,然后#号也删除

在这里插入图片描述

[root@localhost nginx-1.15.9]# killall -s HUP nginx   ## 刷新配置文件
[root@localhost nginx-1.15.9]# ps aux |grep nginx
## 修改完之后过滤查看下

修改成功

在这里插入图片描述

扫描二维码关注公众号,回复: 11596242 查看本文章

1.3、配置Nginx网页缓存时间

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

修改网页缓存配置文件,添加缓存天数

[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
location / {  ##找到这串代码,在下面添加
            root   html;
            index  index.html index.htm;
        }  
        
###添加部分####
location ~ \.(gif|jpg|jepg|png|bmp|ico)$ { 
            root   html;
            expires 1d;   ##指定缓存时间 1天
        }
==>> wq 保存
  • 检查语法并刷新Nginx服务
[root@localhost nginx-1.15.9]# 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

[root@localhost nginx-1.15.9]# killall -s HUP nginx  ## 刷新nginx 配置
  • 接着用 xftp 传一张图片 放进 /usr/local/nginx/html; 图片 名称叫 “b.jpg”

在这里插入图片描述

  • 然后打开浏览器输入 20.0.0.25/b.jpg ==>> 然后用 wireshark 进行抓包
    可以看见我们刚才设置的 expires 1d;(就是86400) 生效了

在这里插入图片描述

1.4、实现 Nginx 日志切割

  • 随着Nginx运行时间增加,日志也会增加握。为了方便掌Nginx运行状态,需要时刻关注Nginx日志文件
  • 太大的日志文件对监控是一个大灾难
    ■定期进行日志文件的切割
  • Nginx自身不具备日志分割处理的功能,但可以通过Nginx信号控制功能的脚本实现日志的自动切割
  • 通过Linux的计划任务周期性地进行日志切割
  • 编写脚本进行日志切割的思路
    1、设置时间变量
    2、设置保存日志路径
    3、将目前的日志文件进行重命名
    4、重建新日志文件
    5、删除时间过长的日志文件
    6、设置cron任务,定期执行脚本自动进行日志分割

1.4.1、实现 Nginx 日志切割实验

  • 编写日志切割脚本
[root@localhost ~]# cd /opt
[root@localhost opt]# vi 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"   
## nginx.pid 进程号(通过此进程号可查看nginx服务是否启动,如果启动了则有这个文件;
                                                   没有启动则没有这个文件)
[ -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)
find $logs_path -mtime +30 |xargs rm -rf   ## 找到30天前的日志,并删掉
== >> wq 保存
  • 执行脚本、验证脚本,并且我们也可以做一个计划任务,让其自动运维!
[root@localhost opt]# chmod +x fenge.sh  ## 给脚本执行权限
[root@localhost opt]# ./fenge.sh   ## 执行脚本
[root@localhost nginx]# cd /var/log/nginx/  ## 查看一下
[root@localhost nginx]# ls
test.com-access.log-20200905
[root@localhost nginx]# crontab -e  ##这边我们做计划任务,在每天的 1.15分自动运行
15 1 * * * /opt/fenge.sh   == > >  wq  保存

1.5、配置Nginx实现连接超时

  • 为避免同一客户端长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现控制连接访问时间
  • 超时参数
    ■Keepalive_timeout
        设置连接保持超时时间
    ■Client_header_timeout
        指定等待客户端发送请求头的超时时间
    ■Client_body_timeout
        设置请求体读超时时间

1.5.1、配置Nginx实现连接超时实验

  • 修改配置连接超时的参数
[root@localhost opt]# vi /usr/local/nginx/conf/nginx.conf	
#keepalive_timeout  0;    ## 找到这行代码,在后面添加如下代码
keepalive_timeout  65 180;   ## 在后面添加 180 参数
client_header_timeout 80;  ## 手动添加
client_body_timeout 80;  ## 手动添加
== > > wq  保存

在这里插入图片描述

  • 检测有无错误,并刷新Nginx配置
[root@localhost opt]# 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

[root@localhost opt]# killall -s HUP nginx   ## 刷新nginx配置
  • 在浏览器输入 20.0.0.25,进行抓包验证

在这里插入图片描述

本次内容就结束啦!
若有不足之处,请多多指教,敬请指出。

猜你喜欢

转载自blog.csdn.net/m0_46563938/article/details/108436223