利用Nginx反代Gravatar头像,支持https

最近这个段时间,发现wordpress自带的头像链接速度变慢了,严重拖了博客的访问速度。想到博客也是挂了Cloudflare CDN,于是利用了下Nginx反代Gravatar头像,效果还不错。配置文件只是简单的反代,支持https,没有加缓存和防盗链。

Nginx配置文件:

server
{
listen 80;
#listen [::]:80;
listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name gravatar.xxxyyy.com;
ssl_certificate /usr/local/nginx/conf/ssl/xxxyyy.com.pem;
ssl_certificate_key /usr/local/nginx/conf/ssl/xxxyyy.com.key;
location / {
proxy_pass https://en.gravatar.com;
}
access_log /home/wwwlogs/gravatar.xxxyyy.com.log;
}

在主题函数文件加入以下代码:

//gravatar头像替换
function my_avatar($avatar) {
$avatar = str_replace(array("secure.gravatar.com"),"gravatar.xxxyyy.com",$avatar);
return $avatar;
}
add_filter( 'get_avatar', 'my_avatar');


相关推荐