标题:用自己的VPS搭建Google反向代理
作者:
日期:2015-04-04 22:35:19
内容:

一、准备工作
首先要感谢wen.lu的开源。参见GitHub地址:
https://github.com/cuber/ngx_http_google_filter_module

中文的说明文档:
https://github.com/cuber/ngx_http_google_filter_module/blob/master/README.zh-CN.md

有GitHub的可以fork一份到自己的主页。具体内容我就不复制过来了。

感谢V2EX。
http://www.v2ex.com/t/154344

其次要准备一台VPS,要在墙外(美国、日本、香港等,推荐
Bandwagonhost,搬瓦工VPS中文网:www.bandwagonhost.cn)。最好是装上Ubuntu14.04系统,因为目前网上流传的教程都是基于这个系统。下面开始教程。

二、教程

教程来自兽兽的通天塔,原文链接:
http://ttt.tt/162/
为防编辑,我复制如下:

一、编译安装 Nginx

1、首先更新下系统

 
sudo apt-get update && sudo apt-get upgrade

2、安装的 Nginx 需要的包以及 Git

 
sudo apt-get install libpcre3 libpcre3-dev zlib1g-dev libssl-dev build-essential git

3、新建立个 Nginx 目录,方便管理

mkdir nginx && cd nginx

下载 Nginx 最新稳定版,目前的版本是 1.6.2,用 Git 克隆两个 Nginx 模块,一个是 wen.lu 开源的
ngx_http_google_filter_module,另一个是 Nginx 替换关键词模块 ngx_http_substitutions_filter_module

wget http://nginx.org/download/nginx-1.6.2.tar.gz tar -xvf nginx-1.6.2.tar.gz git clone https://github.com/cuber/ngx_http_google_filter_module git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module 

4、进入 Nginx 目录并创建个 Nginx 临时文件夹

cd nginx-1.6.2 mkdir /var/tmp/nginx 

5、使用下面的参数开始编译

./configure \ --prefix=/usr --conf-path=/etc/nginx/nginx.conf --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi --http-scgi-temp-path=/var/tmp/nginx/scgi --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --with-http_ssl_module --with-http_gzip_static_module \ --add-module=/root/nginx/ngx_http_google_filter_module \ --add-module=/root/nginx/ngx_http_substitutions_filter_module 

PS:如果需要支持 IPv6 请别忘记增加 IPv6 模块 --with-ipv6

6、没问题以后直接用 make 安装

 
make && make install

二、开启 Nginx 服务

默认这么安装好以后每次检查配置、重启之类的操作略麻烦,所以我们模仿 Ubuntu 14.04 官方源,给系统设置个 nginx 服务,方便我们检查配置、启动重启关闭 Nginx 以及开机自动启动 Nginx

1、进入系统的 /etc/init.d 目录

 
cd /etc/init.d/

2、新建并编辑一个 nginx 文件

 
vi nginx

3、具体内容如下

#!/bin/sh  ### BEGIN INIT INFO # Provides:          nginx # Required-Start:    $local_fs $remote_fs $network $syslog # Required-Stop:     $local_fs $remote_fs $network $syslog # Default-Start:     2 3 4 5 # Default-Stop:      0 1 6 # Short-Description: starts the nginx web server # Description:       starts nginx using start-stop-daemon ### END INIT INFO  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/sbin/nginx NAME=nginx DESC=nginx  # Include nginx defaults if available if [ -f /etc/default/nginx ]; then    . /etc/default/nginx fi  test -x $DAEMON || exit 0  set -e  . /lib/lsb/init-functions  test_nginx_config() {    if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1; then       return 0    else       $DAEMON -t $DAEMON_OPTS       return $?    fi }  case "$1" in    start)       echo -n "Starting $DESC: "       test_nginx_config       # Check if the ULIMIT is set in /etc/default/nginx       if [ -n "$ULIMIT" ]; then          # Set the ulimits          ulimit $ULIMIT       fi       start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \           --exec $DAEMON -- $DAEMON_OPTS || true       echo "$NAME."       ;;     stop)       echo -n "Stopping $DESC: "       start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \           --exec $DAEMON || true       echo "$NAME."       ;;     restart|force-reload)       echo -n "Restarting $DESC: "       start-stop-daemon --stop --quiet --pidfile \           /var/run/$NAME.pid --exec $DAEMON || true       sleep 1       test_nginx_config       # Check if the ULIMIT is set in /etc/default/nginx       if [ -n "$ULIMIT" ]; then          # Set the ulimits          ulimit $ULIMIT       fi       start-stop-daemon --start --quiet --pidfile \           /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true       echo "$NAME."       ;;     reload)       echo -n "Reloading $DESC configuration: "       test_nginx_config       start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \           --exec $DAEMON || true       echo "$NAME."       ;;     configtest|testconfig)       echo -n "Testing $DESC configuration: "       if test_nginx_config; then          echo "$NAME."       else          exit $?       fi       ;;     status)       status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?       ;;    *)       echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest}" >&2       exit 1       ;; esac  exit 0 

4、赋予权限并增加到系统服务

sudo chmod +x ./nginx sudo update-rc.d nginx defaults 

三、修改默认的 nginx.conf 配置文件

默认官方的配置文件写的很简单,这里我们也模仿 Ubuntu 14.04 官方源修改一个适合我们的 Nginx 配置

 

1、编辑 /etc/nginx/nginx.conf

 
vi /etc/nginx/nginx.conf
具体内容如下:
worker_processes 4; pid /var/run/nginx.pid;  events {  worker_connections 768; }  http {   sendfile on;  tcp_nopush on;  tcp_nodelay on;  keepalive_timeout 65;  types_hash_max_size 2048;  server_tokens off;   access_log /var/log/nginx/access.log;  error_log /var/log/nginx/error.log;  proxy_temp_file_write_size 128k;  proxy_temp_path   /var/cache/nginx/temp;  proxy_cache_path  /var/cache/nginx/cache levels=1:2 keys_zone=cache_one:100m inactive=7d max_size=10g;   gzip_static on;  gzip on;  gzip_disable "msie6";   include /etc/nginx/sites-enabled/*; } 

2、新建几个必要的文件夹

其中 /etc/nginx/sites-enabled 用来放我们的网站配置文件,/var/log/nginx 用来放 log 日志文件,/var/cache/nginx/cache/var/cache/nginx/temp 则是 Nginx 反代缓存文件夹。
mkdir -p /etc/nginx/sites-enabled mkdir -p /var/log/nginx mkdir -p /var/cache/nginx/cache mkdir -p /var/cache/nginx/temp 

3、检查 Nginx 配置

直接运行 nginx -t 如果输出如下提示,则一切正常
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 

四、开启 Nginx SSL 支持

1、为了管理方便,我们建立个 ssl 目录

 
mkdir -p /root/ssl && cd /root/ssl

 

2、运行下面的命令,生成 example.com.keyexample.com.csr

openssl req -new -newkey rsa:2048 -nodes -out example.com.csr -keyout example.com.key -subj "/C=US/ST=CA/L=Los Angeles/O=Example Inc./OU=Web Security/CN=example.com" 
如果是泛域名证书,最后的域名改成 *.example.com

3、然后把 csr 文件提交给你的 SSL 证书商

验证好域名以后会颁发给你一个 .crt 文件,我们命名为 example.com.crt

4、接着我们新建一个配置文件,用来反代 Google

请自行更换配置文件里的域名和证书文件名
 
vi /etc/nginx/sites-enabled/google.conf
具体配置如下
server {  listen 80;  server_name example.com;  return 301 https://example.com$request_uri; }  server {  listen 443 ssl;  server_name example;    ssl on;  ssl_certificate /root/ssl/example.com.crt;  ssl_certificate_key /root/ssl/example.com.key;  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;  ssl_prefer_server_ciphers on;  ssl_ciphers ALL:!aNULL:!ADH:!eNULL:!LOW:!EXP:RC4+RSA:+HIGH:+MEDIUM;  keepalive_timeout 70;  ssl_session_cache shared:SSL:10m;  ssl_session_timeout 10m;   resolver 8.8.8.8;   location / {   google on;   google_scholar "scholar.google.com";  } } 

5、保存后重启下 Nginx

 
service nginx restart
OK,大功告成!浏览器里访问 https://example.com/ 看看是否已经可以反代 Google 了。

五、注意事项

1、Google 学术的域名各个地区的机房不一样,请先运行 curl -I scholar.google.com 看看是否跳转到了不同的域名,比如香港的服务器就会跳转到 scholar.google.com.hk,日本的就会跳转到 scholar.google.co.jp,那么相应的 google_scholar "scholar.google.com";这行就要修改成你服务器里访问到的域名
2、用的人过多以后 IP 会被 Google 限制,搜索的时候会要求输入验证码,这里我们的解决方案是通过 DNS 轮转到不同的服务器,这样就会有不同的出口 IP,当然自己用的话没啥问题。

三、一些问题的解决

1、nginx下安装SSL可能遇到的步骤问题

摘录一下兽兽另一个博客上关于nginx下安装SSL的步骤,仅作参考。原文:
http://zou.lu/nginx-https-ssl-module/

1、Nginx 配置 ssl 模块


默认 Nginx 是没有 ssl 模块的,而我的 VPS 默认装的是 Nginx 0.7.63 ,顺带把 Nginx 升级到 0.7.64 并且 配置 ssl 模块方法如下:


下载 Nginx 0.7.64 版本,解压 进入解压目录:

wget http://sysoev.ru/nginx/nginx-0.7.64.tar.gz  tar zxvf nginx-0.7.64.tar.gz  cd nginx-0.7.64
如果要更改header信息的话,

vi src/core/nginx.h  #define NGINX_VERSION      "0.7.62"  #define NGINX_VER          "nginx/" NGINX_VERSION

上面的版本号和nginx自己修改

编译

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module  make && make install
如果有 IPV6 模块,加上 –with-ipv6 参数:

./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-ipv6
(来源:http://www.hostloc.com/thread-4871-1-1.html

因为是小网站,用不着平滑升级,直接 killall nginx 杀掉 nginx 进程,然后 /usr/local/nginx/sbin/nginx 启动 nginx 即可
OK,升级并且安装好 ssl 模块完毕,这里我把 Nginx 修改成了 zoulu,于是乎:


怎么样,很有个性吧!

2、使用 OpenSSL 生成证书

①、生成RSA密钥的方法

openssl genrsa -out privkey.pem 2048
有的证书要 1024 的,所以得:
openssl genrsa -out privkey.pem 1024
②、生成一个证书请求
openssl req -new -key privkey.pem -out cert.csr
会提示输入省份、城市、域名信息等,重要的是,email 一定要是你的域名后缀的,比如 webmaster@zou.lu并且能接受邮件!

这样就有一个 csr 文件了,提交给 ssl 提供商的时候就是这个 csr 文件
(来源:
http://www.lsproc.com/blog/nginx_ssl_config/

直接 cat cert.csr

得到一大串字符,比如这样:

-----BEGIN CERTIFICATE REQUEST-----  MIIBsTCCARoCAQAwcTELMAkGA1UEBhMCQ04xCzAJBgNVBAgTAkhCMQwwCgYDVQQH  EwNTSloxDzANBgNVBAoTBkZhbmZvdTESMBAGA1UEAxMJZzFuZm91LmRlMSIwIAYJ  KoZIhvcNAQkBFhN3ZWJtYXN0ZXJAZmFuZm91LmRlMIGfMA0GCSqGSIb3DQEBAQUA  A4GNADCBiQKBgQC5l4PmZg6TCIpduefxq5gsLXN1JeQdBmUs+pEApeHmNoxE+R4k  VkQUJzLj5o3ltQGJzYrcIfru8NryQSxaT/5IjeFwS7nIMsx8KPkQQ71BJazsiZj+  CdLDRJj1m/SrjTsNrfYj4rFFS1FXq7uEDyreUx7fyAljx70jPSsGBOGwRQIDAQAB  oAAwDQYJKoZIhvcNAQEFBQADgYEACKCBQcnCq5yE3GFyN3NyxCQEvnspkIv9AqI4  FcwqyHPZWkupp3wfubHY80IwtfjlGlTSynzE7FZLVpcbNfKLnAYlYEwDY7NukJNy  pCbyqpJJXdAl3Jcun0NlLtSxTQpR+abO8va/BAO5Hp9h1rpSRtTdSJd2fC/owRV1  BfRuJnA=  -----END CERTIFICATE REQUEST-----
提交给你的 ssl 提供商即可,一般半个钟头到一天时间就会发给你证书,如图:




把所有文件全部上传到一个特定的目录,比如我是上传到 /root/zoulu/

这里,zoulukey.pem 和 zoulucert.csr 是自己在 VPS 生成的,剩下的都是证书签发机构颁发的。

一般情况下,直接用证书签发机构颁发的 crt 文件即可,比如 zou_lu.crt ,但是有很多证书签发机构默认在 Firefox 中文版下是不会信任的,经过仔细研究,终于发现,原来得把证书签发机构办法给你的 crt 文件也放入才行。

方法如下:

合并 PositiveSSLCA.crt (证书签发机构的 crt) 和 zou_lu.crt (自己域名的 crt)

cat  zou_lu.crt >> PositiveSSLCA.crt
mv PositiveSSLCA.crt  zou_lu.crt

或者直接用记事本打开,然后复制 PositiveSSLCA.crt 里面所有的内容到 zou_lu.crt 最下方即可。

(来源:
http://www.lsproc.com/blog/nginx_ssl_config/

2、PositiveSSL 证书手机认证不安全问题

如果你用的是PositiveSSL证书,那么很有可能会遇到这个问题,这里给出解决方法,原文:
http://blog.csdn.net/liwenzhu1989/article/details/8726417
positivessl.com购买下来的安全证书,直接使用到网站上在PC上是没有任何问题的,但是在手机上用chrome或者firefox打开,会说证书危险,不被信任的问题,这个原因是因为positivessl.com下载的证书,没有包含根证书和中间证书,只要把comodo给的根证书,中间证书追加到证书后面就可以认证安全了:

 
cat  STAR_domain_com.crt  AddTrustExternalCARoot.crt  PositiveSSLCA2.crt  >> STAR_domain_com.crt
 
 
之后这个新的证书就可以所有浏览器认证安全了。

其实就是把颁发的几个证书都合并到一个就行了。具体的证书名请以你自己收到的证书名为准。

四、写在最后

这样之后应该就不会有什么问题了。效果可以访问https://www.google.cn.vu体验一下。如果不想自己麻烦,也可以直接用我搭建的。

当然,还可以以此类推,反代Youtube,反代1024,反代各种网站,你懂的。

(via https://tang.biz/google-every-where.html)


返回列表 网站首页