自建谷歌镜像

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:9 个月前

使用 nginx 反向代理谷歌

大提前:拥有一台国外 VPS

搭建好的地址如下:

项目地址:https://github.com/cuber/ngx_http_google_filter_module,这个项目是老项目了,可能不太支持高版本的nginx。这里可以参考这篇博客:https://blog.oyi.me/619

搭建方法(这里以已经安装了nginx为例(比如使用 apt install nginx 已经安装过了))

主要是使用了两个nginx的模块 ngx_http_substitutions_filter_modulengx_http_google_filter_module

下载模块

1
2
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
git clone https://github.com/cuber/ngx_http_google_filter_module

查看已经安装的nginx的编译参数

1
nginx -V

输出信息如下,后续主要用的就是configure arguments后的内容(这里我已经是重新编译后的参数了)

1
2
3
4
5
nginx version: nginx/1.18.0
built by gcc 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-lUTckl/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-module=/etc/nginx/modules-available/ngx_http_substitutions_filter_module --add-dynamic-module=/etc/nginx/modules-available/ngx_http_google_filter_module

下载对应版本的nginx源码,各版本地址:http://nginx.org/download

1
wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压重新编译, 编译时使用 add-dynamic-module 和 add-module

1
2
3
4
5
6
7
tar -xzvf nginx-1.18.0.tar.gz && cd nginx-1.18.0
./configure \
<your configuration> \
--add-dynamic-module=</path/to/>ngx_http_google_filter_module \
--add-module=</path/to/>ngx_http_substitutions_filter_module
# 编译
make

这里不要进行 make install , 上述编译完就在 objs 目录下生成了nginx文件

替换原有的 nginx 文件, 可使用 which nginx 查找位置,然后使用 cp 替换

1
cp -rf objs/nginx /usr/sbin/nginx

修改 nginx.conf,一定要在文件开头把动态包加载进去

1
load_module /xxx路径/objs/ngx_http_google_filter_module.so;

server配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 server {
server_name <你的域名>;
listen 443;

ssl on;
ssl_certificate <你的证书>;
ssl_certificate_key <你的私钥>;

resolver 8.8.8.8;
location / {
google on;
google_scholar on;
}
}

重启nginx

1
service nginx restart 或者 nginx -s reload