How to use Apache Mod_expires and mod_headers for file caching and mod_deflate compression output

Source: Internet
Author: User

1. Compress output with Mod_deflate module (start gzip)

Open mod_deflate

sudo a2enmod deflatesudo/etc/init.d/apache2 restart

Add in httpd.conf

<ifmodule mod_deflate.c> #单独设置需要压缩的类型AddOutputFilterByType deflate text/html text/plain text/xml text/ Cssaddoutputfilterbytype DEFLATE application/x-javascript</ifmodule>

If all output by default needs to be compressed, exclude only parts.

<ifmodule Mod_deflate.c>setoutputfilter deflate #插入过滤器, gzip all output # Set the uncompressed type Setenvifnocase Request_uri. (?: gif|jpe?g|png) $ no-gzip dont-varysetenvifnocase Request_uri. (?: Exe|t?gz|zip|bz2|sit|rar) $ no-gzip dont-varysetenvifnocase Request_uri. (?:p df|doc) $ no-gzip dont-vary</ifmodule>

2. Using Apache Mod_expires and mod_headers for file caching


Open Mod_expires with the mod_headers

sudo a2enmod expiressudo a2enmod headerssudo/etc/init.d/apache2 Restart

With Mod_expires, add in httpd.conf

<ifmodule mod_expires.c>expiresactive on #开启ExpiresDefault A300 #默认ExpiresByType text/html a300expiresbytype Text/css A2592000 # 30 days expiresbytype image/gif a2592000expiresbytype image/jpeg a2592000expiresbytype application/ X-javascript a2592000</ifmodule>

With Mod_headers, add in httpd.conf

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


3.cache-control Description:


The cache of the Web page is controlled by the "Cache-control" in the HTTP message header, and the common values are private, No-cache, Max-age, must-revalidate, etc., and the default is private. Its function is divided into the following situations according to different ways of re-browsing:


(1) Open a new window
The value is private, No-cache, Must-revalidate, and the server will be re-accessed when a new window is opened.
If you specify a max-age value, the server is not re-accessed in the time within this value, for example:
Cache-control:max-age=5 (indicates that access will not go to the server within 5 seconds of accessing this page)
(2) Enter in the Address bar
A value of private or must-revalidate will only access the server on the first visit and will no longer be accessed at a later time.
The value is No-cache, which is accessed every time.
A value of max-age, the access is not repeated until it expires.
(3) Press Back button
The value is private, must-revalidate, Max-age, and is not re-accessed.
A value of No-cache, which is repeatedly accessed every time
(4) Press the Refresh button
No matter what the value, it will be accessed repeatedly
Cache-control (General header, HTTP1.1)
. Public:(only for response headers)
Response: Notifies any cached person of any way that the response can be cached unconditionally.
.Private(Response header only)
Response: Inform the cache (as far as I know, the user agent, the common browser's local cache. Users are also referred to as system users. However, you should not exclude some gateways that can identify the situation for each end user, and only cache responses for a single user. You can specify a field specifically. such as private– "username", the header content named Username in the response header, is not shared by the cache.
.No-cache:
Request: Inform the cache, must forward the original request, and inform any cache, do not directly take your cached copy, fool people. You need to forward my request and verify your cache (if any). The corresponding noun: end-to-end overloading.
Response: Allows the cache to cache the copy. The actual value is that the cache is always forced to check the freshness of the cache. Once you confirm freshness, you can use a cached copy as a response. No-cache, you can also specify a containing field, such as a typical application, No-cache=set-cookie. The result of this is to inform the cache that you do not use cached content for the Set-cookie field. Instead, use the new drop. Other content can use the cache.
.No-store:
Requests: informs, requests, and responses are disallowed from being cached. (Perhaps for privacy reasons)
Response: Ibid.
.Max-age:
Request: Force response cache, based on this value, check freshness. That is, the age value with itself, compared to the request time. If the max-age value is exceeded, the server-side validation is enforced. To ensure that a fresh response is returned. Its function is essentially similar to the traditional expires, But the difference is that expires is based on a particular date value. One but the time of the cache itself is inaccurate. The result may be wrong. And Max-age, obviously, is not the problem. The priority of Max-age is also higher than expires.
Response: Similar to the same, except that the issuing party is different.
.Max-stale:
Request: means that I allow the cache to send a stale cache that expires no more than the specified number of seconds.
Response: Ibid.
.must-revalidate(Response header only)
Response: means that if the cache is stale, it must be re-validated. Instead of trying to return a cache that is not in the freshness period. The difference between No-cache and No-cache is that the concept of freshness is completely disregarded. Always force re-validation. In theory, Must-revalidate is more energy-efficient, but it may not always be as accurate as no-cache. Because even if the cache is considered fresh, there is no guarantee that the server side has not been updated. If the cache is a cache proxy server, if it tries to re-authenticate, Unable to connect to the original server, it is also not allowed to return a copy in the cache that is not fresh. Instead, a 504 Gateway timeout must be returned.
.proxy-revalidate(Response header only)
Response: The limit is similar to must-revalidate. The difference is in the scope of the receptor. Proxy-revalidate, is to exclude the user agent cache. That is, its rules do not apply to the local cache of the user agent.
.Min-fresh(Request header only)
Request: Tells the cache if the current time is added to the Min-fresh value, exceeding the expiration time of the cache. You're going to give me a new one. In fact, the function of a bit and max-age similar. But the bigger one is the semantic difference.
.only-if-cached:(for request headers only)
Request: Tell the cache, I want the content to come from the cache, I don't care if the cached response is fresh.
.S-maxage(Response header only)
Response: The only difference with Max-age is that the s-maxage is only applied to the shared cache. Instead of referencing the local cache of the user agent, such as for a single-user cache. In addition, S-maxage priority is higher than max-age. Cache-extension (Cache-extension is a generalization of McCartney. It means that all customizations, or extensions, directives, clients, and server-side can be customized to extend Cache-control related directives.) Well, actually, we can. cache-control:max-age=300, custom-directive = xxx, public. This allows us to define an extension instruction that is collectively referred to as cache-extension. If the corresponding client or server side, do not know, it will be ignored.
.No-transform
Request: Notify the agent, do not change the media type, such as JPG, you changed to PNG.
Response: Ibid.

This article explains how to use Apache Mod_expires and mod_headers to achieve file caching and mod_deflate compression output, more relevant content please pay attention to the PHP Chinese network.

Related recommendations:
Introduction to the HTML5 history API

Introduction to bubble, dichotomy insertion, fast sorting algorithm

About PHP support for the continuation of the breakpoint file download class related content

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.