1. 程式人生 > 其它 >webuploader上傳資料夾程式碼

webuploader上傳資料夾程式碼

Nginx優化與防盜鏈

一、隱藏版本號

  • 可以使用 Fiddler 工具抓取資料包,檢視 Nginx版本
  • 也可以在 CentOS 中使用命令 curl -I http://192.168.0.102 顯示響應報文首部資訊
curl -I http://192.168.70.20

方法一:修改配置檔案方式

vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;                                #新增,關閉版本號
    ......
}

通過瀏覽器驗證

命令列直接輸出curl -I http://192.168.70.20 驗證

systemctl restart nginx
curl -I http://192.168.70.20

方法二:修改原始碼檔案,重新編譯安裝

vim /opt/nginx-1.12.0/src/core/nginx.h
#define NGINX_VERSION "1.1.1"                     #修改版本號
#define NGINX_VER "IIS" NGINX_VERSION             #修改伺服器型別

cd /opt/nginx-1.12.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
make && make install
vim /usr/local/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens on;
    ......
}

systemctl restart nginx.service
curl -I http://192.168.70.20

二、修改使用者與組

vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;                                 #取消註釋,修改使用者為 nginx ,組為 nginx

systemctl restart nginx

ps aux 
| grep nginx 主程序由root建立,子程序由nginx建立

三、快取時間

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天
        }
......
    }
}

systemctl restart nginx.service

瀏覽器訪問

  • 在Linux系統中,開啟火狐瀏覽器,右擊點檢視元素
  • 選擇 網路 —> 選擇 HTML、WS、其他
  • 訪問 http://192.168.70.20 ,雙擊200響應訊息檢視響應頭中包含 Cahce-Control:max-age=86400 表示快取時間是 86400 秒。也就是快取一天的時間,一天之內瀏覽器訪問這個頁面,都是用快取中的資料,而不需要向 Nginx 伺服器重新發出請求,減少了伺服器的使用頻寬。

四、日誌切割

vi /opt/fenge.sh
#!/bin/bash

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-$d        #移動並重命名日誌檔案
kill -USR1 $(cat $pid_path)                                                    #重建新日誌檔案
find $logs_path -mtime +30 -exec rm -rf {} \;                                #刪除30天之前的日誌檔案
#find $logs_path -mtime +30 |xargs rm -rf 

chmod +x /opt/fenge.sh
/opt/fenge.sh
ls /var/log/nginx
ls /usr/local/nginx/logs/access.log 

crontab -e
0 1 * * * /opt/fenge.sh

補充:在linux作業系統中,每個檔案都有很多的時間引數,其中有三個比較主要,分別是ctime,atime,mtime

  • ctime(status time):當修改檔案的許可權或者屬性的時候,就會更新這個時間,ctime並不是create time,更像是change time,只有當更新檔案的屬性或者許可權的時候才會更新這個時間,但是更改內容的話是不會更新這個時間。
  • atime(accesstime):當使用這個檔案的時候就會更新這個時間。
  • mtime(modification time):當修改檔案的內容資料的時候,就會更新這個時間,而更改許可權或者屬性,mtime不會改變,這就是和ctime的區別。

五、連線超時

  • HTTP有一個KeepAlive模式,它告訴web伺服器在處理完一個請求後保持這個TCP連線的開啟狀態。若接收到來自客戶端的其它請求,服務端會利用這個未被關閉的連線,而不需要再建立一個連線
  • KeepAlive 在一段時間內保持開啟狀態,它們會在這段時間內佔用資源。佔用過多就會影響效能
vim /usr/local/nginx/conf/nginx.conf
http {
...... 
    keepalive_timeout 65 180;
    client_header_timeout 80;
    client_body_timeout 80;
...... 
}

systemctl restart nginx.service
keepalive_timeout
指定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 返回 HTTP 408(Request Timed Out)。

client_body_timeout
指定客戶端與服務端建立連線後傳送 request body 的超時時間。如果客戶端在指定時間內沒有傳送任何內容,Nginx 返回 HTTP 408(Request Timed Out)。

六、更改程序數

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處理,程序數配為4時0001 0010 0100 1000

systemctl restart nginx

七、配置網頁壓縮

  • Nginx的ngx_http_gzip_module壓 縮模組提供對檔案內容壓縮的功能
  • 允許Nginx伺服器將輸出內容在傳送客戶端之前進行壓縮,以節約網站頻寬,提升使用者的訪問體驗,預設已經安裝
  • 可在配置檔案中加入相應的壓縮功能引數對壓縮效能進行優化
vim /usr/local/nginx/conf/nginx.conf
http {
...... 
   gzip on;                         # 取消註釋,開啟gzip壓縮功能
   gzip_min_length 1k;              # 最小壓縮檔案大小
   gzip_buffers 4 16k;              # 壓縮緩衝區,大小為4個16k緩衝區
   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;        #壓縮型別,表示哪些網頁文件啟用壓縮功能
...... 
}
cd /usr/local/nginx/html
先將game.jpg檔案傳到/usr/local/nginx/html目錄下
vim index.html
...... 
<img src="game.jpg"/>                #網頁中插入圖片
</body>
</html>

systemctl restart nginx

重啟服務後瀏覽器驗證

在Linux系統中,開啟火狐瀏覽器,右擊點檢視元素
選擇 網路 ---> 選擇 HTML、WS、其他 
訪問 http://192.168.70.20 ,雙擊200響應訊息檢視響應頭中包含 Content-Encoding: gzip

八、配置防盜鏈

vim /usr/local/nginx/conf/nginx.conf
http {
......
    server {
    ......
        location ~*\.(jpg|gif|swf)$ {
            valid_referers *.kgc.com kgc.com;
            if ( $invalid_referer ) {
                rewrite ^/ http://www.kgc.com/error.png;
                #return 403;
            }
        }
    ......
    }
}
~* \.(jpg|gif|swf)$ :這段正則表示式表示匹配不區分大小寫,以.jpg 或.gif 或.swf 結尾的檔案;

valid_referers :設定信任的網站,可以正常使用圖片;

後面的網址或者域名 :referer 中包含相關字串的網址;

if語句:如果連結的來源域名不在valid_referers所列出的列表中,$invalid_referer為1,則執行後面的操作,即進行重寫或返回 403 頁面。
網頁準備:
Web源主機(192.168.80.10)配置:
cd /usr/local/nginx/html
將game.jpg、error.png檔案傳到/usr/local/nginx/html目錄下
vim index.html
...... 
<img src="game.jpg"/>
</body>
</html>

echo "192.168.80.10 www.kgc.com" >> /etc/hosts 
echo "192.168.80.11 www.benet.com" >> /etc/hosts 

盜鏈網站主機(192.168.80.11):
cd /usr/local/nginx/html
vim index.html
...... 
<img src="http://www.kgc.com/game.jpg"/>
</body>
</html>

echo "192.168.80.10 www.kgc.com" >> /etc/hosts 
echo "192.168.80.11 www.benet.com" >> /etc/hosts 

準備一個防盜鏈網站

盜鏈網站主機(192.168.70.10)

在主機上課檢視到盜鏈accp

配置防盜鏈(192.168.70.20)