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壓縮的頁面
壓縮實例演示
一、編譯安裝Nginx服務
第一步:遠程獲取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
2、解壓源碼包、下載組件
[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 //壓縮函數庫
3、創建程序用戶并配置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
//安裝狀態統計模塊
4、編譯與安裝Nginx
[root@localhost nginx-1.12.0]# make && make install
5、優化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
6、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
7、修改Nginx.conf文件
[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壓縮的頁面
8、Nginx網頁中放入圖片,復制圖片到站點目錄
[root@localhost conf]# cd ../html/
[root@localhost html]# cp /abc/game.jpg ./
[root@localhost html]# ls
50x.html game.jpg index.html
9、修改站點首頁內容
[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
10、打開一臺Win10虛擬機驗證網頁圖片壓縮
在客戶機中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網頁
當Nginx將網頁數據返回給客戶端后,可設置緩存的時間,以方便在日后進行相同內容的請求時直接返回,避免重復請求,加快了訪問速度般針對靜態網頁設置,對動態網頁不設置緩存時間,可在Windows客戶端中使用fiddler查看網頁緩存時間
設置方法
可修改配置文件,在http段、 或者server段、 或者location段加入對特定內容的過期參數
示例
修改Nginx的配置文件,在location段加入expires參數
location ~ \.(gifjpgliepglpnglbmplico)$ {
root html;
expires 1d;
網頁緩存時間實例演示
1、復制圖片到站點目錄
[root@localhost nginx-1.12.0]# ls /abc
Discuz_X3.4_SC_UTF8.zip 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
nginx-1.12.0.tar.gz
[root@localhost nginx-1.12.0]# cp /abc/game.jpg /usr/local/nginx/html/
[root@localhost nginx-1.12.0]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html game.jpg index.html
2、修改Nginx的index.html網頁
[root@localhost html]# vim index.html
<h2>Welcome to nginx!</h2>
<img src="game.jpg"/>
##在h2標簽下添加圖片路徑
3、修改Nginx .conf文件
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
user nginx nginx;
##單獨輸入此行條目,指定用戶nginx,指定組nginx
location ~\.(gif|jepg|jpg|ico|bmp|png)$ {
root html;
expires 1d;
##上述圖片類型圖片緩存一天
}
[root@localhost html]# systemctl stop nginx.service
[root@localhost html]# systemctl start nginx.service
4、打開一臺Win10虛擬機驗證
在客戶機中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網頁
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。