Nginx 优化与防盗链
文章目錄
- 一、Nginx 優(yōu)化
- 1. 隱藏版本號(hào)
- (1) 隱藏版本號(hào)的原因
- (2) 版本號(hào)查看
- ① nginx -v (僅限 web 瀏覽器)
- ② curl -I
- ③ 瀏覽器查看
- (3) 隱藏方法
- ① 修改配置文件
- ② 修改源碼文件,重新編譯
- 2. 修改用戶(hù)與組
- 3. 緩存時(shí)間
- 4. 日志分割
- 5. 連接超時(shí)
- 6. 更改進(jìn)程數(shù)
- 7. 配置網(wǎng)頁(yè)壓縮
- 二、盜鏈與防盜鏈
- 1. 盜鏈
- 1.1 nginx 服務(wù)端配置
- 1.2 盜鏈主機(jī)配置
- 1.3 windows 測(cè)試機(jī)配置
- 1.4 windows 訪問(wèn)測(cè)試
- 2. 防盜鏈
- 2.1 nginx 服務(wù)端配置
- 2.2 windows 訪問(wèn)測(cè)試
- 三、FPM 參數(shù)優(yōu)化
一、Nginx 優(yōu)化
1. 隱藏版本號(hào)
(1) 隱藏版本號(hào)的原因
??為了安全,如果暴露版本信息,黑客可以通過(guò)版本信息得知該版本存在的漏洞,進(jìn)而對(duì)服務(wù)器進(jìn)行攻擊。隱藏版本信息可以避免黑客有的放矢的進(jìn)行破壞。
(2) 版本號(hào)查看
① nginx -v (僅限 web 瀏覽器)
[root@c7-1 ~]#nginx -v nginx version: nginx/1.12.2② curl -I
[root@c7-1 ~]#curl -I 127.0.0.1 HTTP/1.1 200 OK Server: nginx/1.12.2 Date: Fri, 08 Oct 2021 12:29:08 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Mon, 04 Oct 2021 12:54:14 GMT Connection: keep-alive ETag: "615af976-264" Accept-Ranges: bytes③ 瀏覽器查看
(3) 隱藏方法
① 修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confhttp { ......server_tokens off; #添加此行內(nèi)容,關(guān)閉版本號(hào)的顯示 ......[root@localhost ~]# systemctl restart nginx② 修改源碼文件,重新編譯
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confhttp { ......#server_tokens off; #注釋此行內(nèi)容,開(kāi)啟版本號(hào)的顯示 ......[root@localhost ~]# vim /opt/nginx-1.12.2/src/core/nginx.h##修改版本號(hào)和名稱(chēng),可偽裝成其他服務(wù)(例如 apache、mysql 等) #define NGINX_VERSION "5.7.20" #define NGINX_VER "mysql/" NGINX_VERSION#重新編譯 cd /opt/nginx-1.12.2/ ./configure \ --prefix=/usr/local/nginx \ --user=nginx \ --group=nginx \ --with-http_stub_status_modulemake -j 4 && make install systemctl restart nginx #隱藏成功 [root@c7-1 ~]#nginx -v nginx version: mysql/5.7.202. 修改用戶(hù)與組
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.confuser nginx nginx; #第二行,取消注釋,修改用戶(hù)為nginx,組為nginx[root@localhost nginx-1.12.0]# systemctl restart nginx [root@localhost nginx-1.12.0]# ps aux | grep nginx #主進(jìn)程由root創(chuàng)建,子進(jìn)程由nginx創(chuàng)建 root 42095 0.0 0.0 20500 628 ? Ss 23:29 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 42096 0.0 0.0 22948 1404 ? S 23:29 0:00 nginx: worker process root 42103 0.0 0.0 112676 976 pts/0 R+ 23:29 0:00 grep --color=auto nginx3. 緩存時(shí)間
??當(dāng) nginx 將網(wǎng)頁(yè)數(shù)據(jù)返回給客戶(hù)端后,可設(shè)置緩存的時(shí)間,以方便在日后進(jìn)行相同內(nèi)容的請(qǐng)求時(shí)直接返回,避免重復(fù)請(qǐng)求,加快了訪問(wèn)速度。一般針對(duì)靜態(tài)網(wǎng)頁(yè)進(jìn)行設(shè)置,對(duì)動(dòng)態(tài)網(wǎng)頁(yè)不設(shè)置緩存時(shí)間。
[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf......server {......location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ { #新建location,以圖片作為緩存對(duì)象root html;expires 1d; #指定緩存時(shí)間為1天,一天半為1d12h}......} ......[root@localhost nginx-1.12.0]# systemctl restart nginx在網(wǎng)頁(yè)中加入圖片后測(cè)試
[root@c7-1 /usr/local/nginx/html]#ls 50x.html index.html index.html.bak test.jpg[root@c7-1 /usr/local/nginx/html]#vim index.html #加入圖片 </h1> <img src="test.jpg"/> </body></html>4. 日志分割
編寫(xiě)腳本
[root@c7-1 /opt]# vim log_cut.sh #!/bin/bash #Filename:log_cut.shlastday=$(date -d "-1 day" +%Y%m%d) #顯示前一天的時(shí)間 logs_path=/var/log/nginx pid_path=/usr/local/nginx/logs/nginx.pid[ -d $logs_path ] || mkdir -p $logs_path #創(chuàng)建日志文件目錄 mv /usr/local/nginx/logs/access.log $logs_path/test.com_access.log-$lastday mv /usr/local/nginx/logs/error.log $logs_path/test.com_error.log-$lastday #移動(dòng)并重命名日志文件kill -USR1 $(cat $pid_path) #重建新日志文件 find $logs_path -mtime +30 -exec rm -rf {} \; #刪除30天之前的日志文件腳本測(cè)試
[root@c7-1 /opt]#chmod +x log_cut.sh [root@c7-1 /opt]#./log_cut.sh [root@c7-1 /opt]#cd /var/log/nginx/ [root@c7-1 /var/log/nginx]#ls test.com_access.log-20211007 test.com_error.log-20211007添加計(jì)劃任務(wù)
[root@c7-1 ~]#crontab -e no crontab for root - using an empty one crontab: installing new crontab [root@c7-1 ~]#crontab -l 0 1 * * * /opt/log_cut.sh5. 連接超時(shí)
??HTTP 有一個(gè) KeepAlive 模式,它告訴 web 服務(wù)器在處理完一個(gè)請(qǐng)求后保持這個(gè) TCP 鏈接的打開(kāi)狀態(tài)。若接收到來(lái)自同一客戶(hù)端的其他請(qǐng)求,服務(wù)端會(huì)利用這個(gè)未被關(guān)閉的連接,而不需要再建立一個(gè)連接。
??KeepAlive 在一段時(shí)間內(nèi)保持打開(kāi)狀態(tài),它們會(huì)在這段時(shí)間內(nèi)占用資源,占用過(guò)多就會(huì)影響性能。
6. 更改進(jìn)程數(shù)
- 在高并發(fā)環(huán)境中,需要啟動(dòng)更多的 nginx 進(jìn)程以保證快速響應(yīng),用以處理用戶(hù)的請(qǐng)求,避免造成阻塞
- 默認(rèn)情況下,nginx 的多個(gè)進(jìn)程可能更多的跑在一顆 CPU 上,可以分配不同的進(jìn)程給不同的 CPU 處理,以充分利用 cpu 性能
- worker_processes 最多開(kāi)啟 8 個(gè),8 個(gè)以上性能就不會(huì)再提升了,而且穩(wěn)定性會(huì)變低
7. 配置網(wǎng)頁(yè)壓縮
??nginx 的 ngx_http_gzip_module 壓縮模塊提供對(duì)文件內(nèi)容壓縮的功能。允許 nginx 服務(wù)器將輸出內(nèi)容在發(fā)送客戶(hù)端之前進(jìn)行壓縮,以節(jié)約網(wǎng)站帶寬,提升用戶(hù)的訪問(wèn)體驗(yàn),默認(rèn)已經(jīng)安裝,可在配置文件中加入相應(yīng)的壓縮功能參數(shù)對(duì)壓縮性能進(jìn)行優(yōu)化。
vim /usr/local/nginx/conf/nginx.conf ...... http {......gzip on; #開(kāi)啟 gzip 壓縮功能gzip_min_length 1k; #壓縮閾值,最小壓縮為 1kgzip_buffers 4 16k; # buffer 大小為 4 個(gè) 16k 緩沖區(qū)大小gzip_http_version 1.1; #壓縮版本(默認(rèn)不設(shè)置)gzip_comp_level 6; #壓縮比率,最小為 1,處理速度快,傳輸速度慢,9 最大壓縮比,處理速度慢,傳輸速度快(建議5-6)gzip_disable "MSIE [1-6]\."; #配置禁用 gzip 條件,支持正則,表示 ie6 以下不啟用 gzipgzip_vary on; #支持前端緩存服務(wù)器存儲(chǔ)壓縮頁(yè)面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;#支持的壓縮類(lèi)型...... }nginx -t cd /usr/local/nginx/html/ #首頁(yè)中插入 cat.jpg 圖片進(jìn)行測(cè)試vim index.html<h1>Welcome to nginx!</h1><img src="test.jpg"/> #插入一行systemctl restart nginx瀏覽器查看(注意清除瀏覽器緩存)
二、盜鏈與防盜鏈
在企業(yè)網(wǎng)站中,一般都要配置防盜鏈功能,以避免網(wǎng)站內(nèi)容被非法盜用,造成經(jīng)濟(jì)損失,也避免不必要的帶寬浪費(fèi)。
| nginx 服務(wù)端 | 192.168.10.12 |
| 盜鏈端 | 192.168.10.20 |
| Windows10 | 192.168.10.10 |
1. 盜鏈
1.1 nginx 服務(wù)端配置
預(yù)安裝 nginx 服務(wù) echo "192.168.10.12 www.nginx-server.com" >>/etc/hosts 上傳 test.jpg 到 /usr/local/nginx/html 目錄1.2 盜鏈主機(jī)配置
systemctl stop firewalld && systemctl disable firewalld setenforce 0 ntpdate ntp1.aliyun.com#安裝 httpd 服務(wù) yum -y install httpd systemctl start httpd && systemctl enable httpd#配置臨時(shí) DNS 映射 echo "192.168.10.12 www.nginx-server.com" >>/etc/hosts echo "192.168.10.20 www.daolian.com" >>/etc/hostscat > /var/www/html/index.html <<EOF <html><body><h1>this is a "盜鏈" test</h1> <img src= "http://www.nginx-server.com/test.jpg"/> </body></html> EOFsystemctl restart httpd1.3 windows 測(cè)試機(jī)配置
#在 hosts 文件添加映射,需要管理員權(quán)限 C:\Windows\System32\drivers\etc\hosts
1.4 windows 訪問(wèn)測(cè)試
訪問(wèn) nginx-server 查看圖片
訪問(wèn)盜鏈端
2. 防盜鏈
2.1 nginx 服務(wù)端配置
修改配置文件,添加如下內(nèi)容
[root@nginx ~]#vim /usr/local/nginx/conf/nginx.confhttp { ......server {......location ~* \.(jpeg|gif|jpg|swf)$ { #匹配不區(qū)分大小寫(xiě),以 .jpeg/.gif/.jpg/.swf 結(jié)尾的文件valid_referers none blocked *.nginx-server.com nginx-server.com; #設(shè)置信任來(lái)源可以正常訪問(wèn)資源if ( $invalid_referer ) {rewrite ^/ http://www.nginx-server.com/daolian.png;#return 403; #如果鏈接的來(lái)源域名不在 valid_referers 列表中,則 rewrite 跳轉(zhuǎn)頁(yè)面或者返回 403 頁(yè)面}}......} ...... }[root@nginx ~]#nginx -t [root@nginx ~]#systemctl restart nginx上傳 daolian.jpg 圖片到 /usr/local/nginx/html 目錄下2.2 windows 訪問(wèn)測(cè)試
三、FPM 參數(shù)優(yōu)化
nginx 的 PHP 解析功能實(shí)現(xiàn)如果是交由 FPM 處理的,為了提高 PHP 的處理速度,可對(duì) FPM 模塊進(jìn)行參數(shù)的調(diào)整。
- 首先安裝帶 FPM 模塊的 PHP 環(huán)境,保證 PHP 可以正常運(yùn)行
- FPM 進(jìn)程有兩種啟動(dòng)方式,由 pm 參數(shù)指定,分別是 static 和 dynamic,前者將產(chǎn)生固定數(shù)據(jù)的 fpm 進(jìn)程,后者將以動(dòng)態(tài)的方式產(chǎn)生 fpm 進(jìn)程
- static 方式可以使用 pm.max_children 指定啟動(dòng)的進(jìn)程數(shù)量。dynamic 方式的參數(shù)則要根據(jù)服務(wù)器的內(nèi)存與服務(wù)負(fù)載進(jìn)行調(diào)整,參數(shù)下所示
| pm.max_children | 指定啟動(dòng)的進(jìn)程的最大的數(shù)量 |
| pm.start_servers | 動(dòng)態(tài)方式下初始的 ftpm 進(jìn)程數(shù)量 |
| pm.min_spare_servers | 動(dòng)態(tài)方式下最小的 fpm 空閑進(jìn)程數(shù) |
| pm.max_spare_servers | 動(dòng)態(tài)方式下最大的 fpm 空閑進(jìn)程數(shù) |
假設(shè)云服務(wù)器上運(yùn)行了個(gè)人論壇,內(nèi)存為 1.5 GB ,fpm 進(jìn)程數(shù)為20,內(nèi)存消耗將近 1GB ,處理速度較慢,需對(duì)參數(shù)進(jìn)行優(yōu)化處理
[root@server ~]# vim /usr/local/php/etc/php-fpm.conf pid = run/php-fpm.pid[root@server ~]# vim /usr/local/php/etc/php-fpm.d/www.conf pm = dynamic #將以動(dòng)態(tài)的方式產(chǎn)生fpm進(jìn)程 pm.max_children=20 #static模式下空閑進(jìn)程數(shù)上限,大于下面的值 pm.start_servers = 5 #動(dòng)態(tài)方式下默認(rèn)開(kāi)啟的進(jìn)程數(shù),在最小和最大之間 pm.min_spare_servers = 2 #動(dòng)態(tài)方式下最少空閑進(jìn)程數(shù) pm.max_spare_servers = 8 #動(dòng)態(tài)方式下最大空閑進(jìn)程數(shù)#FPM 啟動(dòng)時(shí)有5個(gè)進(jìn)程,最小空閑2個(gè)進(jìn)程,最大空閑8個(gè)進(jìn)程,最多可以有20個(gè)進(jìn)程存在 #重啟 php-fpm [root@server ~]# kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` [root@server ~]# netstat -anpt | grep 9000總結(jié)
以上是生活随笔為你收集整理的Nginx 优化与防盗链的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 计算机设备停用代码22,Win7电脑提示
- 下一篇: 网络分析系列---Nginx优化