服务器之家:专注于服务器技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx隐藏版本号与网页缓存时间的方法

Nginx隐藏版本号与网页缓存时间的方法

2020-01-20 11:56caozhengtao1213 Nginx

这篇文章主要介绍了Nginx优化之隐藏版本号与网页缓存时间的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

Nginx优化---隐藏版本号与网页缓存时间

配置Nginx隐藏版本号

在生产环境中,需要隐藏Nginx的版本号,以避免安全

漏洞的泄漏

查看方法

●使用fiddler I具在Windows客户端查看Nginx版本号

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

Nginx隐藏版本号的方法

●修改配置文件法

●修改源码法

修改配置文件法

1.Nginx的配置文件中的server_ tokens 选项的值设置为off

?
1
2
3
4
5
[root@www conf]# vim nginx.conf
.....
server_ tokens off;
.....
[root@www conf]# nginx -t

2.重启服务,访问网站使用curl -I命令检测

?
1
2
3
4
[root@www conf]# service nginx restart
[root@www conf]# curl -1 http://192.1 68.9.209/
HTTP/1.1200 OK
Server: nginx

3.若php配置文件中配置了fastcgi param SERVER SOFTWARE选项。则编辑php-fpm配置文件,将fastcgi param SERVER SOFTWARE对应的值修改为

?
1
fastcgi_ param SERVER_ SOFTWARE nginx ;

修改源码法

Nginx源码文件/usr/src/nginx-1.12.0/src/core/nginx.h包含了版本信息,可以随意设置重新编译安装,隐藏版本信息

示例:

?
1
2
#define NGINX_ _VERSION“1.1.1” ,修改版本号为1.1.1
#define NGINX_ VER "IIS/" ,修改软件类型为IIS

重启服务,访问网站使用curl -I命令检测

修改Nginx用户与组

Nginx运行时进程需要有用户与组的支持,以实现对网站文件读取时进行访问控制

Nginx默认使用nobody用户账号与组账号,一般也要进行修改

修改的方法

●编译安装时指定用户与组
●修改配置文件指定用户与组

修改配置文件法指定

1.新建用户账号,如nginx

2.修改主配置文件user选项,指定用户账号

3.重启nginx服务,使配置生效

4.使用ps aux命令查看nginx的进程信息,验证运行用户

账号改变效果

?
1
2
3
4
5
6
7
[root@www conf]# vi nginx.conf
user nginx nginx;
[root@www conf]# service nginx restart
[root@www conf]# ps aux lgrep nginx
root    1300340.0 0.0 20220 620? Ss 19:41 0:00 nginx: master process
/usr/local/sbin/nginx
nginx  1300350.0 0.0 20664 1512 ?S 19:41 0:00 nginx: worker process

配置Nginx网页缓存时间

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

设置方法

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

示例

修改Nginx的配置文件,在location段加入expires参数

?
1
2
3
location ~ \.(gifjpgliepglpnglbmplico)$ {
root html;
expires 1d;

隐藏版本号实例演示

一、编译安装Nginx服务

第一步:远程获取Windows上的源码包,并挂载到Linux上

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password:
Sharename    Type   Comment
---------    ----   -------
LNMP      Disk
 
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:
[root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip  nginx-1.12.2.tar.gz
game.jpg          php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz
nginx-1.12.0.tar.gz

第二步:解压源码包

?
1
2
3
4
[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0 rh

第三步:下载安装编译组件包

?
1
2
3
4
5
6
[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \       //C语言
> gcc-c++ \     //c++语言
> pcre-devel \   //pcre语言工具
> zlib-devel    //压缩函数库

第四步:创建程序用户并配置Nginx服务相关组件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//创建程序用户nginx,并限定其不可登录终端
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \     
//配置nginx
> --prefix=/usr/local/nginx \   
//指定安装路径           
> --user=nginx \
//指定用户名
> --group=nginx \
//指定用户所属组
> --with-http_stub_status_module
//安装状态统计模块

第五步:编译与安装Nginx

?
1
[root@localhost nginx-1.12.0]# make && make install

第六步:优化Nginx服务启动脚本,并建立命令软连接

?
1
2
3
4
5
6
7
8
9
10
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
//创建nginx服务命令软链接到系统命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service
//关闭防火墙
[root@localhost nginx-1.12.0]# setenforce 0
//关闭增强型安全功能
[root@localhost nginx-1.12.0]# nginx
//输入nginx 开启服务
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80   //查看服务的80 端口,显示已开启
tcp    0   0 0.0.0.0:80       0.0.0.0:*        LISTEN   7520/nginx: master

第七步:systemctl管理nginx脚本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost ~]# vim /lib/systemd/system/nginx.service   ##创建配置文件
 
[Unit]
Description=nginx                      ##描述
After=network.target                    ##描述服务类型
[Service]
Type=forking                          ##后台运行形式
PIDFile=/usr/local/nginx/logs/nginx.pid      ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx       ##启动服务
ExecReload=/usr/bin/kill -s HUP $MAINPID  ##根据PID重载配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID    ##根据PID终止进程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
 
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service   ##设置执行权限
[root@localhost ~]# systemctl stop nginx.service    ##关闭nginx
[root@localhost ~]# systemctl start nginx.service    ##开启nginx

二、修改配置文件法隐藏版本号

第一步:默认情况下查看Nginx版本号

?
1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~]# curl -I http://192.168.235.158   ##查看版本号
HTTP/1.1 200 OK
Server: nginx/1.12.0
##可见版本号为1.12.0
Date: Wed, 13 Nov 2019 08:32:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 06 Nov 2019 01:53:19 GMT
Connection: keep-alive
ETag: "5dc2278f-264"
Accept-Ranges: bytes

第二步:修改nginx.conf配置文件

?
1
2
3
4
5
6
7
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
 
http {
  include    mime.types;
  default_type application/octet-stream;
  server_tokens off;
##在http协议段落中加入server_ tokens选项的值设置为off即可

 

jpg

第三步:验证Nginx版本号隐藏

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~]# systemctl stop nginx.service
[root@localhost ~]# systemctl start nginx.service
[root@localhost ~]# curl -I http://192.168.235.158
HTTP/1.1 200 OK
Server: nginx
##可见版本号已被隐藏
Date: Wed, 13 Nov 2019 09:18:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 06 Nov 2019 01:53:19 GMT
Connection: keep-alive
ETag: "5dc2278f-264"
Accept-Ranges: bytes

三、修改配置源码法法隐藏版本号

第一步:修改nginx.conf配置文件

?
1
2
3
4
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
...
 server_tokens on;
 ##将off替换成on

第二步:修改源码文件nginx.h中的版本信息

?
1
2
3
4
[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h
 
#define NGINX_VERSION   "1.1.1"
##更改版本信息为1.1.1

第三步:重新编译Nginx

?
1
2
3
4
5
6
7
[root@localhost ~]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.12.0]# make && make install

 

第四步:验证Nginx版本号隐藏

?
1
2
3
4
5
6
7
8
9
10
11
[root@localhost nginx-1.12.0]# curl -I http://192.168.235.158
HTTP/1.1 200 OK
Server: nginx/1.1.1
##可见版本号已成功更改为1.1.1
Date: Wed, 13 Nov 2019 10:20:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Wed, 06 Nov 2019 01:53:19 GMT
Connection: keep-alive
ETag: "5dc2278f-264"
Accept-Ranges: bytes

 

网页缓存时间实例演示

第一步:复制图片到站点目录

?
1
2
3
4
5
6
7
8
9
[root@localhost nginx-1.12.0]# ls /abc
Discuz_X3.4_SC_UTF8.zip  nginx-1.12.2.tar.gz
game.jpg          php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz
nginx-1.12.0.tar.gz
[root@localhost nginx-1.12.0]# cp /abc/game.jpg /usr/local/nginx/html/
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html game.jpg index.html

 

第二步:修改Nginx的index.html网页

 

?
1
2
3
4
5
[root@localhost html]# vim index.html
 
<h1>Welcome to nginx!</h1>
<img src="game.jpg"/>
##在h1标签下添加图片路径

 

第三步:修改Nginx .conf文件

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
 
user nginx nginx;
##单独输入此行条目,指定用户nginx,指定组nginx
 
 location ~\.(gif|jepg|jpg|ico|bmp|png)$ {
      root html;
      expires 1d;
      ##上述图片类型图片缓存一天
    }
 
[root@localhost html]# systemctl stop nginx.service
[root@localhost html]# systemctl start nginx.service

第四步:打开一台Win10虚拟机验证

在客户机中安装fiddler.exe抓包软件,并打开浏览器访问192.168.235.158网页

Nginx隐藏版本号与网页缓存时间的方法

总结

以上所述是小编给大家介绍的Nginx隐藏版本号与网页缓存时间,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://blog.51cto.com/14449521/2450139

延伸 · 阅读

精彩推荐