溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx優(yōu)化--網(wǎng)頁壓縮與緩存時(shí)間

發(fā)布時(shí)間:2020-06-16 11:19:53 來源:網(wǎng)絡(luò) 閱讀:418 作者:wx5d3faba330584 欄目:系統(tǒng)運(yùn)維

一、網(wǎng)頁壓縮

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

gzip on:開啟gzip壓縮輸出

gzip_ min_ length 1k:用于設(shè)置允許壓縮的頁面最小字節(jié)數(shù)

gzip_ buffers 416k:表示申請4個(gè)單位為16k的內(nèi)存作為壓縮結(jié)果流緩存,默認(rèn)值是申請與原始數(shù)據(jù)大小相同的內(nèi)存空間來儲gzip壓縮結(jié)果

zip_ http_ version 1.0:用于設(shè)置識別http協(xié)議版本,默認(rèn)是1.1, 目前大部分瀏覽器已經(jīng)支持gzip解壓,但處理最慢,也比較消耗服務(wù)器CPU資源

gzip_ _comp_ level 2:用來指定gzip壓縮比,1壓縮比最小,處理速度最快; 9壓縮比最大,傳輸速度快,但處理速度最慢,使用默認(rèn)即可

gzip_ types text/plain:壓縮類型,是就對哪些網(wǎng)頁文檔啟用壓縮功能

gzip_ vary on:選項(xiàng)可以讓前端的緩存服務(wù)器緩存經(jīng)過gzip壓縮的頁面

壓縮實(shí)例演示
一、編譯安裝Nginx服務(wù)
第一步:遠(yuǎn)程獲取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        //壓縮函數(shù)庫

3、創(chuàng)建程序用戶并配置Nginx服務(wù)相關(guān)組件

[root@localhost opt]# useradd -M -s /sbin/nologin nginx
//創(chuàng)建程序用戶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
//安裝狀態(tài)統(tǒng)計(jì)模塊

4、編譯與安裝Nginx

[root@localhost nginx-1.12.0]# make && make install

5、優(yōu)化Nginx服務(wù)啟動(dòng)腳本,并建立命令軟連接

[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ 
//創(chuàng)建nginx服務(wù)命令軟鏈接到系統(tǒng)命令
[root@localhost nginx-1.12.0]# systemctl stop firewalld.service 
//關(guān)閉防火墻
[root@localhost nginx-1.12.0]# setenforce 0
//關(guān)閉增強(qiáng)型安全功能
[root@localhost nginx-1.12.0]# nginx 
//輸入nginx 開啟服務(wù)
[root@localhost nginx-1.12.0]# netstat -ntap | grep 80      //查看服務(wù)的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      ##創(chuàng)建配置文件

[Unit]
Description=nginx                                            ##描述
After=network.target                                        ##描述服務(wù)類型
[Service]
Type=forking                                                    ##后臺運(yùn)行形式
PIDFile=/usr/local/nginx/logs/nginx.pid            ##PID文件位置
ExecStart=/usr/local/nginx/sbin/nginx              ##啟動(dòng)服務(wù)
ExecReload=/usr/bin/kill -s HUP $MAINPID    ##根據(jù)PID重載配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID       ##根據(jù)PID終止進(jìn)程
PrivateTmp=true
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service     ##設(shè)置執(zhí)行權(quán)限
[root@localhost ~]# systemctl stop nginx.service       ##關(guān)閉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個(gè)16k緩沖區(qū)大小

gzip_http_version 1.1;
##壓縮版本號

gzip_comp_level 6;
#壓縮比率,最小為1,處理快但傳輸慢;最大為9,處理慢,但傳輸快;此處設(shè)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條件,支持正則表達(dá)式,表示ie6以下不啟用gzip

gzip_vary on;
#讓前端的緩存服務(wù)器緩存經(jīng)過gzip壓縮的頁面

8、Nginx網(wǎng)頁中放入圖片,復(fù)制圖片到站點(diǎn)目錄

[root@localhost conf]# cd ../html/
[root@localhost html]# cp /abc/game.jpg ./
[root@localhost html]# ls
50x.html  game.jpg  index.html

9、修改站點(diǎn)首頁內(nèi)容

[root@localhost html]# vim index.html

<h2>Welcome to nginx!</h2>
<img src="game.jpg"/>
##在h2標(biāo)簽下添加圖片路徑

[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虛擬機(jī)驗(yàn)證網(wǎng)頁圖片壓縮
在客戶機(jī)中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網(wǎng)頁
Nginx優(yōu)化--網(wǎng)頁壓縮與緩存時(shí)間

二、網(wǎng)頁緩存時(shí)間

當(dāng)Nginx將網(wǎng)頁數(shù)據(jù)返回給客戶端后,可設(shè)置緩存的時(shí)間,以方便在日后進(jìn)行相同內(nèi)容的請求時(shí)直接返回,避免重復(fù)請求,加快了訪問速度般針對靜態(tài)網(wǎng)頁設(shè)置,對動(dòng)態(tài)網(wǎng)頁不設(shè)置緩存時(shí)間,可在Windows客戶端中使用fiddler查看網(wǎng)頁緩存時(shí)間
設(shè)置方法
可修改配置文件,在http段、 或者server段、 或者location段加入對特定內(nèi)容的過期參數(shù)
示例
修改Nginx的配置文件,在location段加入expires參數(shù)

location ~ \.(gifjpgliepglpnglbmplico)$ {
root html;
expires 1d;

網(wǎng)頁緩存時(shí)間實(shí)例演示
1、復(fù)制圖片到站點(diǎn)目錄

[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網(wǎng)頁

[root@localhost html]# vim index.html

<h2>Welcome to nginx!</h2>
<img src="game.jpg"/>
##在h2標(biāo)簽下添加圖片路徑

3、修改Nginx .conf文件

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf

user nginx nginx;
##單獨(dú)輸入此行條目,指定用戶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虛擬機(jī)驗(yàn)證
在客戶機(jī)中安裝fiddler.exe抓包軟件,并打開瀏覽器訪問192.168.235.158網(wǎng)頁
Nginx優(yōu)化--網(wǎng)頁壓縮與緩存時(shí)間

以上就是今天所有的知識點(diǎn),小伙伴們要牢記哦!??!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI