Gravatar 头像服务入口, 都在海外, 经常超时, 所以我们在国内添加入口
http {
......
proxy_cache_path /dev/shm/proxy_cache_gravatar levels=1:2 keys_zone=ngx_proxy_cache_gravatar:8m inactive=1d max_size=128m;
......
}
server
{
listen 80;
listen [::]:80;
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name gravatar.aikaiyuan.com;
proxy_buffering on;
proxy_buffer_size 32k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 1m;
proxy_cache ngx_proxy_cache_gravatar;
proxy_ignore_headers Expires Set-Cookie Cache-Control;
proxy_hide_header Cache-Control;
proxy_hide_header Set-Cookie;
proxy_hide_header Expires;
proxy_pass_header X-Forwarded-For;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
add_header Aikaiyuan-Cache "$upstream_cache_status from Cache";
ssl_certificate ssl/gravatar.aikaiyuan.com.crt;
ssl_certificate_key ssl/gravatar.aikaiyuan.com.key;
include ssl/main.conf;
include HSTS.conf;
location = /favicon.ico {
root html;
}
location /avatar {
valid_referers none blocked aikaiyuan.com *.aikaiyuan.com;
if ($invalid_referer) {
rewrite ^/ http://www.aikaiyuan.com/;
}
proxy_cache_key $host$uri;
proxy_cache_valid 200 100d;
proxy_cache_valid 404 2s;
proxy_redirect / /;
proxy_pass http://secure.gravatar.com;
proxy_set_header Host "secure.gravatar.com";
proxy_set_header Accept-Encoding "";
proxy_set_header User-Agent $http_user_agent;
}
修改主题模版 function.php
添加如下配置:
//gravatar反代
function myself_get_avatar($avatar) {
$avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.liyang.io",$avatar);
return $avatar;
}
add_filter( 'get_avatar', 'myself_get_avatar', 10, 3 );
curl 测试
# curl -I 'https://gravatar.aikaiyuan.com/avatar/c5d991ee0c6fa4701b4cdd62edd15496?s=72&d=mm'
HTTP/1.1 200 OK
Server: openresty
Date: Mon, 25 Nov 2019 04:18:19 GMT
Content-Type: image/png
Content-Length: 6581
Connection: keep-alive
Last-Modified: Mon, 04 Nov 2019 07:37:47 GMT
Link: <https://www.gravatar.com/avatar/c5d991ee0c6fa4701b4cdd62edd15496?s=72&d=mm>; rel="canonical"
Content-Disposition: inline; filename="c5d991ee0c6fa4701b4cdd62edd15496.png"
Access-Control-Allow-Origin: *
X-nc: HIT bur 4
Source-Age: 1002368
Expires: Thu, 28 Nov 2019 04:18:19 GMT
Cache-Control: max-age=259200
Aikaiyuan-Cache: HIT from Cache
Strict-Transport-Security: max-age=31536000; includeSubDomains
Accept-Ranges: bytes
转载请注明:爱开源 » Nginx 代理 Gravatar头像服务