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