当前位置:首页 > CMS教程 > WordPress > 列表

解决Gravatar头像、Google fonts和Google ajax不能使用的方法

发布:smiling 来源: PHP粉丝网  添加日期:2015-03-23 14:59:19 浏览: 评论:0 

因为国内google打不开了所以导致像国外的Gravatar头像、Google fonts和Google ajax这些接口都不能使用了,下文我们就一起来看解决办法.

Gravatar头像现在大陆地区偶尔无法访问,还有各种google服务也用不了,对于“一枚程序员”来说是不能容忍的,于是麦葱弄了个解决之法——使用nginx反向代理Gravatar头像、Google fonts和Google ajax.

反代之前需要确定nginx已经安装了proxy_cache模块并配置好路径及参数.

下面是麦葱在nginx.conf中配置的代码:

  1. http 
  2.     #其他代码...# 
  3.     proxy_connect_timeout 5; 
  4.     proxy_read_timeout 60; 
  5.     proxy_send_timeout 5; 
  6.     proxy_buffer_size 16k; 
  7.     proxy_buffers 4 64k; 
  8.     proxy_busy_buffers_size 128k; 
  9.     proxy_temp_file_write_size 128k; 
  10.     proxy_temp_path /tmp/nginx_proxy_temp 1 2;  #临时文件目录 
  11.     #其他代码...# 

然后是单独的配置文件gravatar.conf,带#的需要你进行相应修改:

  1. proxy_cache_path  /tmp/nginx_proxy_cache/gravatar levels=1:2 keys_zone=GRAVATAR:32m inactive=7d max_size=1g; #缓存路径 
  2. server { 
  3.     listen 80; 
  4.     server_name gravatar.yuxiaoxi.com; #绑定的域名 
  5.     location  / { 
  6.         proxy_cache GRAVATAR; 
  7.         proxy_cache_key "$scheme://$host$request_uri"
  8.         proxy_cache_valid 200 304 7d; 
  9.         proxy_cache_valid 301 3d; 
  10.         proxy_cache_valid any 10s; 
  11.         proxy_pass http://0.gravatar.com; #反代的域名 
  12.         proxy_pass_header Server; 
  13.         proxy_redirect off; 
  14.         proxy_set_header Host 0.gravatar.com; 
  15.         proxy_set_header X-Real-IP $remote_addr
  16.         proxy_set_header X-Scheme $scheme
  17.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
  18.     } 
  19.     location ~ /purge(/.*) { 
  20.         allow 127.0.0.1; 
  21.         deny all; 
  22.         proxy_cache_purge GRAVATAR "$scheme://$host$1"
  23.     } 
  24.     location ~* \.(ftpquota|htaccess|htpasswd|asp|aspx|jsp|asa|mdb)?$ { 
  25.         deny all; 
  26.     } 
  27.     access_log /home/wwwroot/gravatar/log/access.log combined; #access_log end  
  28.     error_log /home/wwwroot/gravatar/log/error.log crit; #error_log end 
  29. server 
  30.     listen       443 ssl spdy; 
  31.     server_name gravatar.yuxiaoxi.com; #绑定的域名 
  32.     ssl on; 
  33.     ssl_certificate /usr/local/ssl/yuxiaoxi.crt; #ssl证书 
  34.     ssl_certificate_key /usr/local/ssl/yuxiaoxi.pem; #ssl密钥 
  35.     ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
  36.     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'
  37.     ssl_prefer_server_ciphers on; 
  38.     ssl_session_timeout 5m; 
  39.     ssl_session_cache shared:SSL:10m; 
  40.     ssl_trusted_certificate /usr/local/ssl/ca.crt; #CA根证书 
  41.     location  / { 
  42.         proxy_cache GRAVATAR; 
  43.         proxy_cache_key "$scheme://$host$request_uri"
  44.         proxy_cache_valid 200 304 7d; 
  45.         proxy_cache_valid 301 3d; 
  46.         proxy_cache_valid any 10s; 
  47.         proxy_pass http://0.gravatar.com; #反代的域名 
  48.         proxy_pass_header Server; 
  49.         proxy_redirect off; 
  50.         proxy_set_header Host 0.gravatar.com; 
  51.         proxy_set_header X-Real-IP $remote_addr
  52.         proxy_set_header X-Scheme $scheme
  53.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
  54.     } 
  55.     location ~ /purge(/.*) { 
  56.         allow 127.0.0.1; 
  57.         deny all; 
  58.         proxy_cache_purge GRAVATAR "$scheme://$host$1"
  59.     } 
  60.     location ~* \.(ftpquota|htaccess|htpasswd|asp|aspx|jsp|asa|mdb)?$ {//开源软件:phpfensi.com 
  61.         deny all; 
  62.     } 
  63.     access_log /home/wwwroot/gravatar/log/access_ssl.log combined; #access_log end 
  64.     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;

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.phpfensi.com

3、搜索bymt_avatar,从//获取头像及缓存开始,到//分页功能上一行,替换为:

  1. // 通过curl获取内容 
  2. function bymt_curl_contents($url){ 
  3.     $ch = curl_init(); 
  4.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
  5.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
  6.     curl_setopt($ch, CURLOPT_URL, $url); 
  7.     $response =  curl_exec($ch); 
  8.     curl_close($ch); 
  9.     /** 请求为空 **/ 
  10.     if(emptyempty($response)){ 
  11.         $response = ''
  12.     } 
  13.     return $response
  14. // 写文件 
  15. function bymt_write_file($path$data$mode = 'wb') { 
  16.     if ( ! $fp = @fopen($path$mode)) { 
  17.         return false; 
  18.     } 
  19.     flock($fp, LOCK_EX); 
  20.     fwrite($fp$data); 
  21.     flock($fp, LOCK_UN); 
  22.     fclose($fp); 
  23.     return true; 
  24. //获取头像及缓存 
  25. function bymt_avatar($email$size='50'$time='1209600') { // 默认尺寸50px 有效期14天 
  26.     if ( !is_numeric($size) ) $size = '50'
  27.     if ( !is_numeric($time) ) $time = '1209600'
  28.     if(bymt_c('avatar_cache') ){ 
  29.         $mailhash = md5(strtolower(trim($email))); 
  30.         $default = urlencode(get_option('avatar_default')); 
  31.         $rating = get_option('avatar_rating'); 
  32.         $ava_path = ABSPATH . 'avatar/' . $mailhash'-' . $size . '.jpg'
  33.         if ( ! file_exists($ava_path) || filesize($ava_path) < 1 || (time() - filemtime($ava_path)) > $time) { 
  34.             $ava_url = 'http://gravatar.phpfensi.com/avatar/'.$mailhash.'?s='.$size.'&d='.$default.'&r='.$rating
  35.             $avadata = bymt_curl_contents($ava_url); 
  36.             bymt_write_file($ava_path$avadata); 
  37.         } else { 
  38.             $ava_url = esc_url(home_url('/avatar/')) . $mailhash'-' . $size . '.jpg'
  39.         } 
  40.         return '<img src="'.$ava_url.'" alt="avatar" class="avatar avatar-'.$size.' photo" height="'.$size.'" width="'.$size.'" />'
  41.     }else
  42.         return get_avatar( $email$size ); 
  43.     } 

4、打开/wp-includes/script-loader.php,搜索ajax.googleapis.com替换为ajax.yuxiaoxi.com,搜索 fonts.gstatic.com替换为fonts.phpfensi.com;

搞定收工.

Tags: Gravatar Google fonts 博客头像

分享到: