第五十三集 Nginx的优化

本文介绍了Nginx的多项配置优化措施,包括隐藏版本号、修改用户和组、设置缓存时间、启用网页压缩功能及实现防盗链等。通过实际操作步骤展示如何提升Nginx服务器的安全性和性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


Nginx隐藏版本号

修改配置文件

[root@www ~]# curl 192.168.8.151
<h1> www.liqinmei.com </h1>
[root@www ~]# curl -I http://192.168.8.151
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sun, 27 Jun 2021 13:47:31 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 28
Last-Modified: Sun, 27 Jun 2021 06:38:40 GMT
Connection: keep-alive
ETag: "60d81cf0-1c"
Accept-Ranges: bytes
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf

[root@192 ~]# systemctl restart nginx
[root@192 ~]# curl -I http://192.168.8.151
HTTP/1.1 200 OK
Server: nginx
Date: Sun, 27 Jun 2021 14:12:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 27 Jun 2021 14:03:59 GMT
Connection: keep-alive
ETag: "60d8854f-264"
Accept-Ranges: bytes

修改源码


[root@192 nginx-1.15.9]# vim /opt/nginx-1.15.9/src/core/nginx.h
[root@192 nginx-1.15.9]# systemctl restart nginx
[root@192 nginx-1.15.9]# curl -I http://192.168.8.151
HTTP/1.1 200 OK
Server: Aphche/1.1.6
Date: Sun, 27 Jun 2021 14:43:12 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Sun, 27 Jun 2021 14:30:31 GMT
Connection: keep-alive
ETag: "60d88b87-264"
Accept-Ranges: bytes

修改用户和组

[root@192 nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.con

[root@192 nginx-1.15.9]# systemctl restart nginx
[root@192 nginx-1.15.9]# ps aux | grep nginx
root      21610  0.0  0.0  20560   624 ?        Ss   22:49   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     21611  0.0  0.0  23096  1392 ?        S    22:49   0:00 nginx: worker process
root      21615  0.0  0.0 112724   984 pts/0    S+   22:49   0:00 grep --color=auto nginx

设置缓存时间

[root@192 nginx-1.15.9]# vim /usr/local/nginx/conf/nginx.conf

[root@192 nginx-1.15.9]# cd /usr/local/nginx/html
[root@192 html]# rz -E
rz waiting to receive.
[root@192 html]# ls
1.jpg  50x.html  index.html  微信图片_20210627225905.jpg

[root@192 html]# vim index.html

[root@192 html]# systemctl restart nginx

认证

网页压缩功能

vim /usr/local/nginx/conf/nginx.conf
   gzip on;                  #开启gzip压缩功能
   gzip_min_length 1k;       #压缩阈值
   gzip_buffers 4 16k;       #buffer 大小为4个16k缓冲区大小
   gzip_http_version 1.1;    #压缩版本(默认不设置)
   gzip_comp_level 6;        #压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输速度快(建议5-6)
   gzip_types text/plain application/x-javascript text/css image/jpg image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javascript application/json;
   gzip_disable "MSIE [1-6]\.";  #配置禁用gzip条件,支持正则,表示ie6以下不启用gzip
   gzip_vary on;  #支持前端缓存服务器存储压缩页面
cd /usr/local/nginx/html/   		##首页中插入cat.jpg图片进行测试
vim index.html
	<h1>Welcome to nginx!</h1>
	<img src="1.jpg"/>			#插入一行
systemctl restart nginx

盗链与防盗链

盗链

准备三台机器
盗链端: 192.168.8.150 nginx服务
服务端:192.168.8.151 nginx服务
win10 : 192.168.8.155 #指向服务端DNS /hosts

win10 : 192.168.8.155 #指向服务端DNS /hosts

------------------------------------------服务端添加映射----------------------------------------------
vim /etc/hosts		##
192.168.8.151 www.zsx.com
------------------------------------------盗链端添加映射----------------------------------------------
vim /etc/hosts	
192.168.8.150 daolian
192.168.8.151 www.zsx.com
C:\WINDOWS\System32\drivers\etc		##win10 添加映射;打开目录位置
	PS:首次更改需要修改权限:属性——》安全——》高级——》更改为所有权限
cd /usr/local/nginx/html/
vim index.html 
	<h1>Welcome to nginx!</h1>        ##server端修改index.html.插入图片
	<img src="1.jpg"/>

使用win10 访问192.168.8.150 server端测试

cd /usr/local/nginx/html/			##设置盗链(在盗链端上)
[root@192 html]# ls
50x.html  index.html
[root@nginx html]# vim index.html 				##情况内容添加以下两行
	<h1>Welcome to nginx!</h1>
	<img src="http://www.zsx.com/1.jpg"/>

在win10主机中访问192.168.8.151 盗链网站,仍能访问出结果

防盗链

vim nginx.conf			##修改server端主机的nginx 配置文件
location ~*\.(jpg|gif|swf)$ {            		 ##添加location字段  
       valid_referers none blocked *.zsx.com zsx.com;               ##只允许来源为本地的访问源进行访问                
if ( $invalid_referer ) {
        rewrite ^/ http://www.zsx.com/error.png;
            }
        }


防盗链设置参数详细说明: valid_referers:设置信任的网站,即能引用相应图片的网站(白名单) none:浏览器中
Referer为空的情况,就是直接在浏览器访问图片
blocked:referer不为空的情况,但是值被代理或防火墙删除了,这些值不以http://或者https://开头
后面的网址或者域名:referer中包含相关字符串的网址 if语句:如果链接的来源域名不在 valid_referers所列出的列表中,
$invalid_referer为1,则执行后面的操作,即进行重写或返回403页面

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值