因为国内google打不开了所以导致像国外的Gravatar头像、Google fonts和Google ajax这些接口都不能使用了,下文我们就一起来看解决办法.
Gravatar头像现在大陆地区偶尔无法访问,还有各种google服务也用不了,对于“一枚程序员”来说是不能容忍的,于是麦葱弄了个解决之法——使用nginx反向代理Gravatar头像、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)?$ {//开源软件:Vevb.com
- 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;
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.Vevb.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(emptyempty($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.Vevb.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.Vevb.com;
搞定收工.
新闻热点
疑难解答
图片精选