使用Nginx反向代理谷歌字体库,可缓存加快访问速度。
谷歌字体库
比如以下地址,虽然不再被墙,但访问有时不太稳定,加载速度慢。
1
| https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,300italic,400italic,600italic,700italic
|
国内镜像
1 2 3 4
| http://fonts.useso.com/css?family=Source+Sans+Pro:300,400,600,300italic,400italic,600italic 360的,目前已不再维护
http://fonts.gmirror.org/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700
|
使用过程的一些问题
- 大部分镜像不支持https
- 打开页面的速度寄托在别人的服务器
- 特殊网络环境可能无法访问
搭建反向代理服务
准备以下应用环境,局域网/公网环境都可以,根据自己需要。
- 可访问的IP或域名(例如
fonts.yourdomain.com
)
- 机器环境 linux + nginx
安装nginx及第三方模块等就忽略了,直接贴上配置文件。
修改nginx缓存
1 2 3 4
| proxy_temp_file_write_size 128k; proxy_temp_path /data/nginx_cache/proxy_temp; proxy_cache_path /data/nginx_cache/proxy_cache levels=1:2 keys_zone=cache_one:200m inactive=30d max_size=30g;
|
其中,/data/nginx_cache/proxy_temp
和/data/nginx_cache/proxy_cache
是自定义的目录,如不存在请创建目录。
script1 2
| mkdir -p /data/nginx_cache/proxy_temp mkdir -p /data/nginx_cache/proxy_cache
|
缓存时间为30天,里面各参数根据自己需要进行修改。
反向代理配置
fonts.yourdomain.com.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| upstream google { server fonts.googleapis.com:443; }
upstream gstatic { server fonts.gstatic.com:443; }
server { listen 80;
server_name fonts.yourdomain.com;
resolver 8.8.8.8;
access_log /data/logs/nginx/fonts.yourdomain.com.access.log main; error_log /data/logs/nginx/fonts.yourdomain.com.error.log info;
location /css { sub_filter 'fonts.gstatic.com' 'fonts.yourdomain.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 https://google; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; }
location /icon { sub_filter 'fonts.gstatic.com' 'fonts.yourdomain.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 https://google; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; }
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 https://gstatic; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; } }
server { listen 443 ssl;
ssl on; ssl_certificate /root/ssl/css.crt; ssl_certificate_key /root/ssl/css.key;
ssl_prefer_server_ciphers on; ssl_dhparam /etc/ssl/certs/dhparam.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH+aRSA+RC4 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4"; keepalive_timeout 70; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
server_name fonts.yourdomain.com;
resolver 8.8.8.8;
location /css { sub_filter 'fonts.gstatic.com' 'fonts.yourdomain.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 https://google; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; }
location /icon { sub_filter 'fonts.gstatic.com' 'fonts.yourdomain.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 https://google; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; }
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 https://gstatic; proxy_cache cache_one; proxy_cache_valid 200 304 365d; proxy_cache_key $host$uri$is_args$args; expires max; } }
|