使用 Nginx的反代功能配置gravatar头像服务器的反代节点

  • A+
所属分类:随手小记
摘要

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)$ {
		valid_referers none blocked *.legion.pt gravatar.legion.pt cngravatar.legion.pt;
		if ($invalid_referer) {
			return 403;
		}
	}

	location avatar/ {
			valid_referers none blocked *.legion.pt gravatar.legion.pt cngravatar.legion.pt;
			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_redirect					off;
		proxy_pass						http://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;
		subs_filter_types				text/css text/xml	application/xml	application/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防盗链白名单

lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:1   其中:访客  1   博主  0

    • avatar Vans爱好者 1

      最简单的方法是,直接用七牛建立一个空间,然后镜像cn.gravatar.com,再通过functions.php的代码,将原本的gravatar地址改成七牛的CDN地址,即可。