Ngnix优化与防盗链

33 阅读6分钟

一、隐藏版本号

可以使用 Fiddler 工具抓取数据包,查看 Nginx版本, 也可以在 CentOS 中使用命令 curl -I http://192.168.10.80 显示响应报文首部信息。

curl -I http://192.168.10.80

image.png

方法一:修改配置文件,关闭版本号

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
--------------------
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    
    }
 -------------------
 

image.png

重启服务,访问测试

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# curl -I http://192.168.10.80

image.png

方法二:修改源码文件中的版本号,重新编译安装

#修改源码文件
[root@localhost ~]# vim /opt/nginx-1.12.2/src/core/nginx.h

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


 #重新编译安装
 [root@localhost ~]# cd /opt/nginx-1.12.2/
 [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]# vim /usr/local/nginx/conf/nginx.conf
 http {
     include       mime.types;
     default_type  application/octet-stream;
     server_tokens on;
     ......
 }
 ​
 [root@localhost nginx-1.12.2]# systemctl restart nginx
 [root@localhost nginx-1.12.2]# curl -I http://172.16.10.101

image.png

image.png

二、修改nginx的运行用户和组

方法一:在编译安装时,指定运行用户和组

[root@localhost ~]# cd /opt/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure \   
--prefix=/usr/local/nginx \       #指定nginx的安装路径   
--user=nginx \                    #指定用户名(运行用户)   
--group=nginx \                   #指定组名   
--with-http_stub_status_module    #启用http_stub_status_module模块以支持状态统计

方法二:修改配置文件nginx.conf

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

#重启服务  
[root@localhost ~]# systemctl restart nginx
#查看是否修改成功。可以看到主进程由root创建,子进程由nginx创建  
[root@localhost ~]# ps aux | grep nginx

image.png

image.png

三、修改缓存时间

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

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

http {
......
	server {
	...... 
		location / {
			root html;
			index index.html index.htm;
		}
		
		location ~ \.(gif|jpg|jepg|png|bmp|ico)$  #加入新的 location,以图片作为缓存对象
			root html;
			expires 1d;	#指定缓存时间,1天
		}
......
	}
}

#重启nginx服务  
[root@localhost ~]# systemctl restart nginx
#访问测试  Linux系统中,打开火狐浏览器,访问 http://192.168.10.80/game.jpg  右击点查看元素  选择 网络 ---> 选择 HTML、WS、其他  双击响应消息查看响应头中包含 Cahce-Control:max-age=86400 表示缓存时间是 86400 秒。  也就是缓存一天的时间,一天之内浏览器访问这个页面,都是用缓存中的数据,而不需要向 Nginx 服务器重新发出请求,减少了服务器的使用带宽。

image.png

四、日志切割

[root@localhost ~]# vim /opt/fenge.sh

#!/bin/bash
# Filename: fenge.sh
day=$(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}/kgc.com-access.log-$day	#移动并重命名日志文件
kill -USR1 $(cat $pid_path)		#重建新日志文件
find $logs_path -mtime +30 -exec rm -rf {} \;	#删除30天之前的日志文件
#find $logs_path -mtime +30 | xargs rm -rf 

#赋予执行权限,执行脚本。查看日志文件目录
[root@localhost ~]# chmod +x /opt/fenge.sh
[root@localhost ~]# /opt/fenge.sh
[root@localhost ~]# ls /var/log/nginx
[root@localhost ~]# ls /usr/local/nginx/logs/access.log

#编写计划任务,每天定点执行
[root@localhost nginx]#crontab -e 
0 1 * * * /opt/fenge.sh

image.png

五、设置连接超时时间

HTTP有一个KeepAlive模式,它告诉web服务器在处理完一个请求后保持这个TCP连接的打开状态。若接收到来自客户端的其它请求,服务端会利用这个未被关闭的连接,而不需要再建立一个连接。

KeepAlive 在一段时间内保持打开状态,它们会在这段时间内占用资源。占用过多就会影响性能。

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

http {
...... 
    keepalive_timeout 65 180;   #三次握手的超时时间
    client_header_timeout 80;   #等待客户端发送请求头的超时时间会送408 错误
    client_body_timeout 80;     #设置客户端发送请求体的超时时间
...... 
}

image.png

六、更改进程数

 #1、查看cpu核数
 [root@localhost ~]# cat /proc/cpuinfo |grep processor|wc -l
 或
 [root@localhost ~]# cat /proc/cpuinfo |grep -c processor
 或
 [root@localhost ~]# cat /proc/cpuinfo | grep -c "physical id"
 ​
 #2、查看ginx主进程中包含几个工作进程
 [root@localhost ~]# ps aux | grep nginx
 ​
 #3、编辑配置文件,修改工作进程数
 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
 worker_processes  2;        #修改为与CPU核数相同
 worker_cpu_affinity 01 10;  #设置每个进程由不同cpu处理,进程数配为4时0001 0010 0100 1000
 ​
 #4、重启服务,查看修改后的工作进程
 [root@localhost ~]# systemctl restart nginx
 [root@localhost ~]# ps aux | grep nginx

Snipaste_2022-11-14_14-06-09.png

Snipaste_2022-11-14_14-07-18.png

Snipaste_2022-11-14_14-08-04.png

七、配置网页压缩

Nginx的ngx_http_gzip_module压缩模块提供对文件内容压缩的功能。

允许Nginx服务器将输出内容在发送客户端之前进行压缩,以节约网站带宽,提升用户的访问体验,默认已经安装。

可在配置文件中加入相应的压缩功能参数对压缩性能进行优化。

 #修改配置文件
 [root@localhost ~]#  vim /usr/local/nginx/conf/nginx.conf
 http {
 ...... 
    gzip on;                 #取消注释,开启gzip压缩功能
    gzip_min_length 1k;      #最小压缩文件大小
    gzip_buffers 4 64k;      #压缩缓冲区,大小为4个64k缓冲区
    gzip_http_version 1.1;   #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 6;       #压缩比率
    gzip_vary on;            #支持前端缓存服务器存储压缩页面
    
    #压缩类型,表示哪些网页文档启用压缩功能
    gzip_types text/plain text/javascript application/x-javascript text/css text/xml application/xml application/xml+rss image/jpg image/jpeg image/png image/gif application/x-httpd-php application/javascript application/json;
 ...... 
 }
 
 #传入图片
[root@localhost ~]# cd /usr/local/nginx/html
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# rz -E
rz waiting to receive.
[root@localhost html]# ls
50x.html  game.jpg  index.html
[root@localhost html]# vim index.html
-----
<img src="game.jpg"/>				#网页中插入图片
-----


 #重启服务
 [root@localhost ~]#  systemctl restart nginx
 
 #浏览器访问测试
 在Linux系统中,打开火狐浏览器,右击点查看元素
 选择 网络 ---> 选择 HTML、WS、其他 
 访问 192.168.10.80 ,双击响应消息查看响应头中包含 Content-Encoding: gzip

image.png image.png

八、配置防盗链

web源主机配置:

 #1、将test.jpg、error.png文件传到/usr/local/nginx/html目录下
 [root@localhost ~]# cd /usr/local/nginx/html
 ​
 #2、配置首页文件
 [root@localhost html]# vim index.html
 ----------
<img src="game.jpg"/>
 ----------
 
 #3、添加IP和域名的映射关系
[root@localhost html]# echo "192.168.10.80 www.km.com" >> /etc/hosts 
[root@localhost html]# echo "192.168.10.110 www.benet.com" >> /etc/hosts  

image.png

image.png 盗链网站主机配置:

 #1、切换到站点目录
 [root@localhost ~]# cd /usr/local/nginx/html
 
 #2、配置首页文件,图片盗用Web源主机中的图片资源
 [root@localhost html]# vim index.html
--------
 <img src="http://www.km.com/test.jpg"/>
--------
 #3、添加IP和域名的映射关系
[root@localhost html]# echo "192.168.10.80 www.km.com" >> /etc/hosts 
[root@localhost html]# echo "192.168.10.110 www.benet.com" >> /etc/hosts 
 ​
 在盗图网站主机上进行浏览器查看
 http://www.km.com       //Web源主机
 http://www.benet.com     //盗链网站

image.png

image.png

image.png

Web源主机配置防盗链

 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
 http {
 ......
     server {
     ......
     #不是由km.com域名请求的图片资源,重写到盗链图片error.png
     location ~* \.(jpg|gif|swf)$ {
            root  html;
            expires 1d;
            valid_referers none blocked *.km.com km.com;
                if ( $invalid_referer ) {
                  rewrite ^/ http://www.km.com/error.png;
                }
         }
     ......
     }
 }

 [root@localhost ~]# systemctl restart nginx 

image.png

九、fpm参数优化

Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整。

根据服务器的内存与服务负载,调整FPM模块参数。

 vim /usr/local/php/etc/php-fpm.conf 
 pid = run/php-fpm.pid
 ​
 vim /usr/local/php/etc/php-fpm.d/www.conf
 --96行--
 pm = dynamic                #fpm进程启动方式,动态的
 --107行--
 pm.max_children=20          #fpm进程启动的最大进程数
 --112行--
 pm.start_servers = 5        #动态方式下启动时默认开启的进程数,在最小和最大之间
 --117行--
 pm.min_spare_servers = 2    #动态方式下最小空闲进程数
 --122行--
 pm.max_spare_servers = 8    #动态方式下最大空闲进程数
 ​
 ​
 kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`         #重启php-fpm
 netstat -anpt | grep 9000

易错总结

1、修改配置文件后,必须重启服务才能生效。

2、防盗链实验中:盗链网站,需要将web主机的域名和IP的映射关系,写入/etc/hosts文件中,否则盗链网站无法展示web主机中的图片。

3、浏览器存在缓存时间,连续访问不同网址时,建议先清理浏览器的历史记录,否则展示的图片可能是上一个网站的。