Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze Apache Service mod_expires Module

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

In this issue, the editor will bring you about how to analyze the Apache service mod_expires module. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Apache service mod_expires cache module

One: introduction of mod_expires module

This module is an instruction that allows you to control the contents of the "expires" and "cache-control" headers of HTTP through the Apache configuration file, and to control the contents of the "expires" header and the max-age of the "cache-control" header when the server replies. This validity period can be set to the last modification time for the source file or the client access time.

These HTTP headers indicate the validity and persistence of the content to the client. If the client has a cache locally, the content read when the user accesses it again is read from the cache (if the cache is not invalidated), rather than from the server. The client will also check the cache to see if it needs to be updated from the server, so as to increase the user's experience and reduce the pressure on the server. It is also one of the tuning parameters in the actual production environment.

Two: check and install the module

The direct compilation and installation is shown below

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

Mod_expires.c

Compile and install in DSO mode to see the following

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

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

However, the above two methods cannot be installed at the same time, otherwise errors will occur.

View help

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

-- enable-expires Expires header control

The specific compilation commands are as follows

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

-- enable-expires

The compilation and installation in DSO is as follows

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

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

Parameter description

-a this option automatically adds a LoadModule line to the httpd.conf file to activate the module, or enable it if this line already exists

-c this option indicates that a compilation operation is required

-I this option indicates that an installation operation is required to install one or more dynamic shared objects to the server modules directory

[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

Process omission

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

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

Three: configure the mod_expires module

1. View the current http headers information

[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. Configuration module

There are two modes of module configuration: the configuration in the main configuration and the configuration in the single virtual host configuration file.

The configuration HTTP main configuration file is as follows

[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

The previous content is omitted

Options FollowSymLinks

AllowOverride None

Require all granted

Options FollowSymLinks

AllowOverride None

Require all granted

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

Check the syntax and restart the service

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

Syntax OK

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

Check processes and ports

[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)

Upload a picture to the site directory

[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 1809: 36 20160918093614.jpg

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

Local client browser access

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

The uploaded picture information can be displayed normally.

[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

# found no, this is the time limit of the cache

Content-Type: text/html

[root@Centos blog] # date

Sun Sep 18 12:21:36 CST 2016

Indicates that if you configure the HTTP master configuration file, it takes effect globally, but the virtual host configuration file does not need to be configured.

Configure a single virtual host file as follows

# port bash name

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

Check the syntax and restart the service

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

Syntax OK

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

View processes and ports

[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)

Upload a picture to the site directory

[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 1809: 36 20160918093614.jpg

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

Local client browser access

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

The uploaded picture information can be displayed normally.

View cache information

[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

The preliminary calculation should be 360 days, indicating that the configuration is correct.

[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

Baidu's LOG image cache is valid for about ten years. If you are interested, you can see it for yourself.

Four: the advantages of mod_expires module

1. Enhance users' experience of visiting the website.

Because some files are cached locally, the access speed is improved, and the user experience is improved accordingly.

2. Reduce server bandwidth and load pressure

Because the user reads the contents of the locally cached files when accessing, the interaction with the server is reduced, thus reducing the pressure on the server.

3. Save the cost of maintaining the server

As mentioned above, the pressure on the server is reduced, the maintenance personnel will be reduced accordingly, and the speed of updating the configuration of server accessories will be slowed down accordingly.

The above is the editor for you to share how to analyze the Apache service mod_expires module, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report