反向代理 VS URL重写

URL重写(.htaccess)只是将匹配的URL转发出去而已。如果转发出去的URL被GFW封锁,国内的用户就不能访问。余博客的音乐试听,音乐文件存放在onedrive(skydrive)上,虽然onedrive没被封,但获取动态链接是通过部署在GAE上的skydrive-gae,所以这音乐试听一直都是海外专用啊

.htaccess

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ http://your-app-id.appspot.com/cid-xxxxxxxxxxxxxxxx.office.live.com/self.aspx/.Public/$1

不理会skydrive-gae的内部实现,从浏览器的console都能看到两次302跳转,第一次从自己的域名跳到appspot.com,第二次从appspot.com跳到onedrive的动态地址。

然而,既然从appspot.com返回来的不是二进制文件内容而是302跳转,那么通过apache的反向代理(proxy_http)先取得appspot.com返回来的response header,并原封不动再送到浏览器,可一举突破防火墙。同时,由于转送的数据是response header,并不会显著加重apache服务器的负担,也不会大幅消耗服务器的带宽,浏览器看到的跳转降为1次。因此这种场合,更适用反向代理。

如果正在使用较新的ubuntu,启用proxy_http非常简单:

sudo a2enmod proxy_http

打开 /etc/apache2/sites-available/default.conf,按如下添加ProxyRequests, ProxyPass 和 ProxyPassReverse三行:

<VirtualHost *:80>
    ...
    DocumentRoot /var/www
    ServerName your.domain.com
    ProxyRequests Off
    ProxyPass /music/ http://your-app-id.appspot.com/cid-xxxxxxxxxxxxxxxx.office.live.com/self.aspx/.Public/
    ProxyPassReverse /music/ http://your-app-id.appspot.com/cid-xxxxxxxxxxxxxxxx.office.live.com/self.aspx/.Public/
    ...
</VirtualHost>

重启apache服务器反向代理就生效了。

实践证明,即使是onedrive上的图片,也能外链了。

本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!: