PHP file generated pictures cannot use CDN Cache workaround _php Tutorial

Source: Internet
Author: User

PHP file generated pictures cannot use CDN cache workaround


This article mainly introduces the PHP file generated pictures can not use the CDN cache solution, here the PHP generated image refers to the SRC address is a php file picture, if not to do CDN, the server pressure will be very large, this article explains how to add CDN method, the need for friends can refer to the next

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;

The relevant code is as follows:

The code is as follows:

The test has not changed

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

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:

Copy the code code as follows:

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

http://www.bkjia.com/PHPjc/1020284.html www.bkjia.com true http://www.bkjia.com/PHPjc/1020284.html techarticle php file generated pictures cannot use CDN Cache workaround This article mainly introduces the PHP file generated pictures can not use the CDN cache solution, here the PHP generated image refers 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.