nginx反向代理解决googleapis字体库问题

虽然有第三方的,但是自己nginx里面写死配置来得安稳些.

用法就是在网站目录里面直接引用一下配置文件include googleapis.conf;

这里做了一个虚拟目录/assets/vendor/, 可以根据自己的需求设置.

 

# cat ../googleapis.conf
sub_filter_once off;
sub_filter_types text/css text/xml text/javascript;
sub_filter "https://fonts.googleapis.com" "/assets/vendor/googleapis";
sub_filter "//fonts.googleapis.com" "/assets/vendor/googleapis";
sub_filter "https://ajax.googleapis.com" "/assets/vendor/ajax";
sub_filter "https://fonts.gstatic.com" "/assets/vendor/gstatic";

location /assets/vendor/googleapis {
  rewrite ^/assets/vendor/googleapis/(.+)$ /$1 break;
  sub_filter "https://fonts.gstatic.com" "/assets/vendor/gstatic";
  proxy_set_header Accept-Encoding "";
  proxy_pass https://fonts.googleapis.com;
  proxy_set_header Host "fonts.googleapis.com";
  proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0";
  expires 1y;
}
location /assets/vendor/gstatic {
  rewrite ^/assets/vendor/gstatic/(.+)$ /$1 break;
  proxy_pass https://fonts.gstatic.com:443;
  proxy_set_header Host "fonts.gstatic.com";
  expires 1y;
}
location /assets/vendor/ajax {
  rewrite ^/assets/vendor/ajax/(.+)$ /$1 break;
  proxy_pass https://gajax.googleapis.com;
  proxy_set_header Host ajax.googleapis.com;
  expires 1y;
}

添加新评论 »