Table of Contents
1. Use mod_deflate module to compress output (start gzip)
2. Use Apache mod_expires and mod_headers to implement file caching
to httpd.conf
3.Cache-Control description: " > to httpd.conf
3.Cache-Control description:
Home Backend Development PHP Tutorial How to use Apache mod_expires and mod_headers to implement file caching and mod_deflate compressed output

How to use Apache mod_expires and mod_headers to implement file caching and mod_deflate compressed output

Jun 11, 2018 am 10:16 AM
apache

1. Use mod_deflate module to compress output (start gzip)

Enablemod_deflate

1

2

sudo a2enmod deflate

sudo /etc/init.d/apache2 restart

Copy after login

Add

1

2

3

4

5

<IfModule mod_deflate.c>

#单独设置需要压缩的类型

AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css

AddOutputFilterByType DEFLATE application/x-javascript

</IfModule>

Copy after login

in httpd.conf. By default, all output needs to be compressed, and only some are excluded.

1

2

3

4

5

6

7

<IfModule mod_deflate.c>

SetOutputFilter DEFLATE #插入过滤器,gzip所有输出

#设置不压缩的类型

SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary

</IfModule>

Copy after login

2. Use Apache mod_expires and mod_headers to implement file caching


Openmod_expires Use mod_expires with mod_headers

1

2

3

sudo a2enmod expires

sudo a2enmod headers

sudo /etc/init.d/apache2 restart

Copy after login

, in httpd. conf, add

1

2

3

4

5

6

7

8

9

<IfModule mod_expires.c>

ExpiresActive on #开启

ExpiresDefault A300 #默认

ExpiresByType text/html A300

ExpiresByType text/css A2592000 # 30天

ExpiresByType image/gif A2592000

ExpiresByType image/jpeg A2592000

ExpiresByType application/x-javascript A2592000

</IfModule>

Copy after login

Using mod_headers, add

1

2

3

4

5

6

7

8

9

10

11

<IfModule mod_headers.c>

<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">

Header set Cache-Control "max-age=604800, public"

</FilesMatch>

<FilesMatch "\.(xml|txt)$">

Header set Cache-Control "max-age=18000, public, must-revalidate"

</FilesMatch>

<FilesMatch "\.(html|htm|php)$">

Header set Cache-Control "max-age=3600, must-revalidate"

</FilesMatch>

</IfModule>

Copy after login

to httpd.conf
3.Cache-Control description:


#The cache of web pages is controlled by the "Cache-control" in the HTTP message header. Commonly used Values ​​include private, no-cache, max-age, must-revalidate, etc. The default is private. Its function is divided into the following situations according to different re-browsing methods:


(1) Open a new window
The value is private, no-cache, must-revalidate, then the server will be accessed again when a new window is opened.
If a max-age value is specified, the server will not be accessed again within this value, for example:
Cache-control: max-age=5 (indicates 5 seconds after accessing this web page) Accessing again within seconds will not go to the server)
(2) Press Enter in the address bar
If the value is private or must-revalidate, the server will only be accessed the first time, and will not be accessed again.
If the value is no-cache, it will be accessed every time.
If the value is max-age, it will not be accessed again before expiration.
(3) Press the back button
If the value is private, must-revalidate, max-age, it will not be accessed again.
If the value is no-cache, it will be accessed repeatedly every time
( 4) Press the refresh button
No matter what the value is, it will be accessed repeatedly

Cache-Control (regular header, HTTP1.1)
.public: (Response header only)
Response: Inform cachers of any channel that the response can be cached unconditionally.
.private (Only For the response header)
Response: Inform the cacher (as far as I know, it refers to the user agent, the local cache of common browsers. The user also refers to the system user. But perhaps, it should not be excluded that some gateways can Identify each end user), only cache the response for a single user. And you can specify a certain field. For example, private - "username", the header content named username in the response header will not be shared and cached.
.no-cache:
Request: Inform the cacher that the original request must be forwarded exactly as it is, and inform any cachers not to directly use your cached copy to fool people. You need to go Forward my request and verify your cache (if any). Corresponding noun: end-to-end reloading.
Response: Allow cachers to cache copies. So its actual value is to always force cachers to calibrate Verify the freshness of the cache. Once freshness is confirmed, you can use the cached copy as a response. no-cache, you can also specify an included field, such as a typical application, no-cache=Set-Cookie. The result of this is to inform Cache, for the Set-Cookie field, you should not use cached content. Instead, use Xindi. Other content can be cached.
.no-store:
Request: inform, request and responses are prohibited from being cached. (Perhaps for privacy reasons)
Response: Same as above.
.max-age:
Request: Force response cacher, according to this value, Verify freshness. That is, compare it with its own Age value and the request time. If the max-age value is exceeded, server-side verification is forced to ensure that a fresh response is returned. Its function is essentially similar to traditional Expires. But the difference is that Expires is compared based on a specific date value. Once the cacher's own time is inaccurate, the result may be wrong. Max-age obviously does not have this problem. Max-age also has a higher priority than Expires.
Response: Similar to the above, except that the sender is different.
.max-stale:
Request: Meaning, I allow the cacher to send one, and it will not expire A stale cache that exceeds the specified number of seconds.
Response: Same as above.
.must-revalidate (response header only)
Response: Meaning, if the cache is past fresh period, you must re-verify instead of trying to return a cache that is not in the fresh period. The difference from no-cache is that no-cache completely ignores the concept of fresh period and always forces re-validation. In theory, must-revalidate is more Saves traffic, but compared to no-cache, it may not always be so accurate. Because even if the cacher thinks it is fresh, there is no guarantee that the server has not been updated. If the cacher is a caching proxy server, if it tries to When revalidating, if the original server cannot be connected, a stale copy in the cache is not allowed to be returned. Instead, a 504 Gateway timeout.
.proxy-revalidate(only Response header)
Response: The restriction is similar to must-revalidate. The difference is the scope of the acceptor. proxy-revalidate is to exclude the user agent's cache. That is, its rules do not apply to the user agent's local Cache.
.min-fresh(request header only)
Request: Inform the cacher that if the current time plus the value of min-fresh exceeds the expiration time of the cache, a new one will be given to me. In fact, I personally feel that its function is somewhat similar to max-age. But it is larger. The difference is semantic.
.only-if-cached: (only request header)
Request: Inform the cacher that I want the content to come from the cache, and I don’t care about being cached. Cache response, whether it is fresh.
.s-maxage(response header only)
Response: The only difference from max-age is that s-maxage only applies to shared cache . does not refer to the local cache of the user agent, such as the cache for a single user. In addition, s-maxage has a higher priority than max-age..cache-extension (cache-extension is a general name. It refers to All custom, or extended, instructions, both the client and the server can customize and extend Cache-Control related instructions.) Then, in fact, we can do this Cache-Control:max-age=300, custom-directive = xxx, public. In this way, we define an extension directive collectively called cache-extension. If the corresponding client or server does not recognize this directive, it will be ignored.
.no-transform
Request: Tell the agent not to change the media type, such as jpg, you changed it to png.
Response: Same as above.

This article explains how to use Apache mod_expires and mod_headers Implement file caching and mod_deflate compressed output. For more related content, please pay attention to the PHP Chinese website.

Related recommendations:
Introduction to HTML5 history API

About bubbling, dichotomy insertion, quick sort algorithm Introduction

#Explain the related content of PHP file download class that supports breakpoint resume transfer

The above is the detailed content of How to use Apache mod_expires and mod_headers to implement file caching and mod_deflate compressed output. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1657
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

How to set the cgi directory in apache How to set the cgi directory in apache Apr 13, 2025 pm 01:18 PM

To set up a CGI directory in Apache, you need to perform the following steps: Create a CGI directory such as "cgi-bin", and grant Apache write permissions. Add the "ScriptAlias" directive block in the Apache configuration file to map the CGI directory to the "/cgi-bin" URL. Restart Apache.

How to view your apache version How to view your apache version Apr 13, 2025 pm 01:15 PM

There are 3 ways to view the version on the Apache server: via the command line (apachectl -v or apache2ctl -v), check the server status page (http://&lt;server IP or domain name&gt;/server-status), or view the Apache configuration file (ServerVersion: Apache/&lt;version number&gt;).

What to do if the apache80 port is occupied What to do if the apache80 port is occupied Apr 13, 2025 pm 01:24 PM

When the Apache 80 port is occupied, the solution is as follows: find out the process that occupies the port and close it. Check the firewall settings to make sure Apache is not blocked. If the above method does not work, please reconfigure Apache to use a different port. Restart the Apache service.

How to view the apache version How to view the apache version Apr 13, 2025 pm 01:00 PM

How to view the Apache version? Start the Apache server: Use sudo service apache2 start to start the server. View version number: Use one of the following methods to view version: Command line: Run the apache2 -v command. Server Status Page: Access the default port of the Apache server (usually 80) in a web browser, and the version information is displayed at the bottom of the page.

How to solve the problem that apache cannot be started How to solve the problem that apache cannot be started Apr 13, 2025 pm 01:21 PM

Apache cannot start because the following reasons may be: Configuration file syntax error. Conflict with other application ports. Permissions issue. Out of memory. Process deadlock. Daemon failure. SELinux permissions issues. Firewall problem. Software conflict.

How to configure zend for apache How to configure zend for apache Apr 13, 2025 pm 12:57 PM

How to configure Zend in Apache? The steps to configure Zend Framework in an Apache Web Server are as follows: Install Zend Framework and extract it into the Web Server directory. Create a .htaccess file. Create the Zend application directory and add the index.php file. Configure the Zend application (application.ini). Restart the Apache Web server.

How to delete more than server names of apache How to delete more than server names of apache Apr 13, 2025 pm 01:09 PM

To delete an extra ServerName directive from Apache, you can take the following steps: Identify and delete the extra ServerName directive. Restart Apache to make the changes take effect. Check the configuration file to verify changes. Test the server to make sure the problem is resolved.

See all articles