使用Nginx反代自建Gravatar头像服务器

今天发现头像一直加载不出来,登上梯子以后即可正常显示,看来我朝GFW又升级了,正常情况下secure.gravatar.com的地址一直正常使用的,看来以后似乎也无法正常使用了,于是就想到了自建Gravatar头像服务器,通过简单的反向代理即可实现是用自己的服务器提供头像服务。 由于我有一台东京节点的VPS,于是就拿来用了,Nginx配置(gravatar.liyang.io.conf)如下:[code]proxy_cache_path /data/wwwroot/gravatar.liyang.io 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.liyang.io.crt; ssl_certificate_key /usr/local/nginx/conf/ssl/gravatar.liyang.io.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.liyang.io; access_log /var/log/gravatar.liyang.io_nginx.log combined; index index.html index.htm index.php; root /data/wwwroot/gravatar.liyang.io; 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; } }[/code][size=15]其中,核心代码为:[/size] [code]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; }[/code][size=15]后面就可以使用gravatar.liyang.io来获取头像了。当然鉴于有些人服务器带宽流量比较紧张,只想自己来用,那么只需要加入如下代码即可:[/size] [code]location avatar/ { valid_referers none blocked *.liyang.io gravatar.liyang.io if ($invalid_referer) { return 403; } }[/code][code] location / { valid_referers none blocked *.liyang.io gravatar.liyang.io; if ($invalid_referer) { return 403; } proxy_redirect off; proxy_pass http://gravatar.com; add_header Nginx-Cache "$upstream_cache_status"; [/code][size=15]然后在主题模版中加入如下配置:[/size] [code]//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 );[/code][size=15]本站提供的地址可以无偿使用。[/size]
已邀请:

要回复问题请先登录注册