The solution for PHP backend images that cannot be cached by CDN

Source: Internet
Author: User
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.

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.