日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Nginx 优化与防盗链

發(fā)布時間:2023/12/29 Nginx 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx 优化与防盗链 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 一、Nginx 優(yōu)化
    • 1. 隱藏版本號
      • (1) 隱藏版本號的原因
      • (2) 版本號查看
        • ① nginx -v (僅限 web 瀏覽器)
        • ② curl -I
        • ③ 瀏覽器查看
      • (3) 隱藏方法
        • ① 修改配置文件
        • ② 修改源碼文件,重新編譯
    • 2. 修改用戶與組
    • 3. 緩存時間
    • 4. 日志分割
    • 5. 連接超時
    • 6. 更改進程數(shù)
    • 7. 配置網(wǎng)頁壓縮
  • 二、盜鏈與防盜鏈
    • 1. 盜鏈
      • 1.1 nginx 服務端配置
      • 1.2 盜鏈主機配置
      • 1.3 windows 測試機配置
      • 1.4 windows 訪問測試
    • 2. 防盜鏈
      • 2.1 nginx 服務端配置
      • 2.2 windows 訪問測試
  • 三、FPM 參數(shù)優(yōu)化

一、Nginx 優(yōu)化

1. 隱藏版本號

(1) 隱藏版本號的原因

??為了安全,如果暴露版本信息,黑客可以通過版本信息得知該版本存在的漏洞,進而對服務器進行攻擊。隱藏版本信息可以避免黑客有的放矢的進行破壞。

(2) 版本號查看

① 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)容,關閉版本號的顯示 ......[root@localhost ~]# systemctl restart nginx

② 修改源碼文件,重新編譯

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.confhttp { ......#server_tokens off; #注釋此行內(nèi)容,開啟版本號的顯示 ......[root@localhost ~]# vim /opt/nginx-1.12.2/src/core/nginx.h##修改版本號和名稱,可偽裝成其他服務(例如 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.20

2. 修改用戶與組

[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.confuser nginx nginx; #第二行,取消注釋,修改用戶為nginx,組為nginx[root@localhost nginx-1.12.0]# systemctl restart nginx [root@localhost nginx-1.12.0]# ps aux | grep nginx #主進程由root創(chuàng)建,子進程由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 nginx

3. 緩存時間

??當 nginx 將網(wǎng)頁數(shù)據(jù)返回給客戶端后,可設置緩存的時間,以方便在日后進行相同內(nèi)容的請求時直接返回,避免重復請求,加快了訪問速度。一般針對靜態(tài)網(wǎng)頁進行設置,對動態(tài)網(wǎng)頁不設置緩存時間。

[root@localhost nginx-1.12.0]# vim /usr/local/nginx/conf/nginx.conf......server {......location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ { #新建location,以圖片作為緩存對象root html;expires 1d; #指定緩存時間為1天,一天半為1d12h}......} ......[root@localhost nginx-1.12.0]# systemctl restart nginx

在網(wǎng)頁中加入圖片后測試

[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. 日志分割

編寫腳本

[root@c7-1 /opt]# vim log_cut.sh #!/bin/bash #Filename:log_cut.shlastday=$(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 #創(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 #移動并重命名日志文件kill -USR1 $(cat $pid_path) #重建新日志文件 find $logs_path -mtime +30 -exec rm -rf {} \; #刪除30天之前的日志文件

腳本測試

[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

添加計劃任務

[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.sh

5. 連接超時

??HTTP 有一個 KeepAlive 模式,它告訴 web 服務器在處理完一個請求后保持這個 TCP 鏈接的打開狀態(tài)。若接收到來自同一客戶端的其他請求,服務端會利用這個未被關閉的連接,而不需要再建立一個連接。
??KeepAlive 在一段時間內(nèi)保持打開狀態(tài),它們會在這段時間內(nèi)占用資源,占用過多就會影響性能。

[root@c7-1 ~]#vim /usr/local/nginx/conf/nginx.confhttp { ......keepalive_timeout 65 180;client_header_timeout 80;client_body_timeout 80; ...... }[root@localhost nginx]# systemctl restart nginx

6. 更改進程數(shù)

  • 在高并發(fā)環(huán)境中,需要啟動更多的 nginx 進程以保證快速響應,用以處理用戶的請求,避免造成阻塞
  • 默認情況下,nginx 的多個進程可能更多的跑在一顆 CPU 上,可以分配不同的進程給不同的 CPU 處理,以充分利用 cpu 性能
  • worker_processes 最多開啟 8 個,8 個以上性能就不會再提升了,而且穩(wěn)定性會變低
[root@c7-1 ~]#nproc --all #顯示所有 CPU 數(shù)目 4 [root@c7-1 ~]#nproc #顯示當前進程可用的 CPU 數(shù)目 4 [root@c7-1 ~]#ps aux | grep nginx #查看 nginx 主進程中包含幾個子進程 root 6069 0.0 0.0 20544 608 ? Ss 10:47 0:00 nginx: master process /usr/local/nginx/sbin/nginx nginx 6070 0.0 0.0 23072 1632 ? S 10:47 0:00 nginx: worker process root 6693 0.0 0.0 112728 972 pts/3 S+ 10:56 0:00 grep --color=auto nginx[root@c7-1 ~]#vim /usr/local/nginx/conf/nginx.conf #user nobody; worker_processes 4; #修改為核數(shù)相同或者 2 倍 worker_cpu_affinity 0001 0010 0100 1000#兩個核就是 01 10 ; # 8 個核就是 00000001 00000010 00000100 ...... 01000000 10000000events {worker_connections 4096; #單個工作進程可以允許同時建立外部連接的數(shù)量 }[root@c7-1 ~]#systemctl restart nginx

7. 配置網(wǎng)頁壓縮

??nginx 的 ngx_http_gzip_module 壓縮模塊提供對文件內(nèi)容壓縮的功能。允許 nginx 服務器將輸出內(nèi)容在發(fā)送客戶端之前進行壓縮,以節(jié)約網(wǎng)站帶寬,提升用戶的訪問體驗,默認已經(jīng)安裝,可在配置文件中加入相應的壓縮功能參數(shù)對壓縮性能進行優(yōu)化。

vim /usr/local/nginx/conf/nginx.conf ...... http {......gzip on; #開啟 gzip 壓縮功能gzip_min_length 1k; #壓縮閾值,最小壓縮為 1kgzip_buffers 4 16k; # buffer 大小為 4 個 16k 緩沖區(qū)大小gzip_http_version 1.1; #壓縮版本(默認不設置)gzip_comp_level 6; #壓縮比率,最小為 1,處理速度快,傳輸速度慢,9 最大壓縮比,處理速度慢,傳輸速度快(建議5-6)gzip_disable "MSIE [1-6]\."; #配置禁用 gzip 條件,支持正則,表示 ie6 以下不啟用 gzipgzip_vary on; #支持前端緩存服務器存儲壓縮頁面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;#支持的壓縮類型...... }nginx -t cd /usr/local/nginx/html/ #首頁中插入 cat.jpg 圖片進行測試vim index.html<h1>Welcome to nginx!</h1><img src="test.jpg"/> #插入一行systemctl restart nginx

瀏覽器查看(注意清除瀏覽器緩存)

二、盜鏈與防盜鏈

在企業(yè)網(wǎng)站中,一般都要配置防盜鏈功能,以避免網(wǎng)站內(nèi)容被非法盜用,造成經(jīng)濟損失,也避免不必要的帶寬浪費。

主機IP
nginx 服務端192.168.10.12
盜鏈端192.168.10.20
Windows10192.168.10.10

1. 盜鏈

1.1 nginx 服務端配置

預安裝 nginx 服務 echo "192.168.10.12 www.nginx-server.com" >>/etc/hosts 上傳 test.jpg 到 /usr/local/nginx/html 目錄

1.2 盜鏈主機配置

systemctl stop firewalld && systemctl disable firewalld setenforce 0 ntpdate ntp1.aliyun.com#安裝 httpd 服務 yum -y install httpd systemctl start httpd && systemctl enable httpd#配置臨時 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 httpd

1.3 windows 測試機配置

#在 hosts 文件添加映射,需要管理員權限 C:\Windows\System32\drivers\etc\hosts


1.4 windows 訪問測試

訪問 nginx-server 查看圖片

訪問盜鏈端

2. 防盜鏈

2.1 nginx 服務端配置

修改配置文件,添加如下內(nèi)容

[root@nginx ~]#vim /usr/local/nginx/conf/nginx.confhttp { ......server {......location ~* \.(jpeg|gif|jpg|swf)$ { #匹配不區(qū)分大小寫,以 .jpeg/.gif/.jpg/.swf 結尾的文件valid_referers none blocked *.nginx-server.com nginx-server.com; #設置信任來源可以正常訪問資源if ( $invalid_referer ) {rewrite ^/ http://www.nginx-server.com/daolian.png;#return 403; #如果鏈接的來源域名不在 valid_referers 列表中,則 rewrite 跳轉頁面或者返回 403 頁面}}......} ...... }[root@nginx ~]#nginx -t [root@nginx ~]#systemctl restart nginx上傳 daolian.jpg 圖片到 /usr/local/nginx/html 目錄下

2.2 windows 訪問測試

三、FPM 參數(shù)優(yōu)化

nginx 的 PHP 解析功能實現(xiàn)如果是交由 FPM 處理的,為了提高 PHP 的處理速度,可對 FPM 模塊進行參數(shù)的調整。

  • 首先安裝帶 FPM 模塊的 PHP 環(huán)境,保證 PHP 可以正常運行
  • FPM 進程有兩種啟動方式,由 pm 參數(shù)指定,分別是 static 和 dynamic,前者將產(chǎn)生固定數(shù)據(jù)的 fpm 進程,后者將以動態(tài)的方式產(chǎn)生 fpm 進程
  • static 方式可以使用 pm.max_children 指定啟動的進程數(shù)量。dynamic 方式的參數(shù)則要根據(jù)服務器的內(nèi)存與服務負載進行調整,參數(shù)下所示
選項說明
pm.max_children指定啟動的進程的最大的數(shù)量
pm.start_servers動態(tài)方式下初始的 ftpm 進程數(shù)量
pm.min_spare_servers動態(tài)方式下最小的 fpm 空閑進程數(shù)
pm.max_spare_servers動態(tài)方式下最大的 fpm 空閑進程數(shù)

假設云服務器上運行了個人論壇,內(nèi)存為 1.5 GB ,fpm 進程數(shù)為20,內(nèi)存消耗將近 1GB ,處理速度較慢,需對參數(shù)進行優(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 #將以動態(tài)的方式產(chǎn)生fpm進程 pm.max_children=20 #static模式下空閑進程數(shù)上限,大于下面的值 pm.start_servers = 5 #動態(tài)方式下默認開啟的進程數(shù),在最小和最大之間 pm.min_spare_servers = 2 #動態(tài)方式下最少空閑進程數(shù) pm.max_spare_servers = 8 #動態(tài)方式下最大空閑進程數(shù)#FPM 啟動時有5個進程,最小空閑2個進程,最大空閑8個進程,最多可以有20個進程存在 #重啟 php-fpm [root@server ~]# kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` [root@server ~]# netstat -anpt | grep 9000

總結

以上是生活随笔為你收集整理的Nginx 优化与防盗链的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。