A method to resolve a PHP backend generated image that cannot use the CDN cache

Source: Internet
Author: User

A method to resolve a PHP backend generated image that cannot use the CDN cache


Today found that there is a problem online, a picture domain name, the front-end has been added CDN cache, do not cache, the dynamic implementation of PHP image scaling, but after the output of PHP processed pictures, each time to read from the backend, back-end server pressure increased, after analysis, PHP did not make 304 of the processing,

HTTP principle is this, each request to the server, the service side detection has no modification, such as no modification, 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 cache;

Reference Source:
Http://www.lai18.com/content/433445.html


The relevant code is as follows:

Detection has not changed//edit http://www.lai18.com 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 (' ETag: '. MD5 ($this->image)); header (' cache-control:max-age=2592000 '); Echo $this Image


where the HTTP header http_if_none_match, the server is generally returned to the identity of a URL, generally used MD5 calculation, so that we detect the MD5 value is right, the same can be returned 304;

Ps:

Just started to catch a half-day package, only see the server returned the ETag label, did not see the HTTP header of the client If-none-match, the Fastcgi.conf.default to add the following code:

Fastcgi_param  cache_etag    $http _if_none_match;


A printing $_server, there is no cache_etag this variable, it seems nginx will put the relevant HTTP header into the $_server variable, but also deepened the understanding of the HTTP protocol 

A method to resolve a PHP backend generated image that cannot use the CDN cache

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.