Solution: PHP backend images cannot be cached by CDN. Solution: PHP backend images cannot be cached by CDN.
Today, I found that there is a problem online. an online image domain name has been added to the CDN cache on the front end. if it is not cached, I will use PHP to dynamically scale the image, however, after PHP processes the image output, it will be read from the backend every time. The pressure on backend servers increases instantly. according to analysis, PHP does not process the image as much as 304,
The principle of HTTP is as follows: after each request is sent to the server, the server checks whether there are any changes. If no changes are made, a 304 status code can be returned directly, so that the client can be cached, this is the principle of CDN. if 304 is set, the corresponding URL will be cached;
Reference Source:
Http://www.lai18.com/content/433445.html
The related code is as follows:
// Check if there is no change // 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 ('2017-1-1 ')). "GMT"); // output etag header ('etag :'. md5 ($ this-> image); header ('cache-Control: max-age = 2592000 '); echo $ this-> image;
Among them, the http header HTTP_IF_NONE_MATCH is generally the identifier of a URL returned by the server, which is calculated using MD5. in this way, if the MD5 value is correct, 304 is returned;
PS:
After capturing the half-day package, only the Etag tag returned by the server is displayed, and If-None-Match in the http header of the client is not displayed. the following code is added to fastcgi. conf. default:
fastcgi_param CACHE_ETAG $http_if_none_match;
When $ _ SERVER is printed, there is no CACHE_ETAG variable at all. it seems that nginx will put the relevant HTTP header in the $ _ SERVER variable, which also improves the understanding of the http protocol.