Nginx优化和防盗链

目录

一,隐藏版本号

 二,修改用户和组/nginx

 三,设置缓存时间

 四,日志切割

 五,连接超时

六,更改进程数量(解决高并发,内核优化)

 七,网页压缩

 八,防盗链与盗链

2,防盗链(192.168.135.113)


一,隐藏版本号

1,在生产环境中,需要隐藏nginx的版本号,以避免安全漏洞

 2,查看方法

[root@kya ~]#curl -I 192.168.135.113
HTTP/1.1 200 OK
Server: nginx/1.22.0 修改nginx版本号
Date: Mon, 29 Aug 2022 07:58:28 GMT
Content-Type: text/html
Content-Length: 731
Last-Modified: Sun, 28 Aug 2022 05:22:10 GMT
Connection: keep-alive
ETag: "630afb82-2db"
Accept-Ranges: bytes
[root@kya ~]#

3,修改配置文件

[root@kya conf]#vim /usr/local/nginx/conf/nginx.conf
[root@kya conf]#systemctl reload nginx.service 
[root@kya conf]#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
 

 curl -I http:/192.169.135.112

 

 4,修改源码

 [root@kya core]#vim nginx.h

 ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module 
重新编译安装
make && make install
将方法一中的nginx关闭的版本号重新打开
进行验证

 systemctl restart nginx.service

 二,修改用户和组/nginx

 

 三,设置缓存时间

1,修改主配置文件

2,上传图片,修改站点文件

-rw-r--r--. 1 root root 8787 8月 14 14:22 rabbit.jpg
[root@kya html]#vim index.html 
[root@kya html]#systemctl restart nginx.service 
[root@kya html]#curl -I http://192.168.135.112/rabbit.jpg
HTTP/1.1 200 OK 1.1长链接缓存机制,100ok返回码
Server: mysql/0.00.0 什么服务跑的
Date: Sun, 14 Aug 2022 06:26:31 GMT 时间 
Content-Type: image/jpeg 连接类型
Content-Length: 8787 图片长度
Last-Modified: Sun, 14 Aug 2022 06:22:28 GMT
Connection: keep-alive 会话保持
ETag: "62f894a4-2253"
Expires: Mon, 15 Aug 2022 06:26:31 GMT 缓存机制
Cache-Control: max-age=86400 最大的缓存周期,生命周期
Accept-Ranges: bytes
[root@kya html]#

 

 四,日志切割

1,创建脚本

[root@kya opt]#cat fengge.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/cxk112.access.log ${logs_path}/test.com-accss.log-$d
kill -HUP $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
[root@kya opt]#

二,给与权限进行执行分割

 

 五,连接超时

keepalive_timeout(tcp连接三次握手的超时时间)

  •     指定keepalive的超时时间(timeout),指定每个TCP连接最多可保持多长时间,服务器将会在这个时间后关闭连接,nginx的默认值是65秒,有些浏览器最多只保持60秒,所以可设定为60秒,若将它设置为0,就禁止了keepalive连接。
  •      第二个参数(可选的)指定了再相应头keep-alive:timeout=time中的time值。这个头能让一些浏览器主动关闭连接,这样服务器就不必去关闭连接了。没有这个参数,nginx不会发送keep-alive相应头。

client_header_timeout(客户端请求头的超时时间)

  • 客户端向服务器发送一个完整的request header(请求头)的超时时间,如果客户端再指定时间内没有发送一个完整的request header,nginx返回httpd408的返回码(request timed out:请求超时)
     

clent_body_timeout(客户端请求体的超时时间)

  • 指定客户端与服务器建立连接后发送request body的超时时间,如果客户端在指定时间内没有发送任何内容,nginx返回HTTPD408(request timed out)

[root@kya conf]#systemctl restart nginx.service 
[root@kya conf]#
 

vim /usr/local/nginx/conf/nginx.conf
http {
...... 
    keepalive_timeout 65 180;
    client_header_timeout 80;
    client_body_timeout 80;
...... 
}

systemctl restart nginx
 

六,更改进程数量(解决高并发,内核优化)

cat /proc/cpuinfo | grep -c "physical id"	#查看cpu核数
ps aux | grep nginx	#查看nginx主进程中包含几个子进程
vim /usr/local/nginx/conf/nginx.conf
worker_processes 2;	#修改为核数相同或者2倍
worker_cpu_affinity 01 10;#设置每个进程由不同cpu处理,进程数配2 4 6 8分别为0001 0010 0100 1000 
systemctl restart nginx

 修改主配置文件

 什么时间会用

1,高并发

2,cup压力不均衡

3,负载均衡

 七,网页压缩

vim /usr/local/nginx/conf/nginx.conf

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; #压缩类型,表示哪些网页文档启用压缩功能
...... 
}

 

 八,防盗链与盗链

1,配置盗链(192.168.135.112)

[root@kya ~]#cd /usr/local/nginx/html/
[root@kya html]#ll
总用量 268
-rw-r--r--. 1 root root 232086 8月   8 00:34 1.jpg
-rw-r--r--. 1 root root  32215 8月   9 08:39 2.jpg
-rw-r--r--. 1 root root    497 8月   7 23:48 50x.html
-rw-r--r--. 1 root root    693 8月  10 08:43 index.html
[root@kya html]#vim index.htm

 

2,防盗链(192.168.135.113)

        location ~*\.(jpg|gif|swf)$ {
        valid_referers none blocked *.daolian.com daolian.com;  自定义匹配来源的访问路径  受信任网站
        if ( $invalid_referer ) {
         rewrite ^/ http://www.daolian.com/error.png;
       }
 }
 
 

 

  • ~* .(jpg|gif|jepg|bmp|ico)$ :这段正则表达式表示匹配不区分大小写,以.jpg 或.gif 或.swf结尾的文件;

  • valid_referers :设置信任的网站,可以正常使用图片;

  • 后面的网址或者域名 :referer 中包含相关字符串的网址;

  • if语句:如果链接的来源域名不在valid_referers所列出的列表中,$invalid_referer为1,则执行后面的操作,即进行重写或返回403 页面

添加一张用来指向不符合规定访问域名者指向的照片

 

作者:AKA|布鲁克林欧神仙原文地址:https://blog.csdn.net/m0_54594153/article/details/126586695

%s 个评论

要回复文章请先登录注册