配置Nginx實現網頁壓縮功能
Nginx的ngx_http_gzip_module壓縮模塊提供對文件內容壓縮的功能,允許Nginx服務器將輸出內容在發送客戶端之前進行壓縮,以節約網站帶寬,提升用戶的訪問體驗,默認已經安裝。可在配置文件中加入相應的壓縮功能參數對壓縮性能進行優化。
壓縮功能參數講解
- gzip on:開啓gzip壓縮輸出
- gzip_min_length 1k:用於設置允許壓縮的頁面最小字節數
- gzip_buffers 4 16k:表示申請4個單位爲16k的內存作爲壓縮結果流緩存,默認值是申請與原始數據大小相同的內存空間來存儲gzip壓縮結果
- zip_http_version 1.0:用於設置識別http協議版本,默認是1.1,目前大部分瀏覽器已經支持gzip解壓,但處理最慢,也比較消耗服務器CPU資源
- gzip_comp_level 2:用來指定gzip壓縮比,1壓縮比最小,處理速度最快;9壓縮比最大,傳輸速度快,但處理速度最慢,使用默認即可
- gzip_types text/plain:壓縮類型,是就對哪些網頁文檔啓用壓縮功能
- gzip_vary on:選項可以讓前端的緩存服務器緩存經過gzip壓縮的頁面
1.關閉防火牆和增強性安全功能
[root@localhost ~]# systemctl stop firewalld.service //關閉防火牆
[root@localhost ~]# setenforce 0 //關閉增強性安全功能
[root@localhost ~]#
2.用win10虛擬機訪問nginx服務,並用fiddler工具抓包(未開啓壓縮)
3.修改配置文件開啓壓縮功能
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css image/jpeg image/png image/gif application/xml text/javascript application/x-httpd-php application/javasrcipt application/json;
gzip_disable "MSIE [1-6]\."; //IE瀏覽器6版本以上開啓壓縮功能
gzip_vary on;
[root@localhost ~]# service nginx restart //重啓服務
[root@localhost ~]#
4.再次用win10虛擬機訪問nginx服務,並用fiddler工具抓包(壓縮功能開啓)
配置Nginx實現防盜鏈
- 在企業網站服務中,一般都要配置防盜鏈功能,以避免網站內容被非法盜用,造成經濟損失
- Nginx防盜鏈功能也非常強大。默認情況下,只需要進行簡單的配置,即可實現防盜鏈處理
1.將網站圖片與防盜鏈圖片複製到站點目錄下
[root@localhost ~]# mkdir /mnt/tools
[root@localhost ~]# mount.cifs //192.168.100.50/tools /mnt/tools/
Password for root@//192.168.100.50/tools:
[root@localhost ~]# cp /mnt/tools/forbid.png /usr/local/nginx/html/ //防盜鏈圖片
[root@localhost ~]# cp picture.jpg /usr/local/nginx/html/ //網站圖片
[root@localhost ~]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html forbid.png index.html picture.jpg
[root@localhost html]#
2.在首頁文件添加圖片引用
[root@localhost html]# vim index.html
<h1>Welcome to nginx!</h1>
<img src="picture.jpg"/> //添加
[root@localhost html]#
3.安裝dns服務
[root@localhost html]# yum install bind -y
..........//省略安裝過程
[root@localhost html]#
4.修改主配置文件
[root@localhost html]# vim /etc/named.conf
options {
listen-on port 53 { any; }; //將127.0.0.1改爲any
listen-on-v6 port 53 { ::1; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
recursing-file "/var/named/data/named.recursing";
secroots-file "/var/named/data/named.secroots";
allow-query { any; }; //將localhost改爲any
[root@localhost html]#
5.修改區域配置文件
[root@localhost html]# vim /etc/named.rfc1912.zones
zone "abc.com" IN { //添加區域
type master;
file "abc.com.zone";
allow-update { none; };
};
[root@localhost html]#
6.修改區域數據配置文件
[root@localhost html]# cd /var/named/
[root@localhost named]# ls
data dynamic named.ca named.empty named.localhost named.loopback slaves
[root@localhost named]# cp -p named.localhost abc.com.zone //複製一份模板並重命名
[root@localhost named]#
$TTL 1D
@ IN SOA @ rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
A 127.0.0.1
www IN A 192.168.52.131 //添加解析地址
[root@localhost named]# systemctl start named //開啓服務
[root@localhost named]#
7.在win10-1主機新建文本文檔輸入以下內容,並重命名爲index.html
<html>
<head>
<title>盜鏈網站</title>
</head>
<body>
<h1>this is test web</h1>
<img src="http:www.abc.com/picture.jpg"/> //盜鏈路徑
</body>
</html>
8.按下列步驟開啓win10-1系統的web服務
9.將文件index.html移動到C盤的inetpub目錄的子目錄wwwroot下
10.分別將搭建盜鏈網站的win10-1主機和測試主機win10-2的dns地址改爲Linux虛擬機地址
11.查看win10-1主機的IP地址
12.用win10-2主機分別訪問nginx服務站點和win10-1主機的web服務,成功盜鏈nginx服務站點的圖片
13.修改nginx服務配置文件,開啓防盜鏈功能
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|swf)$ {
valid_referers none blocked *.abc.com abc.com;
if ( $invalid_referer ) {
rewrite ^/ http://www.abc.com/forbid.png;
}
}
[root@localhost named]# service nginx restart
[root@localhost named]#