溫馨提示×

溫馨提示×

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

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

如何分析Apache服務(wù)mod_expires模塊

發(fā)布時間:2022-01-05 18:33:17 來源:億速云 閱讀:119 作者:柒染 欄目:云計算

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何分析Apache服務(wù)mod_expires模塊,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

Apache服務(wù)mod_expires緩存模塊

一:mod_expires模塊介紹

此模塊是允許通過Apache配置文件控制HTTP的“expires”和“cache-control”頭的內(nèi)容,用于控制服務(wù)器應(yīng)答時的“expires”頭的內(nèi)容和“cache-control”頭的max-age的指令,這個有效期可以設(shè)置為對于源文件最后的修改時間或客戶端訪問的時間

這些HTTP頭向客戶端表明了內(nèi)容有效性與持久性,如果客戶端本地有緩存,則用戶再次訪問時讀取的內(nèi)容就是從緩存里讀取的(緩存沒有失效的情況下),而不是從服務(wù)器端讀取到的,客戶端還會檢查緩存中的內(nèi)容,看看是不是需要從服務(wù)器端進(jìn)行更新,從而增加用戶的體驗度,減少服務(wù)器的壓力,實際生產(chǎn)環(huán)境中也是調(diào)優(yōu)參數(shù)之一

二:檢查與安裝模塊

直接編譯安裝查看如下

[root@Centos modules]# /application/apache/bin/apachectl -l|grep mod_expi

mod_expires.c

以DSO方式編譯安裝查看如下

[root@Centos ~]# ll /application/apache/modules/|grep expir

-rwxr-xr-x. 1 root root  37799 Sep 12 03:30 mod_expires.so

但是以上兩種方式不能同時安裝,否則會出現(xiàn)錯誤

查看幫助

[root@Centos httpd-2.4.23]# ./configure --help |grep expir

  --enable-expires        Expires header control

具體編譯命令如下

[root@Centos httpd-2.4.23]# ./configure\

--enable-expires

以DSO的方式編譯安裝如下

cd /Downloads/tools/httpd2.2.24/modules/metadata/

/application/apache/bin/apxs -c -i -a mod_expires.c

參數(shù)說明

-a 此選項會自動增加一個LoadModule行到httpd.conf文件中,來激活模塊,如果此行已存在,則啟用

-c 此選項表示需要執(zhí)行編譯操作

-i 此選項表示需要執(zhí)行安裝操作,以安裝一個或多個動態(tài)共享對象到服務(wù)器modules目錄中

[root@Centos modules]# cd /Downloads/tools/httpd-2.4.23/modules/metadata/

[root@Centos metadata]# /application/apache/bin/apxs -a -c -i mod_expires.c

處程省略

----------------------------------------------------------------------

chmod 755 /application/apache2.4.23/modules/mod_expires.so

[activating module `expires' in /application/apache2.4.23/conf/httpd.conf]

三:配置mod_expires模塊

1、查看當(dāng)前的 http headers信息

[root@Centos ~]# curl -I http://blog.abc.com:9999/ 

HTTP/1.1 200 OK

Date: Sun, 18 Sep 2016 03:40:44 GMT

Server: Apache/2.4.23 (Unix)

Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT

ETag: "1f-53c122a061992"

Accept-Ranges: bytes

Content-Length: 31

Content-Type: text/html

2、配置模塊

模塊配置有兩種模式:主配置里面配置與單個虛擬主機(jī)配置文件里配置

配置HTTP主配置文件如下

[root@Centos conf]# ls

extra  httpd.conf  httpd.conf.bak  index.html  magic  mime.types  original

[root@Centos conf]# cp httpd.conf httpd.conf.2016-09-09

[root@Centos conf]# vi httpd.conf

前面的內(nèi)容省略

<Directory "/data/www/bbs">

    Options FollowSymLinks

    AllowOverride None

    Require all granted

</Directory>

<Directory "/data/www/blog">

    Options FollowSymLinks

    AllowOverride None

    Require all granted

</Directory>

ExpiresActive on

    ExpiresDefault "access plus 12 month"

    ExpiresByType text/css "access plus 12 month"

    ExpiresByType image/gif "access plus 12 month"

    ExpiresByType image/jpge "access plus 12 month"

    ExpiresByType image/jpg "access plus 12 month"

    ExpiresByType image/png "access plus 12 month"

    ExpiresByType application/x-shockwave-flash "access plus 12 month"

    ExpiresByType application/x-javascript "access plus 12 month"

    ExpiresByType video/x-flv "access plus 12 month"

"httpd.conf" 513L, 18662C written

檢查語法并重啟服務(wù)

[root@Centos conf]# /application/apache/bin/apachectl -t

Syntax OK

[root@Centos conf]# /application/apache/bin/apachectl graceful

檢查進(jìn)程與端口

[root@Centos conf]# ps -ef|grep http

root       1717      1  0 09:45 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

daemon     5319   1717  0 12:13 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

daemon     5320   1717  0 12:13 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

daemon     5321   1717  0 12:13 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

root       5404   5022  0 12:13 pts/0    00:00:00 grep http

[root@Centos conf]# lsof -i tcp:9999

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   1717   root    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

httpd   5319 daemon    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

httpd   5320 daemon    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

httpd   5321 daemon    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

[root@Centos conf]# lsof -i tcp:8888

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   1717   root        6u  IPv6  14444      0t0  TCP *:ddi-tcp-1 (LISTEN)

httpd   5319 daemon    6u  IPv6  14444      0t0  TCP *:ddi-tcp-1 (LISTEN)

httpd   5320 daemon    6u  IPv6  14444      0t0  TCP *:ddi-tcp-1 (LISTEN)

httpd   5321 daemon    6u  IPv6  14444      0t0  TCP *:ddi-tcp-1 (LISTEN)

上傳一張圖片到站點(diǎn)目錄

[root@Centos blog]# rz

rz waiting to receive.

Starting zmodem transfer.  Press Ctrl+C to cancel.

Transferring 20160918093614.jpg...

  100%      49 KB   49 KB/s 00:00:01       0 Errors

[root@Centos blog]# ll

total 56

-rw-r--r--. 1 root root 50657 Sep 18 09:36 20160918093614.jpg

-rw-r--r--. 1 root root    31 Sep  9 20:19 index.html

本地客戶端瀏覽器訪問

http://blog.abc.com:9999/20160918093614.jpg

可以正常顯示上傳的圖片信息

[root@Centos blog]# curl -I http://blog.abc.com:9999

HTTP/1.1 200 OK

Date: Sun, 18 Sep 2016 04:21:24 GMT

Server: Apache/2.4.23 (Unix)

Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT

ETag: "1f-53c122a061992"

Accept-Ranges: bytes

Content-Length: 31

Cache-Control: max-age=31104000

Expires: Wed, 13 Sep 2017 04:21:24 GMT     

#發(fā)現(xiàn)沒有,這就是緩存的時間期限

Content-Type: text/html

[root@Centos blog]# date

Sun Sep 18 12:21:36 CST 2016

說明如果配置HTTP主配置文件,則全局生效,虛擬主機(jī)配置文件則不需要配置

配置單個虛擬主機(jī)文件如下

#port bash name

<VirtualHost *:9999>

    ServerAdmin admini@abc.com

    DocumentRoot "/data/www/blog"

    ServerName blog.abc.com

    ServerAlias blog1.com

    ErrorLog "logs/bbs-error_log"

    CustomLog "logs/bbs-access_log" common

    ExpiresActive on

    ExpiresDefault "access plus 12 month"

    ExpiresByType text/css "access plus 12 month"

    ExpiresByType image/gif "access plus 12 month"

    ExpiresByType image/jpge "access plus 12 month"

    ExpiresByType image/jpg "access plus 12 month"

    ExpiresByType image/png "access plus 12 month"

    ExpiresByType application/x-shockwave-flash "access plus 12 month"

    ExpiresByType application/x-javascript "access plus 12 month"

    ExpiresByType video/x-flv "access plus 12 month"

"httpd-vhosts.conf" 50L, 1821C written

檢查語法并重啟服務(wù)

[root@Centos extra]# /application/apache/bin/apachectl -t

Syntax OK

[root@Centos extra]# /application/apache/bin/apachectl graceful

查看進(jìn)程與端口

[root@Centos extra]# ps -ef |grep http

root       1717      1  0 09:45 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

daemon     5088   1717  0 11:52 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

daemon     5089   1717  0 11:52 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

daemon     5090   1717  0 11:52 ?        00:00:00 /application/apache2.4.23/bin/httpd -k start

root       5176   5022  0 11:53 pts/0    00:00:00 grep http

[root@Centos extra]# lsof -i tcp:9999

COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

httpd   1717   root    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

httpd   5088 daemon    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

httpd   5089 daemon    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

httpd   5090 daemon    8u  IPv6  14448      0t0  TCP *:distinct (LISTEN)

上傳一張圖片到站點(diǎn)目錄

[root@Centos blog]# rz

rz waiting to receive.

Starting zmodem transfer.  Press Ctrl+C to cancel.

Transferring 20160918093614.jpg...

  100%      49 KB   49 KB/s 00:00:01       0 Errors

[root@Centos blog]# ll

total 56

-rw-r--r--. 1 root root 50657 Sep 18 09:36 20160918093614.jpg

-rw-r--r--. 1 root root    31 Sep  9 20:19 index.html

本地客戶端瀏覽器訪問

http://blog.abc.com:9999/20160918093614.jpg

可以正常顯示上傳的圖片信息

查看緩存信息

[root@Centos blog]# curl -I http://blog.abc.com:9999

HTTP/1.1 200 OK

Date: Sun, 18 Sep 2016 04:01:36 GMT

Server: Apache/2.4.23 (Unix)

Last-Modified: Fri, 09 Sep 2016 12:19:55 GMT

ETag: "1f-53c122a061992"

Accept-Ranges: bytes

Content-Length: 31

Cache-Control: max-age=31104000

Expires: Wed, 13 Sep 2017 04:01:36 GMT

Content-Type: text/html

[root@Centos blog]# date

Sun Sep 18 12:02:20 CST 2016

初步計算應(yīng)該是360天的樣子,說明配置是正確的

[root@Centos extra]# curl -I https://www.baidu.com/img/bd_logo1.png

HTTP/1.1 200 OK

Server: bfe/1.0.8.18

Date: Sun, 18 Sep 2016 07:07:04 GMT

Content-Type: image/png

Content-Length: 7877

Connection: keep-alive

P3P: CP=" OTI DSP COR IVA OUR IND COM "

Set-Cookie: BAIDUID=01FAEEFF2AF063514442EEC0D5D83A68:FG=1; expires=Mon, 18-Sep-17 07:07:04 GMT; max-age=31536000; path=/; domain=.baidu.com; version=1

Last-Modified: Wed, 03 Sep 2014 10:00:27 GMT

ETag: "1ec5-502264e2ae4c0"

Accept-Ranges: bytes

Cache-Control: max-age=315360000

Expires: Wed, 16 Sep 2026 07:07:04 GMT

百度的LOG圖片緩存有效期約為十年,其它的有興趣可以自己去看

四:mod_expires模塊的優(yōu)點(diǎn)

1、提升用戶對訪問網(wǎng)站的體驗度

由于一些文件緩存在本地,訪問速度提升了,用戶體驗也就相應(yīng)的提升了

2、減少服務(wù)器帶寬與負(fù)載壓力

由于用戶訪問時是讀取本地緩存的文件內(nèi)容,減少了與服務(wù)器之間的交互,從而減輕服務(wù)器壓力

3、節(jié)約維護(hù)服務(wù)器的成本

和上述一樣,服務(wù)器壓力小了,維護(hù)人員也會相應(yīng)的減少,服務(wù)器配件配置更新的速度也會相應(yīng)的慢下來

上述就是小編為大家分享的如何分析Apache服務(wù)mod_expires模塊了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

AI