Gravatar頭像現在大陸地區偶爾無法訪問,還有各種google服務也用不了。對於“一枚程式員”來說是不能容忍的。
於是麥蔥弄了個解決之法——使用nginx反向 ProxyGravatar頭像、Google fonts和Google ajax。
反代之前需要確定nginx已經安裝了proxy_cache模組並配置好路徑及參數。
下面是麥蔥在nginx.conf中配置的代碼:
http
{
#其他代碼...#
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_temp_path /tmp/nginx_proxy_temp 1 2; #臨時檔案目錄
#其他代碼...#
}
然後是單獨的設定檔gravatar.conf,帶#的需要你進行相應修改:
proxy_cache_path /tmp/nginx_proxy_cache/gravatar levels=1:2 keys_zone=GRAVATAR:32m inactive=7d max_size=1g; #緩衝路徑
server {
listen 80;
server_name gravatar.yuxiaoxi.com; #綁定的網域名稱
location / {
proxy_cache GRAVATAR;
proxy_cache_key "$scheme://$host$request_uri";
proxy_cache_valid 200 304 7d;
proxy_cache_valid 301 3d;
proxy_cache_valid any 10s;
proxy_pass http://0.gravatar.com; #反代的網域名稱
proxy_pass_header Server;
proxy_redirect off;
proxy_set_header Host 0.gravatar.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge GRAVATAR "$scheme://$host$1";
}
location ~* \.(ftpquota|htaccess|htpasswd|asp|aspx|jsp|asa|mdb)?$ {
deny all;
}
access_log /home/wwwroot/gravatar/log/access.log combined; #access_log end
error_log /home/wwwroot/gravatar/log/error.log crit; #error_log end
}
server
{
listen 443 ssl spdy;
server_name gravatar.yuxiaoxi.com; #綁定的網域名稱
ssl on;
ssl_certificate /usr/local/ssl/yuxiaoxi.crt; #ssl認證
ssl_certificate_key /usr/local/ssl/yuxiaoxi.pem; #ssl密鑰
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128:AES256:AES:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK';
ssl_prefer_server_ciphers on;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:10m;
ssl_trusted_certificate /usr/local/ssl/ca.crt; #CA根憑證
location / {
proxy_cache GRAVATAR;
proxy_cache_key "$scheme://$host$request_uri";
proxy_cache_valid 200 304 7d;
proxy_cache_valid 301 3d;
proxy_cache_valid any 10s;
proxy_pass http://0.gravatar.com; #反代的網域名稱
proxy_pass_header Server;
proxy_redirect off;
proxy_set_header Host 0.gravatar.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /purge(/.*) {
allow 127.0.0.1;
deny all;
proxy_cache_purge GRAVATAR "$scheme://$host$1";
}
location ~* \.(ftpquota|htaccess|htpasswd|asp|aspx|jsp|asa|mdb)?$ {
deny all;
}
access_log /home/wwwroot/gravatar/log/access_ssl.log combined; #access_log end
error_log /home/wwwroot/gravatar/log/error_ssl.log crit; #error_log end
}
Google fonts和Google ajax配置差不多,我就不貼出來了,請在文尾查看下載連結。
你也可以直接使用麥蔥弄好的,支援SSL哦~:
將www.gravatar.com,cn.gravatar.com,0.gravatar.com,1.gravatar.com,2.gravatar.com替換為gravatar.yuxiaoxi.com;
將fonts.gstatic.com替換為fonts.yuxiaoxi.com;
將ajax.googleapis.com替換為ajax.yuxiaoxi.com;
BYMT主題使用者修改辦法:
1、開啟/wp-content/themes/BYMT/目錄下的functions.php檔案
2、搜尋bymt_get_avatar,將gravatar.duoshuo.com替換為gravatar.yuxiaoxi.com
3、搜尋bymt_avatar,從//擷取頭像及緩衝開始,到//分頁功能上一行,替換為:
// 通過curl擷取內容
function bymt_curl_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
$response = curl_exec($ch);
curl_close($ch);
/** 請求為空白 **/
if(empty($response)){
$response = '';
}
return $response;
}
// 寫檔案
function bymt_write_file($path, $data, $mode = 'wb') {
if ( ! $fp = @fopen($path, $mode)) {
return false;
}
flock($fp, LOCK_EX);
fwrite($fp, $data);
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
//擷取頭像及緩衝
function bymt_avatar($email, $size='50', $time='1209600') { // 預設尺寸50px 有效期間14天
if ( !is_numeric($size) ) $size = '50';
if ( !is_numeric($time) ) $time = '1209600';
if(bymt_c('avatar_cache') ){
$mailhash = md5(strtolower(trim($email)));
$default = urlencode(get_option('avatar_default'));
$rating = get_option('avatar_rating');
$ava_path = ABSPATH . 'avatar/' . $mailhash. '-' . $size . '.jpg';
if ( ! file_exists($ava_path) || filesize($ava_path) < 1 || (time() - filemtime($ava_path)) > $time) {
$ava_url = 'http://gravatar.yuxiaoxi.com/avatar/'.$mailhash.'?s='.$size.'&d='.$default.'&r='.$rating;
$avadata = bymt_curl_contents($ava_url);
bymt_write_file($ava_path, $avadata);
} else {
$ava_url = esc_url(home_url('/avatar/')) . $mailhash. '-' . $size . '.jpg';
}
return '<img src="'.$ava_url.'" alt="avatar" class="avatar avatar-'.$size.' photo" height="'.$size.'" width="'.$size.'" />';
}else{
return get_avatar( $email, $size );
}
}
4、開啟/wp-includes/script-loader.php,搜尋ajax.googleapis.com替換為ajax.yuxiaoxi.com,搜尋 fonts.gstatic.com替換為fonts.yuxiaoxi.com;
搞定收工!