当前位置: 移动技术网 > 网络运营>服务器>nginx > Nginx隐藏版本号与网页缓存时间的方法

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

2020年03月09日  | 移动技术网网络运营  | 我要评论

街霸2剧场版,徐铮喜剧电影,褪魔粉在哪买

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

配置nginx隐藏版本号

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

漏洞的泄漏

查看方法

●使用fiddler i具在windows客户端查看nginx版本号

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

nginx隐藏版本号的方法

●修改配置文件法

●修改源码法

修改配置文件法

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

[root@www conf]# vim nginx.conf
.....
server_ tokens off;
.....
[root@www conf]# nginx -t

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

[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对应的值修改为

fastcgi_ param server_ software nginx ;

修改源码法

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

示例:

#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的进程信息,验证运行用户

账号改变效果

[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参数

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

隐藏版本号实例演示

一、编译安装nginx服务

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

[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

第二步:解压源码包

[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

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

[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \       //c语言
> gcc-c++ \     //c++语言
> pcre-devel \   //pcre语言工具
> zlib-devel    //压缩函数库

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

[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

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

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

[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脚本

[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版本号

[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配置文件

[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版本号隐藏

[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配置文件

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

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

[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h

#define nginx_version   "1.1.1"
##更改版本信息为1.1.1

第三步:重新编译nginx

[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版本号隐藏

[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

网页缓存时间实例演示

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

[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 

第二步:修改nginx的网页

[root@localhost html]# vim 

<h1>welcome to nginx!</h1>
<img src="game.jpg"/>
##在h1标签下添加图片路径

第三步:修改nginx .conf文件

[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隐藏版本号与网页缓存时间,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网