很多时候想在中文维基百科上查一些词条,但是又不方便开VPN,配置一个自用的反向代理还是不错的选择。
Caddy 是一个用 Golang 开发的 web server,自动申请并配置 Let's Encrypt 证书的功能是其最大亮点,所以我决定在 Caddy 上配置我的 Wikipedia 反向代理。
首先,需要在 Caddy 的官网下载并安装 Caddy server,注意务必要勾选http.filter 这个插件,不然我们的反向代理无法对页面的内容进行链接的过滤和替换。
Caddy 的配置非常简单,无需多言,我就在这里贴一下我用来代理 Wikipedia 的配置:
首先是主站的配置,使用http.filter插件把所有的zh.wikipedia.org链接都替换成wiki.example.com,此外,还需要把所有/的请求都301重定向到/wiki/Wikipedia:首页,不然会自动跳转回zh.wikipedia.org。
wiki.example.com {
gzip
proxy / https://zh.wikipedia.org {
header_upstream X-Real-IP {remote}
header_upstream User-Agent {>User-Agent}
header_upstream Accept-Encoding identity
}
redir 301 {
if {path} is /
/wiki/Wikipedia:首页
}
filter rule {
content_type text/.*
search_pattern zh.wikipedia.org
replacement wiki.example.com
}
filter rule {
content_type text/.*
search_pattern zh.m.wikipedia.org
replacement m.wiki.example.com
}
filter rule {
content_type text/.*
search_pattern upload.wikimedia.org
replacement up.wiki.example.com
}
}
如果用手机浏览器访问,会跳转到手机版网页,因此也需要做同样的替换。
m.wiki.example.com {
gzip
proxy / https://zh.m.wikipedia.org {
header_upstream X-Real-IP {remote}
header_upstream User-Agent {>User-Agent}
header_upstream Accept-Encoding identity
}
redir 301 {
if {path} is /
/wiki/Wikipedia:首页
}
filter rule {
content_type text/.*
search_pattern zh.wikipedia.org
replacement wiki.example.com
}
filter rule {
content_type text/.*
search_pattern zh.m.wikipedia.org
replacement m.wiki.example.com
}
filter rule {
content_type text/.*
search_pattern upload.wikimedia.org
replacement up.wiki.example.com
}
}
顺便也给媒体资源做一个反向代理。
up.wiki.example.com {
gzip
proxy / https://upload.wikimedia.org {
header_upstream X-Real-IP {remote}
header_upstream User-Agent {>User-Agent}
}
}
DNS配置:
不管是wiki.example.com还是m.wiki.example.com还是up.wiki.example.com都需要添加A记录指向 Caddy 所在的服务器IP,Caddy启动后会自动对各个域名解析进行验证并向 Let's Encrypt 申请并配置 TLS 证书,完全不用考虑如何配置 TLS 证书。
已知问题:
搜索不可用,因为 Wikipedia 的搜索有时候会触发到目的词条的 302 重定向,而 302 响应的 header 中的location字段目前无法通过http.filter插件进行查找和替换,因此仍然会跳转到zh.wikipedia.org。
另外,目前我只配置了中文维基百科的反向代理,毕竟英文版的还能访问不是么。
Leave a Comment