«

两种方式反代Google(镜像)--nginx反代和nginx扩展

时间:2017-9-24 07:38     作者:admin     分类: Linux


写这篇文章的缘由是看见了我的博友Secret他写了一篇文章:

造轮子之谷歌镜像站 让我想起了 之前自己折腾过的nginx扩展镜像Google,效率比这个高,而且支持高级的配置,多级配合组成类似集群的功能,今天又折腾了一下,所以写一下过程,以方便后来需要的人.

声明:请在法律允许范围内合理使用搜索引擎,本文只作为技术笔记,不负任何责任.


  1. 更新库
  2. apt-get update
  3.  
  4. # 安装 gcc & git
  5. apt-get install build-essential git gcc g++ make -y
  6.  
  7. # nginx 官网: http://nginx.org/en/download.html
  8. wget "http://nginx.org/download/nginx-1.8.1.tar.gz"
  9.  
  10. # pcre 官网:http://www.pcre.org/
  11. wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz"
  12.  
  13. # opessl 官网:https://www.openssl.org/
  14. wget "https://www.openssl.org/source/openssl-1.0.1t.tar.gz"
  15.  
  16. # zlib 官网:http://www.zlib.net/
  17. wget "http://zlib.net/zlib-1.2.8.tar.gz"
  18.  
  19. # 下载本扩展
  20. git clone https://github.com/cuber/ngx_http_google_filter_module
  21.  
  22. # 下载 substitutions 扩展
  23. git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
  24.  
  25. # 解压缩
  26. 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
  27.  
  28. # 进入 nginx 源码目录
  29. cd nginx-1.8.1
  30.  
  31. # 创建 nginx 安装目录
  32. mkdir /usr/local/nginx-1.8.1

编译nginx及其扩展

  1. # 设置编译选项
  2. ./configure \
  3. --prefix=/usr/local/nginx-1.8.1 \
  4. --with-pcre=../pcre-8.39 \
  5. --with-openssl=../openssl-1.0.1t \
  6. --with-zlib=../zlib-1.2.8 \
  7. --with-http_ssl_module \
  8. --add-module=../ngx_http_google_filter_module \
  9. --add-module=../ngx_http_substitutions_filter_module
  10.  
  11. # 编译, 安装
  12. # 如果扩展有报错, 请发 issue 到
  13. # https://github.com/cuber/ngx_http_google_filter_module/issues
  14. make
  15. make install

 最后启动nginx,访问你的服务器IP或者是解析到上面的域名,即可看到nginx是否安装好.

ngx_http_google_filter_module项目github地址(他那里也有说明,不过是英文的,能看懂的可以直接去看原文):

https://github.com/cuber/ngx_http_google_filter_module


下面说一下nginx的配置:

  1. 简单的单机配置https,已经不支持http反代了
  1. server {
  2. server_name <你的域名>;
  3. listen 443;
  4.  
  5. ssl on;
  6. ssl_certificate <你的证书>;
  7. ssl_certificate_key <你的私钥>;
  8.  
  9. resolver 8.8.8.8;
  10. location / {
  11. google on;
  12. }
  13. }
  1. 进阶配置:配置多个服务器来缓解并发和出现验证码的频率

google_scholar 依赖于 google, 所以 google_scholar 无法独立使用. 由于谷歌学术近日升级, 强制使用 https 协议, 并且 ncr 已经支持, 所以不再需要指定谷歌学术的 tld

  1. location / {
  2. google on;
  3. google_scholar on;
  4. # 设置成德文,默认的语言是中文简体
  5. google_language "de";
  6. }

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/

http://coolnull.com/4245.html

就到这里了.下次再见:) 最后 欢迎访问我的Google镜像:gg.mrxn.net

标签: google nginx 编译 代理

版权所有:Mrxn's Blog
文章标题:两种方式反代Google(镜像)--nginx反代和nginx扩展
除非注明,文章均为 Mrxn's Blog 原创,请勿用于任何商业用途,转载请注明作者和出处 Mrxn's Blog

扫描二维码,在手机上阅读

推荐阅读:

评论:
avatar
Secret 2017-09-25 09:17
确实验证码很头疼,碰到了不挂梯子的话基本就完了。
commentator
Mrxn 2017-09-28 13:49
@Secret:所以呀 nginx扩展这种小范围使用是最省心的
avatar
李明 2017-09-24 10:16
很专业的文章
commentator
Mrxn 2017-09-24 12:46
@李明:不专业 哈哈 只不过把自己的过程写下来 当做笔记吧