demo:https://lanse.tk
好久不见,colorful迎来了一个短暂到仅有12天的暑假,在暑假期间,有许多朋友们找我寻求能够上google的方法,因此搭建了此google镜像网站以供朋友们上网寻求资料。在此简要概述搭建方法。
:
由于不同的环境下操作有些不同,因此本次分版进行无nginx环境以及有nginx环境下的搭建方法。
无环境下的google镜像网站搭建
由于使用的nginx扩展驱动是两年前发布的,使用较旧版本的nginx相对会减少难度
准备
由于扩展驱动需要部分环境,所以我们提前安装好各种需要的指令以及安装包
- 首先安装gcc & git命令
yum -y install gcc
yun -y install git
yum -y install g++
- 下载nginx源码(1.7.8版本)(如果想要下载最新版本进行安装请查看右侧方法)
- 下载pcre(官网https://www.pcre.org)
- 下载openssl(官网https://www.openssl.org)
- 下载zlib(https://www.zlib.net)
wget "http://nginx.org/download/nginx-1.7.8.tar.gz"
wget "http://ftp.cs.stanford.edu/pub/exim/pcre/pcre-8.38.tar.gz"
wget "https://www.openssl.org/source/openssl-1.0.1j.tar.gz"
wget "http://zlib.net/fossils/zlib-1.2.8.tar.gz"
- 下载nginx扩展驱动
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
- 解压缩
tar xzvf nginx-1.7.8.tar.gz
tar xzvf pcre-8.38.tar.gz
tar xzvf openssl-1.0.1j.tar.gz
tar xzvf zlib-1.2.8.tar.gz
进入源码目录进行编译
cd nginx-1.7.8
- 这里添加:
--add-module=../ngx_http_google_filter_module \
--add-module=../ngx_http_substitutions_filter_module
- 全部代码如下:
./configure \
--prefix=/opt/nginx-1.7.8 \
--with-pcre=../pcre-8.38 \
--with-openssl=../openssl-1.0.1j \
--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 && make install
- 启动
sudo /opt/nginx-1.7.8/sbin/nginx
含发行版nginx迁移(含宝塔环境等)搭建镜像网站
从发行版迁移需要保持自己原有的扩展以及nginx版本,若版本不对可能会不支持某些扩展
准备
(在左边指令均已安装的情况下)
查看已安装nginx的版本及编译选项
nginx -V
如下为我的nginx编译选项
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1g 21 Apr 2020
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module \ --add-module=/www/server/nginx/src/ngx_cache_purge - --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=/www/server/nginx/src/pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module
- 下载对应nginx大版本
wget "http://nginx.org/download/nginx-1.18.0.tar.gz"
- 下载nginx扩展驱动
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module
git clone https://github.com/cuber/ngx_http_google_filter_module
- (此处可能需要安装依赖库的dev包)
- 对照自己发行版的configure参数进行configure
在原本的基础上添加ngx_http_substitutions_filter_module
以及ngx_http_google_filter_module
两个模块
此处注意新版本与旧版本的不同:
- 1.编译时使用
add-dynamic-module
,eg:
./configure \
<your configuration> \
--add-dynamic-module=</path/to/>ngx_http_google_filter_module \
--add-module=</path/to/>ngx_http_substitutions_filter_module
- 2.需要修改nginx.conf
查找nginx.conf:
ps -ef | grep nginx
## root 9817 1 0 Aug10 ? 00:00:01 nginx: master process /www/server nginx/sbin/nginx -c /www/server/nginx/conf/nginx.conf
## www 22034 9817 0 10:30 ? 00:00:01 nginx: worker process
## www 22035 9817 0 10:30 ? 00:00:00 nginx: cache manager process
## root 30665 28197 0 11:12 pts/1 00:00:00 grep --color=auto nginx
其中的manager process
后面为你的nginx.conf
的位置
修改nginx.conf
文件,向其中添加:
load_module modules/ngx_http_google_filter_module.so;
- 其余与旧版本一直,添加原有的模块
./configure --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-dynamic-module=/www/server/nginx/src/ngx_http_google_filter_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=/www/server/nginx/src/pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module
- 编译安装
make
- 覆盖原有的二进制文件(请先查找nginx的位置)
which nginx
## /usr/sbin/nginx
cp -rf objs/nginx /usr/sbin/nginx
- 重启nginx,出现添加的模组即可
service nginx restart
nginx -V
基本配置方法
如果你上面是跟着我的走,那么在这一步是不会出现问题的OwO
基本配置方法
http 配置方式
server {
server_name <你的域名>;
listen 80;
resolver 8.8.8.8;
location / {
google on;
}
}
https 配置方式
server {
server_name <你的域名>;
listen 443;
ssl on;
ssl_certificate <你的证书>;
ssl_certificate_key <你的私钥>;
resolver 8.8.8.8;
location / {
google on;
}
}
进阶配置
基本搜索
配置resolver
用于域名解析
server {
# ... 仅列举部分配置
resolver 8.8.8.8;
location / {
google on;
}
# ...
}
google学术 google_scholar
google_scholar
依赖于 google
, 所以 google_scholar
无法独立使用.
由于谷歌学术近日升级, 强制使用 https
协议, 并且 ncr
已经支持, 所以不再需要指定谷歌学术的 tld
location / {
google on;
google_scholar on;
}
语言偏好设置
location / {
google on;
google_scholar on;
# 设置成中文
google_language "zh-CN";
}
支持的语言:
ar -> 阿拉伯
bg -> 保加利亚
ca -> 加泰罗尼亚
zh-CN -> 中国 (简体)
zh-TW -> 中国 (繁体)
hr -> 克罗地亚
cs -> 捷克
da -> 丹麦
nl -> 荷兰
en -> 英语
tl -> 菲律宾
fi -> 芬兰
fr -> 法国
de -> 德国
el -> 希腊
iw -> 希伯来
hi -> 印地文
hu -> 匈牙利
id -> 印度尼西亚
it -> 意大利
ja -> 日本
ko -> 朝鲜
lv -> 拉脱维亚
lt -> 立陶宛
no -> 挪威
fa -> 波斯
pl -> 波兰
pt-BR -> 葡萄牙 (巴西)
pt-PT -> 葡萄牙 (葡萄牙)
ro -> 罗马尼亚
ru -> 俄罗斯
sr -> 塞尔维亚
sk -> 斯洛伐克
sl -> 斯洛文尼亚
es -> 西班牙
sv -> 瑞典
th -> 泰国
tr -> 土耳其
uk -> 乌克兰
vi -> 越南
搜索引擎爬虫许可
如果想要使用 google
自己的 robots.txt
请将 google_robots_allow
设为 on
#...
location / {
google on;
google_robots_allow on;
}
#...
upstreaming
upstream
减少一次域名解析的开销, 并且通过配置多个网段的 google ip 能够一定程度上减少被 google 机器人识别程序侦测到的几率 (弹验证码).
# 可以通过如下方法获取 google ip
➜ ~ dig www.google.com @8.8.8.8 +short
173.194.38.209
173.194.38.211
173.194.38.212
173.194.38.210
173.194.38.208
配置如下
upstream www.google.com {
server 173.194.38.209:443;
server 173.194.38.211:443;
server 173.194.38.212:443;
server 173.194.38.210:443;
server 173.194.38.208:443;
}
Proxy Protocal
默认采用 https
与后端服务器通信.
你可以使用 google_ssl_off
来强制将一些域降到 http
协议.
这个设置可以让一些需要二次转发的域通过 http
协议进行转发, 从而不再依赖 ssl
证书.
#
# 例如 'www.google.com' 按如下方式代理
# vps(hk) -> vps(us) -> google
#
#
# vps(hk) 配置
#
server {
# ...
location / {
google on;
google_ssl_off "www.google.com";
}
# ...
}
upstream www.google.com {
server < vps(us) 的 ip >:80;
}
#
# vps(us) 配置
#
server {
listen 80;
server_name www.google.com;
# ...
location / {
proxy_pass https://www.google.com;
}
# ...
}
- 最后,
service nginx restart
补充
我的配置
upstream www.google.com {
server 216.58.193.196:443;
}
server
{
listen 80;
listen 443 ssl http2;
server_name lanse.tk;
resolver 8.8.8.8;
# 防止网络爬虫
#forbid spider
if ($http_user_agent ~* "qihoobot|Baiduspider|Googlebot|Googlebot-Mobile|Googlebot-Image|Mediapartners-Google|Adsbot-Google|Feedfetcher-Google|Yahoo! Slurp|Yahoo! Slurp China|YoudaoBot|Sosospider|Sogou spider|Sogou web spider|MSNBot|ia_archiver|Tomato Bot")
{
return 403;
}
# 禁止用其他域名或直接用IP访问,只允许指定的域名
#forbid illegal domain
if ( $host != "lanse.tk" ) {
return 403;
}
location / {
google on;
google_ssl_off “www.google.com”;
google_scholar on;
google_language zh-CN;
}
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
#HTTP_TO_HTTPS_START
if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}
#HTTP_TO_HTTPS_END
ssl_certificate /www/server/panel/vhost/cert/lanse.tk/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/lanse.tk/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP引用配置,可以注释或修改
#PHP-INFO-END
#一键申请SSL证书验证目录相关设置
access_log /www/wwwlogs/lanse.tk.log;
error_log /www/wwwlogs/lanse.tk.error.log;
}
无模块设置location
如果编译时没有加 ngx_http_google_filter_module
模块,location的设置可以参考如下:(摘自https://zhgcao.github.io/2016/06/09/nginx-reverse-proxy-google/)
location / {
proxy_redirect off;
proxy_cookie_domain google.com <domain.name>;
proxy_pass https://www.google.com;
proxy_connect_timeout 60s;
proxy_read_timeout 5400s;
proxy_send_timeout 5400s;
proxy_set_header Host "www.google.com";
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Referer https://www.google.com;
proxy_set_header Accept-Encoding "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Accept-Language "zh-CN";
proxy_set_header Cookie "PREF=ID=047808f19f6de346:U=0f62f33dd8549d11:FF=2:LD=en-US:NW=1:TM=1325338577:LM=1332142444:GM=1:SG=2:S=rE0SyJh2W1IQ-Maw";
subs_filter https://www.google.com.hk <domain.name>;
subs_filter https://www.google.com <domain.name>;
#subs_filter_types text/css text/xml text/javascript;
sub_filter_once off;
}
本文地址: 搭建google镜像网站(适用最新版nginx)Module for Google Mirror