前言:在企业信息化应用环境中,服务器的安全性和响应速度需要根据实际情况进行相应参数配置,以达到最优的用户体验。
默认的Nginx安装参数只能提供最基本的服务,还需要调整如网页缓存时间、链接超时、网页压缩等相应参数,才能发挥出服务器的最大作用。
下面将依次介绍Nginx隐藏版本号、更改用户与组、配置网页缓存时间、日志切割、设置连接超时。
文章目录
一、隐藏版本号
在生产环境中,需要隐藏Nginx的版本号,以避免泄露Nginx的版本,使攻击者不能针对特定版本进行攻击。在隐藏前,可以使用Fiddler工具抓取数据包,查看Nginx版本,也可以在CentOS中使用命令curl -I http://192.168.9.161(IP地址)/查看。
隐藏Nginx版本号有两种方式
1.修改Nginx源码文件
先用抓包软件查看信息
将Nginx的配置文件中的server_tokens选项值设置为off,如没有该配置项,加上即可。
[root@localhost conf]# curl -I http://192.168.9.161/ //查看信息
HTTP/1.1 200 OK
Server: nginx/1.12.2 //版本号
Date: Tue, 24 Dec 2019 11:11:37 GMT
Content-Type: text/html
Content-Length: 649
Last-Modified: Tue, 24 Dec 2019 10:44:03 GMT
Connection: keep-alive
ETag: "5e01ebf3-289"
Accept-Ranges: bytes
[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf //更改配置文件
20 server_tokens off; //隐藏版本
[root@localhost conf]# killall -s HUP nginx //重载服务
[root@localhost conf]# netstat -ntap|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16523/nginx: master
[root@localhost conf]# curl -I http://192.168.9.161/
HTTP/1.1 200 OK
Server: nginx //版本已隐藏
Date: Tue, 24 Dec 2019 11:16:29 GMT
Content-Type: text/html
Content-Length: 649
Last-Modified: Tue, 24 Dec 2019 10:44:03 GMT
Connection: keep-alive
ETag: "5e01ebf3-289"
Accept-Ranges: bytes
[root@localhost conf]# systemctl stop firewalld.service //关闭防护墙
[root@localhost conf]# setenforce 0
用抓包软件查看,版本已隐藏
2.修改Nginx的主配置文件
Nginx源码文件 /usr/src/nginx-1.6.3/src/core/nginx.h 包含了版本信息,可以随意设置,然后重新编译安装,即隐藏了版本信息。
[root@localhost conf]# curl -I http://192.168.9.161/ //查看信息
HTTP/1.1 200 OK
Server: nginx/1.12.2 //版本号
Date: Tue, 24 Dec 2019 11:11:37 GMT
Content-Type: text/html
Content-Length: 649
Last-Modified: Tue, 24 Dec 2019 10:44:03 GMT
Connection: keep-alive
ETag: "5e01ebf3-289"
Accept-Ranges: bytes
[root@localhost ~]# cd /opt/nginx-1.12.2/src/core/ //进入源码包内核
[root@localhost core]# ls //通过nginx.h更改版本
nginx.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.c
nginx.h ngx_file.c ngx_palloc.c ngx_rwlock.h
[root@localhost core]# vim nginx.h
13 #define NGINX_VERSION "1.1.1" //将版本号改为1.1.1
[root@localhost core]# cd ../../
[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]# killall -s QUIT nginx //关闭服务
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx
[root@localhost nginx-1.12.2]# nginx //启动服务
[root@localhost nginx-1.12.2]# netstat -ntap | grep nginx //确认开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16002/nginx: master
[root@localhost nginx-1.12.2]# curl -I http://192.168.9.161/ //查看
HTTP/1.1 200 OK
Server: nginx/1.1.1 //版本已更改
Date: Tue, 24 Dec 2019 12:07:47 GMT
Content-Type: text/html
Content-Length: 649
Last-Modified: Tue, 24 Dec 2019 10:44:03 GMT
Connection: keep-alive
ETag: "5e01ebf3-289"
Accept-Ranges: bytes
用抓包软件查看,版本显示为1.1.1
**
二、修改用户与组
Nginx运行时进程需要有用户与组的支持,用以实现对网站文件读取时进行访问控制。主进程由root创建,子进程由指定的用户与组创建。Nginx默认使用nobody用户账号与组账号,一般也要进行修改。
修改Nginx用户与组有两种方法,一种是在编译安装时指定用户与组,另一种是修改配置文件指定用户与组
1.在编译安装时指定用户与组
编译Nginx时指定用户与组,就是配置Nginx文件,在./configure后面指定用户与组的参数。
[root@localhost ~]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \ //指定用户名是nginx
> --group=nginx \ //指定组名是nginx
> --with-http_stub_status_module
[root@localhost nginx-1.12.2]# make && make install
2.修改配置文件指定用户与组
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
16 user nginx nginx; //修改用户为nginx,组为nginx
3.查看用户与组
重启Nginx查看进程运行情况,主进程由root账户创建,子进程则由nginx创建。
[root@localhost conf]# ps aux | grep nginx //查看进程运行情况
root 54583 0.0 0.0 20544 608 ? Ss 16:26 0:00 nginx: master process nginx //主进程由root创建
nginx 54584 0.0 0.1 23368 2200 ? S 16:26 0:00 nginx: worker process //子进程由nginx创建
root 69329 0.0 0.0 112716 964 pts/1 R+ 16:40 0:00 grep --color=auto nginx
三、配置网页缓存时间
当Nginx将网页数据返回给客户端后,可设置缓存的时间,以方便日后进行相同内容的请求时直接返回,避免重复请求,加快访问速度,一般只针对静态资源进行设置,对动态网页不用设置缓存时间。
1.查看
先将准备好的图片game.jpg复制到Nginx的工作目录中
[root@localhost ~]# cd /usr/local/nginx/html/ //将准备好的图片作为缓存对象复制到Nginx的工作目录中
[root@localhost html]# ls
50x.html index.php
[root@localhost html]# cp /tupian/LNMP/gun.jpg ./
[root@localhost html]# ls
50x.html gun.jpg index.php
访问http://192.168.9.161/game.jpg,用Fiddler工具进行抓包,查看响应报文,没有图片的缓存信息
2.修改配置文件
修改Nginx的配置文件,在新location段加入expires参数,指定缓存的时间,1d表示一天。
[root@localhost ~]# cd /opt/nginx-1.12.2/conf/
[root@localhost conf]# vim nginx.conf
48 location ~\.(gif|jepg|jpg|ico|bmp|png)$ { //加入新的location
49 root html;
50 expires 1d; //指定缓存时间为1天
51 }
3.查看
重启Nginx服务后,访问网址抓包,响应报文中含有Expries参数,表示缓存的时间。
其中的Cache-Control:max-age=86400表示缓存时间是86400秒,也就是缓存一天的时间,一天之内浏览器访问这个页面,都使用缓存中的数据,而不需要想Nginx服务器重新发出请求,减少了服务器的使用频度。
四、日志切割
随着Nginx运行时间的增加,产生的日志也会增加,为了方便掌握Nginx的运行状态,需要时刻关注Nginx日志文件。太大的日志文件对监控是一个大灾难,非常不便于分析排查,因此需要定期地进行日志文件的切割。
Nginx没有类似Apache的cronlog日志分割处理功能,但可以通过Nginx的信号控制功能脚本来实现日志的自动切割,并将脚本加入到Linux的计划任务中,让脚本在每天的固定时间执行,便可实现日志切割功能。
1.编写脚本并执行
[root@localhost ~]# cd /opt/
[root@localhost opt]# vim fenge.sh
#!/bin/bash
#Filename:fenge.sh
d=$(date -d "-1 day" "+%Y%m%d") //显示一天前的时间
logs_path="/var/log/nginx" //分割目录点
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path //创建日志文件目录
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d //移动并重命名日志文件
kill -USR1 $(cat $pid_path) //重建新日志文件
find $logs_path -mtime +30 | xargs rm -rf //删除30天之前的日志文件
[root@localhost opt]# ./fenge.sh //执行分割脚本
[root@localhost opt]# cd /var/log/nginx/
[root@localhost nginx]# ls
test.com-access.log-20191224 //按日期分割了日志文件
[root@localhost nginx]# cat /usr/local/nginx/logs/access.log //原来的日志文件重新创建
2.设置crontab
设置crontab任务,定期执行脚本自动进行日志分割
[root@localhost opt]# crontab -e
30 1 * * * /opt/fenge.sh
五、设置连接超时
在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可设置相应的连接超时参数,实现对连接访问时间的控制。可以修改配置文件nginx.conf,设置keepalive_timeout超时时间
1.超时参数讲解
Keepalive_timeout
设置连接保持超时时间,一般可只设置该参数,默认为75秒,可根据网站的情况设置,或者关闭,可在http段、server段、或者location段设置
Client_header_timeout
指定等待客户端发送请求头的超时时间
Client_body_timeout
设置请求体读超时时间
2.更改配置
[root@localhost opt]# cd /opt/nginx-1.12.2/conf/
[root@localhost conf]# vim nginx.conf
30 #keepalive_timeout 0;
31 keepalive_timeout 65 180; //设置超时是180秒
32 client_header_timeout 80; //设置头部header超时时间
33 client_body_timeout 80; //设置body超时时间
[root@localhost conf]# nginx -s stop //重启服务
[root@localhost conf]# nginx