PHP file generated pictures cannot use CDN cache solution _php Tips

Source: Internet
Author: User

Today found a problem online, a picture domain name on the line, in the front-end has been added CDN cache, do not fall cache, PHP dynamic implementation of the picture scaling, but after the output of PHP processed pictures, each time to read from the back end of the server, the pressure of an instant increase, after analysis, PHP did not make 304 of the processing,

The principle of HTTP is this, each request to the server, service-side detection has not been modified, if not modified, you can directly return a 304 status code, so that the client's cache, the principle of CDN is so, if set 304, will be the corresponding URL to cache;

The relevant code is as follows:

Copy Code code as follows:

Detection has not changed
if (Isset ($_server[' Http_if_none_match ')) {
$etag = $_server[' Http_if_none_match '];
if (MD5 ($this->image) = = = $etag) {
Header ("http/1.1 304 not Modified");
Exit
}
}

Header ("last-modified:"). Gmdate ("D, D M Y h:i:s", Strtotime (' 2011-1-1 ')). " GMT ");
Output ETag Header
Header (' ETag: ' MD5 ($this->image));
Header (' cache-control:max-age=2592000 '); Echo $this->image;

The HTTP header http_if_none_match, is generally the server returned to a certain URL of the logo, the general use of MD5 calculation, so we detect the MD5 value is right, the same can return 304;

Ps:

Just started to catch a half day package, only to see the server returned the etag tag, do not see the client's HTTP header if-none-match, harm to add the following code in Fastcgi.conf.default:

Copy Code code as follows:

Fastcgi_param Cache_etag $http _if_none_match;

A print $_server, there is no cache_etag this variable, it seems that Nginx will be related to the HTTP header into the $_server variable, but also deepened the understanding of the HTTP protocol

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.