Solution to failure to use CDN cache for images generated by PHP files _ PHP Tutorial

Source: Internet
Author: User
The CDN cache cannot be used for images generated by php files. The solution to the inability to use CDN cache for images generated by php files this article mainly introduces the solution to the inability to use CDN cache for images generated by php files, the PHP image generated here refers to the solution that the image generated by the s PHP file cannot be cached by CDN.

This article mainly introduces how to use CDN to cache images generated by PHP files. The PHP image generated here means that the src address is an image of a PHP file. if CDN is not used, the pressure on the server will be very high. This article explains how to add CDN. For more information, see

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;

The related code is as follows:

The code is as follows:

// Check for any changes

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 the etag header

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:

The code is as follows:

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.

This article mainly introduces how to use CDN cache for images generated by PHP files. The PHP images here refer to s...

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.