自己反代 Google 字体库,实现国内/外均高速访问(其他公共库的原理相同)

2014-09-18 17:35:32 +08:00
 hjc4869
最近看到有人分享公共库镜像,比较适合国内主机使用,但我认为国外主机还是自己反代一下比较好。比如Linode,自己反代速度简直飞起来。
使用nginx来进行反向代理,可以自己添加SSL证书(免费的StartSSL也行)
然后添加这样的一个配置文件即可。这里的内容有精简,详细说明在这儿:
https://hjc.im/google-fonts-reverse-proxy/
可以做到限制特定域名使用,也就是说防止别人盗用。

nginx配置文件:

#所有fonts.ligstd.com替换为自己的字体库域名
upstream google {
server fonts.googleapis.com:80;
}

upstream gstatic {
server fonts.gstatic.com:80;
}

server {
listen 80;
listen [::]:80;

server_name fonts.ligstd.com;

location /css {
sub_filter 'fonts.gstatic.com' 'fonts.ligstd.com';
sub_filter_once off;
sub_filter_types text/css;
proxy_pass_header Server;
proxy_set_header Host fonts.googleapis.com;
proxy_set_header Accept-Encoding '';
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://google;
}

location / {
proxy_pass_header Server;
proxy_set_header Host fonts.gstatic.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://gstatic;
}
}

server {
listen 106.186.18.133:443 ssl spdy;
listen [2400:8900::f03c:91ff:fe73:bc8f]:443 ssl spdy;
ssl on;
ssl_certificate /root/fonts.ligstd.com/ssl.crt;
ssl_certificate_key /root/fonts.ligstd.com/ssl.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:RSA+3DES:!ADH:!AECDH:!MD5;
server_name fonts.ligstd.com;
location /css {
sub_filter 'http://fonts.gstatic.com' 'https://fonts.ligstd.com';
sub_filter_once off;
sub_filter_types text/css;
proxy_pass_header Server;
proxy_set_header Host fonts.googleapis.com;
proxy_set_header Accept-Encoding '';
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://google;
}

location / {
proxy_pass_header Server;
proxy_set_header Host fonts.gstatic.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://gstatic;
}
}
8298 次点击
所在节点    分享创造
10 条回复
MForever78
2014-09-18 19:57:39 +08:00
文章蛮赞,回去试一下。楼主喜欢用调皮的删除线 <del>另外你右下角的社交分享图标没对齐不开心</del>
hjc4869
2014-09-18 20:06:46 +08:00
@MForever78 下面几个国内的社交分享图标是我自己简单添加的,没有特别考虑对齐。。在IE和火狐上基本是齐的,但是到Chrome和Safari上就悲剧了,不知道为何。。
Showfom
2014-09-18 20:51:28 +08:00
楼主你这样的没设置缓存哦,建议再加上缓存,因为这些静态内容基本不怎么会变,可以设置 365 天的缓存 2333333
aheadlead
2014-09-18 21:07:26 +08:00
其实我一直没搞懂反向代理是什么...
对于wordpress默认主题有没有简单的方法可以解决字体问题?谢
emric
2014-09-18 21:14:31 +08:00
@aheadlead

可以去重置 Wordpress 默认库.

或者用现成的插件 http://www.soulteary.com/2014/06/15/replace-google-libs.html
hjc4869
2014-09-18 21:19:16 +08:00
@aheadlead (很多个)客户端 <----> (反向代理服务器 <----> 目标服务器)
SquirrelMAN
2014-09-19 16:11:35 +08:00
@hjc4869 请问反向代理是不是类似于VPN?
hjc4869
2014-09-19 18:26:25 +08:00
@SquirrelMAN VPN是虚拟网络,差别还是很大的。
Heracles
2014-09-19 23:16:40 +08:00
hjc4869
2014-09-19 23:57:04 +08:00
@Heracles 如果没记错的话360不支持HTTPS。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/134290

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX