Nginx的ngx http .gzip_ module壓縮模塊提供對文件內容壓縮的功能,允許Nginx服務器將輸出內容在發送客戶端之前進行壓縮,以節約網站帶寬,提升用戶的訪問體驗,默認已經安裝.可在配置文件中加入相應的壓縮功能參數對壓縮性能進行優化
gzip on:開啟gzip壓縮輸出
gzip_ min_ length 1k:用于設置允許壓縮的頁面最小字節數
gzip_ buffers 416k:表示申請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壓縮的頁面
第一步:遠程獲取Windows上的源碼包,并掛載到Linux上
[root@localhost ~]# smbclient -L //192.168.235.1
Enter SAMBA\root's password:
Sharename Type Comment
--------- ---- -------
LNMP Disk
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.235.1/LNMP /abc
Password for root@//192.168.235.1/LNMP:
[root@localhost ~]# [root@localhost ~]# ls /abc
Discuz_X3.4_SC_UTF8.zip nginx-1.12.0.tar.gz
error.png nginx-1.12.2.tar.gz
game.jpg php-7.1.10.tar.bz2
mysql-boost-5.7.20.tar.gz php-7.1.20.tar.gz
第二步:解壓源碼包
[root@localhost ~]# cd /abc
[root@localhost abc]# tar zxvf nginx-1.12.0.tar.gz -C /opt
[root@localhost abc]# ls /opt
nginx-1.12.0 rh
第三步:下載安裝編譯組件包
[root@localhost abc]# cd /opt
[root@localhost opt]# yum install -y \
> gcc \ //C語言
> gcc-c++ \ //c++語言
> pcre-devel \ //pcre語言工具
> zlib-devel //壓縮函數庫
第四步:創建程序用戶并配置Nginx服務相關組件
[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//創建程序用戶nginx,并限定其不可登錄終端
[root@localhost opt]# cd nginx-1.12.0/
[root@localhost nginx-1.12.0]# ./configure \
//配置nginx
> --prefix=/usr/local/nginx \
//指定安裝路徑
> --user=nginx \
//指定用戶名
> --group=nginx \
//指定用戶所屬組
> --with-http_stub_status_module
//安裝狀態統計模塊
第五步:編譯與安裝Nginx
[root@localhost nginx-1.12.0]# make && make install
第六步:優化Nginx服務啟動腳本,并建立命令軟連接
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
//創建nginx服務命令軟鏈接到系統命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service
//關閉防火墻
[root@localhost nginx-1.12.0]# setenforce 0
//關閉增強型安全功能
[root@localhost nginx-1.12.0]# nginx
//輸入nginx 開啟服務
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80 //查看服務的80 端口,顯示已開啟
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7520/nginx: master
第七步:systemctl管理nginx腳本
[root@localhost ~]# vim /lib/systemd/system/nginx.service ##創建配置文件
[Unit]
Description=nginx ##描述
After=network.target ##描述服務類型
[Service]
Type=forking ##后臺運行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx ##啟動服務
ExecReload=/usr/bin/kill -s HUP $MAINPID ##根據PID重載配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ##根據PID終止進程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service ##設置執行權限
[root@localhost ~]# systemctl stop nginx.service ##關閉nginx
[root@localhost ~]# systemctl start nginx.service ##開啟nginx
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
gzip on;
#使用x鍵刪除此行前的井號注釋
gzip_min_length 1k;
#壓縮閾值
gzip_buffers 4 16k;
#buffers大小為4個16k緩沖區大小
gzip_http_version 1.1;
##壓縮版本號
gzip_comp_level 6;
#壓縮比率,最小為1,處理快但傳輸慢;最大為9,處理慢,但傳輸快;此處設6,相對適中
gzip_types text/plain application/x-javascript text/css image/jpg image/jpegimage/png image/gif application/xml text/javascript application/x-httpd-php
application/javascript application/json;
#支持的類型格式類型
gzip_disable "MSIE [1-6]\.";
#配置禁用gzip條件,支持正則表達式,表示ie6以下不啟用gzip
gzip_vary on;
#讓前端的緩存服務器緩存經過gzip壓縮的頁面
第一步:復制圖片到站點目錄
[root@localhost conf]# cd ../html/
[root@localhost html]# cp /abc/game.jpg ./
[root@localhost html]# ls
50x.html game.jpg index.html
第二步:修改站點首頁內容
[root@localhost html]# vim index.html
<h2>Welcome to nginx!</h2>
<img src="game.jpg"/>
##在h2標簽下添加圖片路徑
[root@localhost html]# systemctl stop nginx.service
[root@localhost html]# systemctl start nginx.service
[root@localhost html]# systemctl stop firewalld.service
[root@localhost html]# setenforce 0
在客戶機中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網頁

[root@localhost ~]# yum install bind -y
[root@localhost ~]# vim /etc/named.conf
options {
listen-on port 53 { any; };
...
allow-query { any; };
[root@localhost ~]# vim /etc/named.rfc1912.zones
zone "accp.com" IN { type master;
file "accp.com.zone";
allow-update { none; };
};
[root@localhost ~]# cd /var/named/
[root@localhost named]# cp -p named.localhost accp.com.zone
#復制模板文件
[root@localhost named]# vim accp.com.zone
$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.235.158
#IP地址指向本機地址
[root@localhost named]# systemctl start named
[root@localhost named]# systemctl stop firewalld.service
[root@localhost named]# setenforce 0
1.創建一個網頁內容,修改后綴為index.html
<html>
<head>
<title>云計算</title>
</head>
<body>
<h2>this is test web</h2>
<img src="http://www.accp.com/game.jpg"/>
</body>
</html>
2.搭建網站
3.修改首選DNS服務器地址



[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|swf)$ {
#支持格式
valid_referers none blocked *.accp.com accp.com;
#允許用accp.com為后綴訪問
if ( $invalid_referer ) {
#判斷是否被盜鏈
rewrite ^/ http://www.accp.com/error.png;
#判定被盜鏈就跳轉錯誤圖片
}
}
[root@localhost named]# cp /abc/error.png /usr/local/nginx/html/
[root@localhost named]# systemctl stop nginx
[root@localhost named]# systemctl start nginx

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。