自建glowing-bear及nginx限定可访问后缀

平常用weechat+glowing-bear来连irc,glowing-bear官方提供的地址用了cloudfare的cdn资源,但cloudfare由于墙的原因访问很不稳定。专门用来跑一些web app的epiphany不能很好地支持翻墙,所以还是在自己的vps上搭一个glowing-bear。

安装glowing-bear

说起来搭一个glowing-bear真的超级简单,在nginx的webroot目录执行一下git-clone

git clone https://github.com/glowing-bear/glowing-bear.git

就可以通过/glowing-bear来访问了。

替换cloudflare资源

由于cloudflare上的资源都是放在https://cdnjs.cloudflare.com/下,可以使用nginx做一下反向代理:

location /cloudflare/ {
  proxy_pass https://cdnjs.cloudflare.com/;
}

在glowing-bear的目录下,执行:

find . -name "*.html" -exec sed 's/https:\/\/cdnjs\.cloudflare\.com/\/cloudflare/g' {} \;
find . -name "*.js" -exec sed 's/https:\/\/cdnjs\.cloudflare\.com/\/cloudflare/g' {} \;

nginx限定可访问后缀

由于glowing-bear是直接clone项目下来,里面有些文件是不需要也不应该被访问的,如.git。我们可以通过nginx,限制非特定后缀的文件被访问。在天真地用写代码的思维来写各种if判断后,我发现了nginx配置的几个点:

  1. if没有嵌套;
  2. if没有对应的else
  3. 不同module下{}里允许的内容是各不相同的,例如,if底下没有deny

几经艰辛,写出了可用的配置:

location ~ ^/glowing-bear/$ {
  allow all;
}
location ~ ^/glowing-bear/ {
  if ($request_uri !~ "^/glowing-bear/(?<request_uri_sub_path>[^?]*)(\?.*)?$") {    return 403;
  }
  if ($request_uri_sub_path ~* "^\.git.+$") {
    return 403;
  }
  if ($request_uri_sub_path !~* "^.*(\.js|\.css|\.svg|\.png|\.mp3|\.ogg|\.html)$|^$") {
    return 403;
  }
  allow all;
}
Show Comments