Because of the GFW relationship, the use of gravatar blog comment avatar often appears "graphic split special effects", which must be a problem many webmasters have encountered. There are also many tutorials on the Internet, which solve the problem of graph cracking by changing the source of avatar. It can indeed solve the problem of image cracking, but the loading speed of the avatar really needs to be improved. Here are three ways to cache the avatar locally.
1、 Code scheme
Code cache scheme from Willin Kan Master, excerpted from WP University The following is the specific approach:
① Create cache directory
Create a folder in the same level directory of wp content, name it avatar, and set the folder permissions to 0755 (if 0755 fails, try 0777).
② . Set default avatar
Prepare a default avatar of appropriate size, named "default. jpg", and put it in the avatar folder.
③ Add cache code
Copy the following code to the functions.php file of the template:
function my_avatar($avatar) { $tmp = strpos($avatar, 'http'); $g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp); $tmp = strpos($g, 'avatar/') + 7; $f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp); $w = get_bloginfo('wpurl'); $e = ABSPATH .' avatar/'. $f .'.jpg'; $t = 1209600; // Set 14 days in seconds if ( ! Is_file ($e) | | (time() - filemtime ($e))>$t) {//When the avatar does not exist or the file is updated after 14 days copy(htmlspecialchars_decode($g), $e); } else $avatar = strtr($avatar, array($g => $w.'/avatar/'.$ f.'.jpg')); if (filesize($e) < 500) copy($w.'/avatar/default.jpg', $e); return $avatar; } add_filter('get_avatar', 'my_avatar');
2、 Plug in Scheme
Not long ago, robin blogger brother bird shared a plug-in that caches gavatar avatar to local in the beginning group. After personal trial, it was found that it was good. The name of this plug-in is nix gravatar cache. It seems that there is something wrong with the original plug-in. Brother Bird DIY.
If you find that there is a problem with the original version, download the modified version of Brother Bird!
The copyright of the downloaded content provided by this site belongs to this site. Reprint must Indicate the source!
*The copyright of articles marked with "reprint" belongs to the original author.
III Nginx scheme
In the process of testing this plug-in, I looked at the effective avatar path and got a sudden inspiration: this cache can be cached locally through Nginx's proxy reverse proxy! It is similar to the direction agent Google to solve the problem of being blocked.
Just do what you say. After a lot of tests, I found it really works! Let's share the specific methods.
① , Compile Nginx
If you have practiced the Nginx caching tutorial shared by Zhang Ge's blog before, I believe this step can be ignored.
The Nginx reverse proxy cache needs to integrate the ngx_cache_purge module. If not, you need to recompile Nginx. For details, please refer to the article shared earlier by Zhang Ge's blog:
《 Enable Nginx cache acceleration for websites and support html pseudo static pages 》Proxy mode, add the cache module, and add proxy cache rules in the http context module, such as:
proxy_connect_timeout 5; proxy_read_timeout 60; proxy_send_timeout 5; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_cache_path /tmp/cache/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=30d max_size=5g; proxy_temp_path /tmp/cache/proxy_cache/temp;
②、 Nginx configuration
Add the following rules to the existing rules of the website, reverse proxy gavatar and cache them locally:
location /avatar { proxy_pass http://cn.gravatar.com ; proxy_redirect off; proxy_set_header Host cn.gravatar.com; proxy_cache cache_one; proxy_cache_valid 200 302 304 365d; proxy_cache_valid 301 1d; proxy_cache_valid any 1m; add_header Images-Cache "$upstream_cache_status from $host"; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; access_log off; log_not_found off; expires max; }
③、 PHP code
Insert the following code in functions.php under the theme directory:
//Change gavatar source function mytheme_get_avatar($avatar) { $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"zhang.ge",$avatar); // Please change it to your homepage domain name return $avatar; } add_filter( 'get_avatar', 'mytheme_get_avatar', 10, 3 );
You can change the WordPress avatar address to your own domain name. Because the secondary directory field of the avatar address is/avatar/, it will match the reverse proxy rule we added in Nginx in the previous step, so you can pull the avatar from cn.gavatar.com and cache it locally on the server.
Obviously, this method supports various website building programs (need to modify the website code), which is more efficient than PHP code or plug-ins! And no external url address will appear!
4、 Tortuous expansion
In the last article, Zhang Ge's blog shared the optimization scheme currently in use. One of the suggestions is that static resources use secondary domain names and refuse to write cookies. So this article can continue to expand and toss about: change the avatar address to a secondary domain name. For example, right click Zhang Ge's blog comment avatar and you can see that it has become res.zgboke.com. In fact, it is just a new server module of res.zgboke.com. It is very simple. Interested friends can do something by themselves. This article will not explain it.