Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

时间:2022-02-23 02:05:39

原文:https://blog.51cto.com/11134648/2134389

默认的Nginx安装参数只能提供最基本的服务,还需要调整如网页缓存时间、连接超时、网页压缩等相应参数,才能发挥出服务器的最大作用。
下面实验用到的抓包工具存放在百度网盘,密码:0fl5
Ngnix服务的安装详细介绍请参考 部署Nginx网站服务实现访问状态统计以及访问控制功能

  • 一、Nginx服务优化

可以从隐藏版本号、更改用户与组、配置网页缓存时间、日志切割、设置连接超时这几个方面进行优化。

1.隐藏版本号

在生产环境中需要隐藏Nginx的版本号,以避免泄露Nginx的版本,使×××者不能针对特定版本进行×××。查看Nginx的版本在CentOS中使用命令curl -I http://172.16.10.10/即可

[root@localhost ~]# curl -I http://172.16.10.10/
HTTP/1.1 200 OK
Server: nginx/1.12.0 #Nginx版本信息
Date: Fri, 29 Jun 2018 08:52:27 GMT
Content-Type: text/html
Content-Length: 483
Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT
Connection: keep-alive
ETag: "5b35d814-1e3"
Accept-Ranges: bytes

隐藏版本号有两种方式,一种是修改Nginx的源码文件,指定不显示版本号,第二种是修改Nginx的主配置文件。

(1)修改主配置文件的方式如下:

将Nginx的配置文件中的server_tokens选项值设置为off,如没有该配置项,加上即可。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
........... #省略内容
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; #关闭版本号
............ #省略内容
[root@localhost ~]# nginx -t    #测试配置文件
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

再次访问网址,只显示Nginx,版本号已经隐藏。

[root@localhost ~]# service nginx restart #重新启动nginx服务
[root@localhost ~]# curl -I http://172.16.10.10/
HTTP/1.1 200 OK
Server: nginx #nginx隐藏了版本号
Date: Fri, 29 Jun 2018 09:09:36 GMT
Content-Type: text/html
Content-Length: 483
Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT
Connection: keep-alive
ETag: "5b35d814-1e3"
Accept-Ranges: bytes

(2)Nginx的源码文件包含了版本信息,可以随意设置,然后重新编译安装,就会隐藏版本信息。

[root@localhost ~]# vim /opt/nginx-1.12.0/src/core/nginx.h #编辑源码文件

#define NGINX_VERSION      "1.1.1"              #修改版本号
#define NGINX_VER "IIS" NGINX_VERSION #修改服务器类型

重新编译安装

[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 && make && make install

再次访问网址,只显示修改之后的版本信息。

[root@localhost nginx-1.12.0]# service nginx restart      #重启nginx服务
[root@localhost nginx-1.12.0]# curl -I http://172.16.10.10/HTTP/1.1 200 OK
Server: IIS1.1.1 #nginx的版本信息
Date: Fri, 29 Jun 2018 09:30:09 GMT
Content-Type: text/html
Content-Length: 483
Last-Modified: Fri, 29 Jun 2018 06:56:20 GMT
Connection: keep-alive
ETag: "5b35d814-1e3"
Accept-Ranges: bytes

2.修改用户和组

Nginx运行时进程需要有用户与组的支持,用以实现对网站文件读取时进行访问控制。主进程由root创建,子进程由指定的用户与组创建。Nginx默认使用nobody用户账号与组账号,一般要修改。

(1)编译Nginx时指定用户与组,就是配置nginx时,在./configure后面指定用户与组的参数。

[root@localhost ~]# cd /opt/nginx-1.12.0/
[root@localhost nginx-1.12.0]#./configure --prefix=/usr/local/nginx
--user=nginx #指定用户名是nginx
--group=nginx #指定组名是nginx
--with-
&& make && make install

(2)修改Nginx配置文件nginx.conf指定用户与组。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf 

user nginx nginx;     #修改用户为nginx,组为nginx

重启nginx查看进程运行情况,主进程由root账户创建,子进程由nginx创建。

[root@localhost ~]# ps aux | grep nginx
root 14923 0.0 0.0 20540 624 ? Ss 17:30 0:00 nginx: master process /usr/local/nginx/sbin/nginx #主进程由root创建
nginx 14925 0.0 0.1 22984 1412 ? S 17:30 0:00 nginx: worker process #子进程由nginx创建
root 19344 0.0 0.0 112720 984 pts/0 R+ 17:47 0:00 grep --color=auto nginx

3.配置网页缓存时间

当Nginx将网页数据返回给客户端后,可设置缓存时间,方便日后进行相同内容请求是直接返回,避免重复请求,加快访问速度,一般只针对静态资源进行设置,对动态网页不用设置缓存时间。
操作步骤如下所示:

(1)以图片作为缓存对象,将game.jpg放到Nginx的网站目录下。

[root@localhost ~]# cd /usr/local/nginx/html/    #Nginx的网站目录
[root@localhost html]# ls
50x.html error.png game.jpg index.html test.html

(2)访问http://172.16.10.10/game.jpg, 再用Fidder工具抓包,查看响应报文,没有图片的缓存信息。

Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

(3)修改配置文件,在新的location段加入expire参数,指定缓存时间,1d表示一天。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

    location ~\.(gif|jpg|jepg|png|bmp|ico)$ {    #加入新的location
root html;
expires 1d; #指定缓存时间
}

(4)重启nginx服务,访问网址抓包,响应报文中含有Expire参数,表示缓存的时间。

[root@localhost ~]# service nginx restart

Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

4.日志切割

随着Nginx的运行时间的增加,产生的日志也会增加。太大的日志文件非常不便于分析和排查,因此需要定期的进行日志文件的切割。Nginx没有类似Apache的cronlog日志分割处理功能,但可以通过Nginx的信号控制功能脚本来实现日志的自动切割,并将脚本加入到Linux的计划任务中,让脚本在每天的固定时间执行。

(1)首先编写脚本/opt/fenge.sh,把Nginx的日志文件/usr/local/nginx/logs/access.log移动到目录/var/log/nginx下面,以当前时间作为日志文件的名称,然后用kill-USR1创建新的日志文件/usr/local/nginx/logs/access.log,最后删除前30天的日志文件。

[root@localhost ~]# vim /opt/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天之前的日志文件

(2)执行/opt/fenge.sh,测试日志文件是否被切割。

[root@localhost ~]# chmod +x /opt/fenge.sh
[root@localhost ~]# ./fenge.sh #执行分割脚本
[root@localhost ~]# ls /var/log/nginx/
test.com-access.log-20180628

(3)设置crontab任务,定期执行脚本自动进行日志分割。

[root@localhost ~]# crontab -e
0 1 * * * /opt/fenge.sh #每天的凌晨1点执行/opt/fenge.sh脚本

5.设置连接超时

在企业网站中,为了避免同一个客户长时间占用连接,造成资源浪费,可以设置相应的连接超时参数,实现对连接访问的时间的控制。

(1)修改配置文件nginx.conf,设置keepalive_timeout超时时间。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
... #省略内容
http {
...
keepalive_timeout 65 180; #设置超时是180秒
client_header_timeout 80; #指定请求头的超时时间
client_body_timeout 80; #指定请求体超时时间
... #省略内容
}

keepalive_timeout 第一个参数指定了与客户端的keep-alive连接超时时间,服务器将会在这个时间后关闭连接;第二个参数指定了响应头Keep-Alive:timeout=time中的time值。这个头能让浏览器主动关闭连接,这样服务器就不必去关闭连接。
(2)重启nginx服务,访问网址,用Fidder工具抓包。
Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

  • 二、Nginx优化深入

    可以从更改进程数、配置网页压缩、配置防盗链这几个方面进行深入优化。

1.更改进程数

在高并发环境中,需要启动更多的nginx进程以保证快速响应,用以处理用户的请求,避免造成阻塞。使用ps aux命令查看Nginx运行的进程的个数。

[root@localhost ~]# ps aux | grep nginx
root 14923 0.0 0.0 20540 624 ? Ss 17:30 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 14925 0.0 0.1 22984 1412 ? S 17:30 0:00 nginx: worker process
root 19344 0.0 0.0 112720 984 pts/0 R+ 17:47 0:00 grep --color=auto nginx

其中master process是主进程,开启了一个,worker process是子进程,也是开启了一个。

(1)修改nginx的文件中的worker_process参数,一般设为CPU的个数或核数,在高并发的情况下,可设置为CPU的个数或者核数的2倍,可以先查看CPU的核数以确定参数。

[root@localhost ~]# cat /proc/cpuinfo | grep -c "physical"
2

参数设置为2,和CPU核数相同,运行进程数设置多一些,响应客户端请求时,Nginx就不会临时启动新的进程提供服务,减少了系统的开销,提升了服务的速度。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
worker_proces 2;

(2)修改完后,重启服务,查看运行进程数的变化。

[root@localhost ~]# service nginx restart
[root@localhost ~]# ps aux | grep nginx
root 21453 0.0 0.0 20540 636 ? Ss 20:11 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 21457 0.0 0.1 23000 1424 ? S 20:11 0:00 nginx: worker process
nginx 21458 0.0 0.1 23000 1424 ? S 20:11 0:00 nginx: worker process
root 21472 0.0 0.0 112720 984 pts/0 S+ 20:11 0:00 grep --color=auto nginx

开启了一个主进程和2个子进程,可见参数设置起了作用。

2.配置网页压缩

Nginx的ngx_http_gzip_module压缩模块提供了对文件内容压缩的功能,以节约网站的带宽,提升用户的访问体验,默认nginx已经安装该模块,只需要在配置文件加入相应的压缩功能参数对压缩性能进行优化。

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on; #开启gzip压缩输出
gzip_buffers 4 64k; #表示申请4个单位为64kB的内存作为压缩结果流缓存
gzip_http_version 1.1; #用于设置http协议版本,默认是1.1
gzip_comp_level 2; #指定gzip压缩比,压缩比最小,处理速度最快
gzip_min_length 1k; #设置允许压缩的页面最小字节数
gzip_vary on; #让前端的缓存服务器缓存经过gzip压缩的页面
gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss text/jpg text/png; #压缩类型

重启服务,以大小超过1KB的html文件作为nginx网站内容,然后访问网址抓取数据报文,显示使用gzip进行了压缩。
Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

3.配置防盗链

在企业网站服务中,一般都要配置防盗链功能,以避免网站内容被非法盗用,造成经济损失,也避免不必要的带宽浪费。

(1)先实现盗链

需要准备两台主机模拟盗链,一台主机作为客户端访问盗链网站。

IP地址 域名 用途 系统
172.16.10.10 www.benet.com 源主机 CentOS7
172.16.10.20 www.bt.com 盗链主机 CentOS7
172.16.10.8 -------------- 客户机 Windows7
1)修改客户机的C:\Windows\System32\drivers\etc\hosts文件,设置域名和IP映射关系。
172.16.10.10  www.benet.com
172.16.10.20 www.bt.com
2)修改源主机和盗链主机的hosts文件,设置域名和IP映射关系。
[root@localhost ~]# vim /etc/hosts
172.16.10.10 www.benet.com
172.16.10.20 www.bt.com
3)把图片game.jpg放到源主机benet.com的站点目录下。
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html game.jpg index.html
4)在盗链主机bt.com的站点目录下编写盗链页面test.html,盗取源主机benet.com的图片。
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# vim test.html
<html>
<h1>Server 172.16.10.20</h1>
<body>
<img src="http://www.benet.com/game.jpg"/>
</body>
</html>
5)访问盗链网页http://www.bt.com/test.html, 查看是否盗链成功。

在图片上点击右键选择属性,可以看到网址是http://www.benet.com/game.jpg。
Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)
Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

(2)配置Nginx防盗链

Nginx的防盗原理是加入location项,用正则表达式过滤图片类型文件,对于信任的网址可以正常使用,对于不信任的网址则返回相应的错误图片。在源主机benet.com的配置文件加入以下代码:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|swf)$ {
valid_referers none blocked *.benet.com benet.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.benet.com/error.png;
}
}
  • ~*.(jpg|gif|swf)$: 匹配不区分大小写,以.jpg 或.gif或 .swf结尾的文件。
  • valid_referers:设置信任的网站,可以正常使用图片。
  • none:浏览器中refer为空的情况,就是直接在浏览器访问图片。
  • blocked:浏览器中refer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或 https://开头
  • 后面的网址或域名:refer包含相关字符串的网址。
  • if语句:如果链接的来源域名不在valid_referers所列出的列表中, $invalid_referer 为1,则执行后面的操作,即进行重写或返回403页面。
    1)把图片error.png放到源主机benet.com的站点目录下。
    [root@localhost ~]# cd /usr/local/nginx/html/
    [root@localhost html]# ls
    50x.html error.png game.jpg index.html test.html
    2)这时重启服务器,重新访问http://www.test.com/test.html, 显示的是被重写的图片,说明防盗链成功。

    Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

需要注意的是如果在配置文件中加入了新的location项去配置网页缓存时间,那么防盗链加入的location项应置于其之前,才能防盗链成功。