#进击的学术# 利用Nginx反代Google及学术

nginx众所周知的原因,谷歌(Google)在我们的世界里总是若隐若现的,一些服务(如九尾搜索JWSS)为我们带来了便利,但是谷歌学术还是因为一些原因而很难稳定的使用下去,一方面也是谷歌在封锁一些公开的服务,于是,利用手上空余的VPS假设自己的Google服务就成了我等闲来无事折腾的目标。首先要感谢Cuber的ngx_http_google_filter_module的项目(Github地址),让nginx加上这个拓展之后,可以轻松实现Google的反代(微魔也不确定用反代这个词是否恰当)。

安装环境:Ubuntu 14.04,其他系统参照。另外,你还需要一个安全证书供https安全链接使用(参考《如何使用NameCheap赠送的SSL安全证书》,据说使用http很容易被封…所以https。)

1.安装相关软件

#
# 安装 gcc & git
apt-get install build-essential git gcc g++ make

# 下载Nginx最新版源码
# http://nginx.org/en/download.html
wget "http://nginx.org/download/nginx-1.9.2.tar.gz"

# 下载最新版 pcre
# http://www.pcre.org/
wget "ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.36.tar.gz"

# 下载最新版 openssl
# https://www.openssl.org/
wget "https://www.openssl.org/source/openssl-1.0.2c.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.9.2.tar.gz
tar xzvf pcre-8.36.tar.gz
tar xzvf openssl-1.0.2c.tar.gz
tar xzvf zlib-1.2.8.tar.gz


# 进入 nginx 源码目录
cd nginx-1.9.2

# 设置编译选项

./configure \
  --prefix=/opt/nginx-1.9.2 \
  --with-pcre=../pcre-8.36 \
  --with-openssl=../openssl-1.0.2c \
  --with-zlib=../zlib-1.2.8 \
  --with-http_ssl_module \
  --add-module=../ngx_http_google_filter_module \
  --add-module=../ngx_http_substitutions_filter_module

make
sudo make install

# 启动Nginx
sudo /opt/nginx-1.9.2/sbin/nginx

2.配置Nginx

编辑/opt/nginx-1.9.2/conf/nginx.conf

我的配置如下,红色部分为修改/追加于默认配置的内容

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

server {
#强制http跳转https
        listen 80;
        server_name 域名;
        rewrite ^/(.*) https://域名/$1 permanent;
}

    server {
        listen       443;
        server_name  域名;

        ssl on;
        ssl_certificate 证书.crt路径;
        ssl_certificate_key 证书.key路径;

        resolver 8.8.8.8;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                #添加Google和Google学术支持,重要!
                google on;
                google_scholar on;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

}

最后重载一下Nginx的配置,应该就能看到Google的页面了。

/opt/nginx-1.9.2/sbin/nginx -s reload

猜你 喜欢

关于作者: 微魔

小微魔,大智慧!

1 条评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注