您想学习如何配置Apache Browser Caching功能吗? 在本教程中,我们将向您展示在运行Ubuntu Linux的计算机上配置Apache Browser Caching功能所需的所有步骤。

•Ubuntu版本:18.04

硬件清单:

以下部分介绍了用于创建此Wordpress教程的设备列表。

上面列出的每一件硬件都可以在亚马逊网站上找到。

WordPress Playlist:

在此页面上,我们可以快速访问与Wordpress安装相关的视频列表。

不要忘记订阅我们命名的youtube频道 FKIT.

WordPress相关教程:

在此页面上,我们提供了与Wordpress安装相关的教程列表的快速访问。

教程 - Apache浏览器缓存配置

首先,您需要安装Apache Web服务器。

在Linux控制台上,使用以下命令安装Apache。

# apt-get update
# apt-get install apache2

现在,您需要启用以下Apache模块:

•mod_headers
•mod_expires

使用以下命令启用mod_headers和mod_expires。

# a2enmod headers
# a2enmod expires

# systemctl restart apache2

编辑apache2.conf配置文件。

# vi /etc/apache2/apache2.conf

在文件末尾,输入所需的浏览器缓存配置。

例如,这是我们的配置。

<IfModule mod_expires.c>
ExpiresActive On
FileETag None
ExpiresDefault "access plus 14 days"
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType text/css "now plus 1 month"
ExpiresByType image/ico "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 month"
ExpiresByType text/html "access plus 1 days"
</IfModule>

要激活新配置,需要重新启动Apache。

# systemctl restart apache2

请记住,您需要指定所需的浏览器缓存时间以满足您的需求。

测试 - Apache浏览器缓存配置

要测试浏览器缓存配置,让我们创建一个带有图像的基本HTML页面

# cd /var/www/html
# wget https://techexpert.tips/wp-content/uploads/2017/12/TechExpert-Logo-Small.png
# vi test.html

这是test.html文件的内容。

<html>
<body>
<h1>TEST header</h1><br>
<img src="TechExpert-Logo-Small.png">
</body>

</html>

安装所需的软件以测试Apache Browser Caching功能。

# apt-get update
# apt-get install wget curl

使用以下命令使用WGET测试HTML文档的浏览器缓存功能。

请记住,您需要将200.200.200.200更改为您的服务器IP地址。

# wget -S http://200.200.200.200/test.html

以下是配置前的浏览器缓存配置结果:

HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Fri, 14 Dec 2018 16:58:36 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Fri, 14 Dec 2018 16:19:49 GMT
ETag: "64-57cfdcce7b707"
Accept-Ranges: bytes
Content-Length: 100
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
Length: 100 [text/html]

以下是配置后的浏览器缓存配置结果:

HTTP request sent, awaiting response...
HTTP/1.1 200 OK
Date: Fri, 14 Dec 2018 17:05:04 GMT
Server: Apache/2.4.29 (Ubuntu)
Last-Modified: Fri, 14 Dec 2018 16:19:49 GMT
Accept-Ranges: bytes
Content-Length: 100
Cache-Control: max-age=86400
Expires: Sat, 15 Dec 2018 17:05:04 GMT
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html
Length: 100 [text/html] Saving to: ‘test.html.3’

如您所见,我们的配置为HTML文档指定了一天的缓存。

•ExpiresByType text / html“access plus 1 days”

WGET结果显示HTML文件将保留到明天。

•到期日:周六,2018年12月15日17:05:04 GMT

使用以下命令使用CURL测试浏览器缓存功能到PNG图像。

# curl -svo /dev/null http://200.200.200.200/TechExpert-Logo-Small.png

以下是配置前的浏览器缓存配置结果:

* TCP_NODELAY set
* Connected to 34.220.19.99 (34.220.19.99) port 80 (#0)
> GET /TechExpert-Logo-Small.png HTTP/1.1
> Host: 34.220.19.99
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 14 Dec 2018 17:24:32 GMT
< Server: Apache/2.4.29 (Ubuntu)
< Last-Modified: Sat, 24 Nov 2018 02:49:53 GMT
< ETag: "483-57b60277af640"
< Accept-Ranges: bytes
< Content-Length: 1155
< Content-Type: image/png
<
{ [1155 bytes data]

以下是配置后的浏览器缓存配置结果:

* TCP_NODELAY set
* Connected to 34.220.19.99 (34.220.19.99) port 80 (#0)
> GET /TechExpert-Logo-Small.png HTTP/1.1
> Host: 34.220.19.99
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Fri, 14 Dec 2018 17:18:04 GMT
< Server: Apache/2.4.29 (Ubuntu)
< Last-Modified: Sat, 24 Nov 2018 02:49:53 GMT
< Accept-Ranges: bytes
< Content-Length: 1155
< Cache-Control: max-age=2592000
< Expires: Sun, 13 Jan 2019 17:18:04 GMT
< Content-Type: image/png
<
{ [1155 bytes data]

如您所见,我们的配置为PNG图像指定了一个月的缓存。

•ExpiresByType image / png“access plus 1 month”

CURL结果显示PNG文件将保留到1月13日。

•到期日:Sun,2019 Jan 1 17:18:04 GMT