Linux—Nginx优化—缓存and隐藏版本号
一.隐藏版本号
1.在生产环境中,需要隐藏Nginx的版本号,以免泄露Nginx的版本,使得攻击者不能针对特定版本进行攻击。
2.查看Nginx的版本有两种方法
1)使用fiddler工具抓取数据包,查看Nginx版本
2)在Centos7上使用使用命令 curl -I http://192.168.88.138/ 查看
3.隐藏Nginx版本号也有两种方法
1)修改Nginx的源码文件,指定不显示版本号
2)修改Nginx的主配置文件
二.如何隐藏版本号:
1.伪造版本号:(如若已安装Nginx,则需重新编译)
修改源码包:
[root@localhost mnt]# ls
nginx-1.12.2.tar.gz
[root@localhost mnt]# tar zxvf nginx-1.12.2.tar.gz -C /opt
[root@localhost mnt]# vim /opt/nginx-1.12.2/src/core/nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "1.1.1"
#define NGINX_VER "nginx/" NGINX_VERSION
正常安装编译Nginx:
安装环境包:
[root@localhost mnt]# yum install gcc gcc-c++ pcre* zlib-devel make -y
创建用户
[root@localhost mnt]# useradd -M -s /sbin/nologin nginx
配置,安装且编译
[root@localhost mnt]# cd /opt/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
优化服务控制,service工具
[root@localhost nginx-1.12.2]# vim /etc/init.d/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 nginx-1.12.2]# chmod +x /etc/init.d/nginx
关防火墙开服务
[root@localhost nginx-1.12.2]# systemctl stop firewalld.service
[root@localhost nginx-1.12.2]# setenforce 0
[root@localhost nginx-1.12.2]# service nginx start
查看版本号:
[root@localhost nginx-1.12.2]# curl -I http://192.168.88.137
HTTP/1.1 200 OK
Server: nginx/1.1.1
Date: Fri, 27 Dec 2019 06:33:57 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 27 Dec 2019 06:31:08 GMT
Connection: keep-alive
ETag: "5e05a52c-264"
Accept-Ranges: bytes
2.隐藏版本号:
修改配置文件:
[root@localhost init.d]# vim /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens 0ff:
重启服务:
[root@localhost init.d]# service nginx stop
[root@localhost init.d]# service nginx start
查看:
[root@localhost init.d]# curl -I http://192.168.88.138
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 27 Dec 2019 06:43:36 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 27 Dec 2019 04:06:47 GMT
Connection: keep-alive
ETag: "5e058357-264"
Accept-Ranges: bytes
三.缓存优化
1.当Nginx将网页数据返回给给客户端之后,可以设置缓存的时间,方便下次再浏览相同的内容时直接返回,避免重复请求,加快访问速度,一般只针对静态资源设置,对于动态网页不用设置缓存时间
2.在Nginx服务中,expires参数指定缓存时间
3.当没有设置expires参数时,使用Fiddler进行抓包
四.优化缓存操作
1.先拷贝照片:
[root@localhost ~]# cd /mnt
[root@localhost mnt]# cp 123.jpg /usr/local/nginx/html/
[root@localhost mnt]# cd /usr/local/nginx/html
[root@localhost html]# ls
123.jpg 50x.html index.html
2.添加图片:
[root@localhost html]# vim index.html
<img src="123.jpg"/>
3.修改配置:
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
添加:
location ~\.(gif|jepg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
}
4.重启服务
[root@localhost html]# service nginx stop
[root@localhost html]# service nginx start