两种方式反代Google(镜像)--nginx反代和nginx扩展
时间:2017-9-24 07:38 作者:admin 分类: Linux
写这篇文章的缘由是看见了我的博友Secret他写了一篇文章:
造轮子之谷歌镜像站 让我想起了 之前自己折腾过的nginx扩展镜像Google,效率比这个高,而且支持高级的配置,多级配合组成类似集群的功能,今天又折腾了一下,所以写一下过程,以方便后来需要的人.
声明:请在法律允许范围内合理使用搜索引擎,本文只作为技术笔记,不负任何责任.
- 更新库
- apt-get update
- # 安装 gcc & git
- apt-get install build-essential git gcc g++ make -y
- # nginx 官网: http://nginx.org/en/download.html
- wget "http://nginx.org/download/nginx-1.8.1.tar.gz"
- # pcre 官网:http://www.pcre.org/
- wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz"
- # opessl 官网:https://www.openssl.org/
- wget "https://www.openssl.org/source/openssl-1.0.1t.tar.gz"
- # zlib 官网:http://www.zlib.net/
- wget "http://zlib.net/zlib-1.2.8.tar.gz"
- # 下载本扩展
- git clone https://github.com/cuber/ngx_http_google_filter_module
- # 下载 substitutions 扩展
- git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
- # 解压缩
- tar xzvf nginx-1.8.1.tar.gz && tar xzvf pcre-8.39.tar.gz && tar xzvf openssl-1.0.1t.tar.gz && tar xzvf zlib-1.2.8.tar.gz
- # 进入 nginx 源码目录
- cd nginx-1.8.1
- # 创建 nginx 安装目录
- mkdir /usr/local/nginx-1.8.1
编译nginx及其扩展
- # 设置编译选项
- ./configure \
- --prefix=/usr/local/nginx-1.8.1 \
- --with-pcre=../pcre-8.39 \
- --with-openssl=../openssl-1.0.1t \
- --with-zlib=../zlib-1.2.8 \
- --with-http_ssl_module \
- --add-module=../ngx_http_google_filter_module \
- --add-module=../ngx_http_substitutions_filter_module
- # 编译, 安装
- # 如果扩展有报错, 请发 issue 到
- # https://github.com/cuber/ngx_http_google_filter_module/issues
- make
- make install
最后启动nginx,访问你的服务器IP或者是解析到上面的域名,即可看到nginx是否安装好.
ngx_http_google_filter_module项目github地址(他那里也有说明,不过是英文的,能看懂的可以直接去看原文):
https://github.com/cuber/ngx_http_google_filter_module
下面说一下nginx的配置:
- 简单的单机配置https,已经不支持http反代了
- server {
- server_name <你的域名>;
- listen 443;
- ssl on;
- ssl_certificate <你的证书>;
- ssl_certificate_key <你的私钥>;
- resolver 8.8.8.8;
- location / {
- google on;
- }
- }
- 进阶配置:配置多个服务器来缓解并发和出现验证码的频率
google_scholar 依赖于 google, 所以 google_scholar 无法独立使用. 由于谷歌学术近日升级, 强制使用 https 协议, 并且 ncr 已经支持, 所以不再需要指定谷歌学术的 tld
- location / {
- google on;
- google_scholar on;
- # 设置成德文,默认的语言是中文简体
- google_language "de";
- }
Upstreaming
upstream 减少一次域名解析的开销, 并且通过配置多个网段的 google ip 能够一定程度上减少被 google 机器人识别程序侦测到的几率 (弹验证码). upstream 参数要放在 http{} 中(也就是放在server{}配置外),注意这个参数只有你加了SSL证书是https的时候才会有效,否则会报错! 寻找这个参数的谷歌IP很简单,在你的VPS上面 ping www.google.com ,获得的IP把最后一位数 加1或者减1 就行了。
upstream www.google.com { server 173.194.38.1:443; server 173.194.38.2:443; server 173.194.38.3:443; server 173.194.38.4:443; }
Proxy Protocol--代理保护
默认情况下,代理将使用https与后端服务器通信。您可以使用google_ssl_off强制某些域名回退到http协议。如果要通过没有ssl证书的另一个网关来代理某些域,这是非常有用的。
# # eg. # i want to proxy the domain 'www.google.com' like this # vps(hk) -> vps(us) -> google # # # configuration of vps(hk) # server { # ... location / { google on; google_ssl_off "www.google.com"; } # ... } upstream www.google.com { server < ip of vps(us) >:80; } # # configuration of vps(us) # server { listen 80; server_name www.google.com; # ... location / { proxy_pass https://www.google.com; } # ... }所有的这些配置都是在全新的机器上配置,如果你已经配置好了nginx那么,也很容易,你只需要重新添加扩展动态编译进去就好了,编译完切记不要make install,只需要make编译,然后覆盖就行.
./configure \
--prefix=/usr/local/nginx \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_v2_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
.......
--add-module=/data/software/ngx_http_google_filter_module \
--add-module=/data/software/ngx_http_substitutions_filter_module
注意:你需要在nginx的源码包文件夹下面执行这个./configure命令,使用之前先使用 nginx-V 查看nginx版本下载相同版本的源码包,添加扩展的时候要注意路径,在最好复制的时候先停止nginx,同时以防万一,先拷贝一份nginx在覆盖.参考资料如下:
http://imshusheng.com/linux/173.html
http://www.ttlsa.com/nginx/how-to-install-nginx-third-modules/
就到这里了.下次再见:) 最后 欢迎访问我的Google镜像:gg.mrxn.net
推荐阅读: