您的位置:首页 > 运维架构 > Nginx

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

2019-11-13 22:57 1621 查看

配置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即可

第三步:验证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  index.html

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

[root@localhost html]# vim index.html

<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网页

谢谢阅读!!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: