ravatar是wordpress默认引用的头像,很多wordpress用户发现gravatar头像被墙了,今天看了下用cn.gravatar.com/avatar/*的时候延迟又很慢,所以就自建gravatar服务器反代服务器。方法的原理是:官方的Gravatar 头像所在的路径一般都是www.gravatar.com、0.gravatar.com、1.gravatar.com、2.gravatar.com 这四个,我们只需要将头像默认的URL换成我们自己的反代服务器地址就行了。
Nginx配置
[root@Legion-JP-Linone vhost]# cat /usr/local/nginx/conf/vhost/gravatar.legion.pt.conf | grep -Ev '^(\s+)?#'
proxy_cache_path /home/wwwroot/gravatar.legion.pt/cache levels=1:2 keys_zone=gravatar:10m inactive=7d max_size=1g;
server {
listen 80;
server_name gravatar.legion.pt cngravatar.legion.pt;
access_log /home/wwwlogs/gravatar.legion.pt_nginx.log combined;
include none.conf;
if ( $query_string ~* ".*[\;'\].*" ){
return 404;
location ~ .*\.(wma|wmv|asf|mp3|mmf|zip|rar|jpg|gif|png|swf|flv)$ {
if ($invalid_referer) {
return 403;
location avatar/ {
if ($invalid_referer) {
return 403;
location / {
valid_referers none blocked *.legion.pt gravatar.legion.pt cngravatar.legion.pt *.dwhd.org;
if ($invalid_referer) {
return 403;
proxy_redirectoff;
proxy_passhttp://gravatar.com;
add_headerNginx-Cache"$upstream_cache_status";
proxy_connect_timeout180;
proxy_send_timeout180;
proxy_read_timeout180;
proxy_buffer_size128k;
proxy_buffers4128k;
proxy_busy_buffers_size128k;
proxy_temp_file_write_size128k;
proxy_cachegravatar;
proxy_cache_valid2003021h;
proxy_cache_valid4041m;
proxy_cache_key$uri$is_args$args;
proxy_set_headerHost$host;
proxy_set_headerAccept-Encoding"";
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
subs_filter_typestext/css text/xmlapplication/xmlapplication/xml+rss;
subs_filter http://gravatar.com http://gravatar.legion.pt;
在wordpress主题的functions.php文件中加入
//gravatar反代
function mytheme_get_avatar($avatar) {
$avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.legion.pt",$avatar);
return $avatar;
add_filter( 'get_avatar', 'mytheme_get_avatar', 10, 3 );
具体效果可以看本站,本站使用gravatar.legion.pt代替了gravatar官方的服务器,请勿直接使用本站的反代地址,如果需要使用请留言我给你做nginx防盗链白名单
原文链接:https://blog.csdn.net/weixin_39830688/article/details/119265581