Nginx服务优化 ---- 隐藏版本号 、页面缓存

Nginx服务优化 ---- 隐藏版本号 、页面缓存

一:配置Nginx隐藏版本号

  • 在生产环境中,需要隐藏Nginx的版本号,以避免安全漏洞的泄漏
1、查看方法
  • 使用fiddler工具在Windows客户端查看Nginx版本号

  • 在CentOS系统中使用"curl -I 网址”命令查看

2、Nginx隐藏版本号的方法
  • 修改配置文件法

  • 修改源码法

二:实验过程

1、首先先安装好nginx服务
[root@localhost ~]# yum install gcc gcc-c++ zlib-devel pcre pcre-devel -y            ‘安装依赖包’
[root@localhost ~]# useradd -M -s /sbin/nologin nginx        ‘创建名为nginx的用户,不允许登录系统’
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.10.29/share /abc       ‘挂载共享文件’
[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/           ‘解压nginx压缩包’
[root@localhost opt]# cd nginx-1.12.2/
[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.2]# make && make install
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin      '优化路径'
[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

[root@localhost etc]# vim init.d/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"
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
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
[root@localhost init.d]# service nginx start
[root@localhost init.d]# netstat -ntap | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5774/nginx: master  

可以在win10客户机上测试,输入虚拟主机的IP地址

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TV8OeA0N-1577272650713)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577265521221.png)]

隐藏 nginx 版本号有两种方式:

第一种:直接修改 Nginx 的主配置文件,让其不显示版本号即可;

第二种:修改 Nginx 源码文件 。

2、查看版本号
[root@localhost ~]# curl -I http://192.168.34.157/        ‘可以知道nginx的版本号’
3、第一种方法:修改Nginx主配置文件 。 进入配置文件,在http协议中进行修改
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IW1tBarH-1577272650715)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577266203065.png)]

[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start    ‘重启nginx’
[root@localhost ~]# curl -I http://192.168.34.157/      ‘再次查看版本号,已被隐藏了’
4、第二种方法:修改nginx源码文件。源码文件中包含了版本信息,可以随意设置(自定义),然后再重新编译安装,就隐藏了真实的版本信息。
[root@localhost ~]# cd opt/nginx-1.12.2/src/core/nginx.h    '找源码包'
[root@localhost core]# vim nginx.h

在这里插入图片描述

5、重新编译安装
[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.2]# make && make install

[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.34.157/         ‘再次查看’

再次查看,这时的版本号已被隐藏,被自己修改了
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vRvSyLjl-1577272650716)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577269741400.png)]

注意:如果版本号没有修改成功,也不显示,有可能是配置文件中关闭了版本显示。可开启 :server_tokens on;

三:页面缓存

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

  • 一般针对静态网页设置,对动态网页不设置缓存时间;

  • 可在Windows客户端中使用fiddler查看网页缓存时间;

  • 设置方法:

  • 可修改配置文件,在http段、 或者server段、 或者location段加入对特定内容的过期参数。

1、 以图片作为缓存对象,先复制一张图片到 Nginx 的工作目录
[root@localhost abc]# cp dog.jpg /usr/local/nginx/html/        ‘图片放在共享文件’
[root@localhost html]# ls
50x.html  dog.jpg  index.html        ‘查看是否复制成功到html目录中’
2、修改网页,将图片加入到网页内容中
[root@localhost html]# vim index.html

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C9ITZFZx-1577272650717)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577270900384.png)]

在win10中验证,输入IP地址,就可以看到图片
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8mtYJIse-1577272650717)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577271166882.png)]

3、改nginx 的配置文件
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost html]# service nginx start

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oi24drRh-1577272650718)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577271956931.png)]

4、在win10客户机中访问网页并抓包

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DHefNlF5-1577272650719)(C:\Users\xumin\AppData\Roaming\Typora\typora-user-images\1577272385001.png)]

设置缓存时间为一天后,就表示在一天内浏览器访问这个网页,都会使用缓存中的数据,而不需要向 nginx服务器重新发出请求了,减少了服务器的使用频度

发布了62 篇原创文章 · 获赞 11 · 访问量 2356

猜你喜欢

转载自blog.csdn.net/XuMin6/article/details/103704340