Skip to content

Instantly share code, notes, and snippets.

@JingwenTian
Created October 4, 2014 10:01
Show Gist options
  • Save JingwenTian/913f61b17a61856b78ac to your computer and use it in GitHub Desktop.
Save JingwenTian/913f61b17a61856b78ac to your computer and use it in GitHub Desktop.
nginx反代twitter完整镜像 (原文:http://jude.me/2014/10/04/twitter-mirror-2.html

示例

需要注意的几点

  • content-security-policy这个header要去掉,或者重写进你自己的域名,不然有些浏览器不会加载外部的元素
  • 使用proxy_cookie_domain替换cookies的作用域。twitter返回的cookies作用域是.twitter.com,也就是twitter.com的所有二级域名都可以调用,为使用者的安全考虑建议替换成反代所使用的二级域名比如tt.jude.me
  • 不仅html页面的域名需要替换,很多js和css中的域名也需要替换,注意proxy_set_header Accept-Encoding ‘’ 和subs_filter_types的配合使用
  • t.co短域名使用response header中的location进行跳转,对于站内的网址,t.co跳转后的域名始终是twitter.com,所以需要使用proxy_redirect把location重写到我们的反代域名下
  • 使用手机浏览器访问桌面版会自动跳转到移动版
  • 这东西和钓鱼站没啥区别了,代理服务器要想获取你的帐号密码或者cookies都很简单(我说的不是我自己),最好自己搭建并弄个简单的认证,不要随便使用网上别人的。
  • 因为这次使用的是二级域名,所以就不要再把相对地址替换为绝对地址,如果你想要在单个域名的目录下使用,还需要注意相对、绝对地址的替换
  • 其他的待发现,欢迎指出

移动端mobile.twitter.com的配置

server
{
listen 80;
listen 443 ssl spdy;
ssl_certificate /etc/ssl/jude.crt;
ssl_certificate_key /etc/ssl/jude.key;
server_name t.jude.me;
if ($server_port = 80) {
rewrite ^/(.*)$ https://t.jude.me/$1 redirect;
}
location /robots.txt{
alias /me/proxy/robots.txt;
}
location / {
proxy_hide_header content-security-policy;
proxy_set_header Accept-Encoding "";
subs_filter_types application/javascript text/javascript application/json;
proxy_set_header User-Agent "Opera/9.80 (Windows NT 6.2; WOW64) Presto/2.12.388 Version/12.14";
proxy_cookie_domain .twitter.com t.jude.me;
proxy_pass https://mobile.twitter.com/;
subs_filter mobile.twitter.com t.jude.me;
subs_filter '://t.co' 's://t.jude.me/t.co';
subs_filter '://ma.twimg.com' '://t.jude.me/ma.twimg.com';
subs_filter '://pbs.twimg.com' '://t.jude.me/pbs.twimg.com';
subs_filter '://twitter.com' '://t.jude.me/twitter.com';
}
location /t.co {
proxy_pass http://t.co/;
proxy_redirect http://twitter.com https://t.jude.me/$1;
}
location /ma.twimg.com {
proxy_pass https://ma.twimg.com/;
}
location /pbs.twimg.com {
proxy_pass https://pbs.twimg.com/;
}
location /twitter.com {
proxy_hide_header content-security-policy;
proxy_pass https://twitter.com/;
proxy_set_header Accept-Encoding "";
proxy_set_header User-Agent "Opera/9.80 (Windows NT 6.2; WOW64) Presto/2.12.388 Version/12.14";
subs_filter 't.co' 't.jude.me/t.co';
subs_filter 'ma.twimg.com' 't.jude.me/ma.twimg.com';
subs_filter 'pbs.twimg.com' 't.jude.me/pbs.twimg.com';
subs_filter 'twitter.com' 't.jude.me/twitter.com';
}
}

桌面端twitter.com反代配置

server
{
#	auth_basic	"hello";
# auth_basic_user_file /me/proxy/passwd;
listen 80;
listen 443 ssl spdy;
ssl_certificate /etc/ssl/jude.crt;
ssl_certificate_key /etc/ssl/jude.key;
server_name tt.jude.me;
if ($server_port = 80) {
rewrite ^/(.*)$ https://tt.jude.me/$1 redirect;
}
location /robots.txt{
alias /me/proxy/robots.txt;
}
location / {
proxy_hide_header content-security-policy;
proxy_set_header Accept-Encoding '';
proxy_pass https://twitter.com/;
proxy_cookie_domain .twitter.com tt.jude.me;
subs_filter_types application/javascript text/javascript application/json;
subs_filter 'twitter.com' 'tt.jude.me';
subs_filter '//t.co' '//tt.jude.me/t.co';
subs_filter '\/\/t.co' '\/\/tt.jude.me\/t.co';
subs_filter 'mobile.tt.jude.me' 't.jude.me';
subs_filter twitter.com tt.jude.me;
subs_filter 'abs.twimg.com' 'tt.jude.me/abs.twimg.com';
subs_filter 'pbs.twimg.com' 'tt.jude.me/pbs.twimg.com';
subs_filter 'o.twimg.com' 'tt.jude.me/o.twimg.com';
subs_filter 'amp.twimg.com' 'tt.jude.me/amp.twimg.com';
subs_filter 'ton.twimg.com' 'tt.jude.me/ton.twimg.com';
}
location /t.co {
proxy_pass http://t.co/;
proxy_redirect http://twitter.com https://tt.jude.me/$1;
}
location /abs.twimg.com {
proxy_pass http://abs.twimg.com/;
proxy_cookie_domain twitter.com tt.jude.me;
proxy_set_header Accept-Encoding '';
subs_filter_types application/javascript text/javascript;
subs_filter twitter.com tt.jude.me;
subs_filter 'abs.twimg.com' 'tt.jude.me/abs.twimg.com';
subs_filter 'pbs.twimg.com' 'tt.jude.me/pbs.twimg.com';
subs_filter 'o.twimg.com' 'tt.jude.me/o.twimg.com';
subs_filter 'amp.twimg.com' 'tt.jude.me/amp.twimg.com';
subs_filter 'ton.twimg.com' 'tt.jude.me/ton.twimg.com';
}
location /pbs.twimg.com {
proxy_pass http://pbs.twimg.com/;
proxy_set_header Accept-Encoding '';
subs_filter_types application/javascript text/javascript;
subs_filter twitter.com tt.jude.me;
subs_filter 'abs.twimg.com' 'tt.jude.me/abs.twimg.com';
subs_filter 'pbs.twimg.com' 'tt.jude.me/pbs.twimg.com';
subs_filter 'o.twimg.com' 'tt.jude.me/o.twimg.com';
subs_filter 'amp.twimg.com' 'tt.jude.me/amp.twimg.com';
subs_filter 'ton.twimg.com' 'tt.jude.me/ton.twimg.com';
}
location /amp.twimg.com {
proxy_pass https://amp.twimg.com/;
proxy_set_header Accept-Encoding '';
subs_filter_types application/javascript text/javascript;
subs_filter twitter.com tt.jude.me;
subs_filter 'abs.twimg.com' 'tt.jude.me/abs.twimg.com';
subs_filter 'pbs.twimg.com' 'tt.jude.me/pbs.twimg.com';
subs_filter 'o.twimg.com' 'tt.jude.me/o.twimg.com';
subs_filter 'amp.twimg.com' 'tt.jude.me/amp.twimg.com';
subs_filter 'ton.twimg.com' 'tt.jude.me/ton.twimg.com';
}
location /ton.twimg.com {
proxy_pass https://ton.twimg.com/;
proxy_set_header Accept-Encoding '';
subs_filter_types application/javascript text/javascript;
subs_filter twitter.com tt.jude.me;
subs_filter 'abs.twimg.com' 'tt.jude.me/abs.twimg.com';
subs_filter 'pbs.twimg.com' 'tt.jude.me/pbs.twimg.com';
subs_filter 'o.twimg.com' 'tt.jude.me/o.twimg.com';
subs_filter 'amp.twimg.com' 'tt.jude.me/amp.twimg.com';
subs_filter 'ton.twimg.com' 'tt.jude.me/ton.twimg.com';
}
}
@imzhi
Copy link

imzhi commented Jul 31, 2015

好东西,谢了~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment