Gravatar头像服务,国内经常加载不出来,严重影响体验,自建一个Gravatar头像代理可以解决这个问题。

通过反代来实现,以下是Nginx配置代码:

proxy_cache_path  /data/wwwroot/gravatar.ucwm.com  levels=1:2   keys_zone=gravatar:10m inactive=7d max_size=1g; 
server {
  listen 80;
  listen 443 ssl http2;
  ssl_certificate /usr/local/nginx/conf/ssl/gravatar.ucwm.com.crt;
  ssl_certificate_key /usr/local/nginx/conf/ssl/gravatar.ucwm.com.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  ssl_prefer_server_ciphers on;
  ssl_session_timeout 10m;
  ssl_session_cache builtin:1000 shared:SSL:10m;
  ssl_buffer_size 1400;
  add_header Strict-Transport-Security max-age=15768000;
  ssl_stapling on;
  ssl_stapling_verify on;
  server_name gravatar.ucwm.com;
  access_log /var/log/gravatar.ucwm.com_nginx.log combined;
  index index.html index.htm index.php;
  root /data/wwwroot/gravatar.ucwm.com;
  if ($ssl_protocol = "") { return 301 https://$host$request_uri; }
  
  include /usr/local/nginx/conf/rewrite/none.conf;
  #error_page 404 /404.html;
  #error_page 502 /502.html;
  if ( $query_string ~* ".*[\;'\<\>].*" ){
        return 404;
  }
    location / {
        proxy_redirect                    off;
        proxy_pass                        https://secure.gravatar.com;
        add_header                        Nginx-Cache            "$upstream_cache_status";
        proxy_connect_timeout            180;
        proxy_send_timeout                180;
        proxy_read_timeout                180;
        proxy_buffer_size                128k;
        proxy_buffers                    4                    128k;
        proxy_busy_buffers_size            128k;
        proxy_temp_file_write_size        128k;
        proxy_cache                        gravatar;
        proxy_cache_valid                200                    302        1h;
        proxy_cache_valid                404                    1m;
        proxy_cache_key                    $uri$is_args$args;
        proxy_set_header                Host                $host;
        proxy_set_header                Accept-Encoding        "";
        proxy_set_header                X-Real-IP            $remote_addr;
        proxy_set_header                X-Forwarded-For        $proxy_add_x_forwarded_for;
  }
  location ~ /\.ht {
    deny all;
  }
}

防止盗链,需加入如下代码:

 location avatar/ {
            valid_referers none blocked *.ucwm.com gravatar.ucwm.com
            if ($invalid_referer) {
                    return 403;
            }
    }
    location / {
        valid_referers none blocked *.ucwm.com gravatar.ucwm.com;
            if ($invalid_referer) {
                return 403;
            }
        proxy_redirect                    off;
        proxy_pass                        http://gravatar.com;
        add_header                        Nginx-Cache            "$upstream_cache_status";

在主题模版中加入如下配置:

//gravatar反代
function myself_get_avatar($avatar) {
    $avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.ucwm.com",$avatar);
    return $avatar;
}
add_filter( 'get_avatar', 'myself_get_avatar', 10, 3 );

gravatar.ucwm.com还未搭建,先记录以下,等有时间再来搭建。


人之渺小,如蜉蝣于天地。