linux,  nginx

Nginx反向代理Google

方法

Nginx动态添加模块(已经安装好的nginx,添加一个未被编译安装的模块)

模块介绍

ngx_http_google_filter_module:是一个过滤器模块,能够让谷歌镜像更便捷的部署。内建了正则表达式、URI locations和其他复杂的配置。原生nginx模块确保了更加高效地处理cookies, gstatic scoures和重定向。
ngx_http_substitutions_filter_module:此模块作用是替换过滤响应主体,nginx也有一个类似这样的原生模块,但有一个缺点,就是只能使用一条规则,而nginx_substitutions_filter则不限规则数量。

系统

ubuntu

具体:
1. 下载第三方扩展模块ngx_http_google_filter_module 和 ngx_http_substitutions_filter_module

cd /usr/local/src/ 
git clone https://github.com/cuber/ngx_http_google_filter_module
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_modul

2. 查看nginx编译安装时安装了哪些模块

nginx -V 
nginx version: nginx/1.22.0
built by gcc 9.3.0 (Ubuntu 9.3.0-10ubuntu2) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'

3. 加入需要安装的模块,重新编译。

安装编译环境

sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev openssl libssl-dev

下载与当前nginx版本相同的源码包

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.20.1.tar.gz

解压源码包,进入源码包

tar -zxvf nginx-1.20.1.tar.gz
cd nginx-1.20.1

开始编译,如这里添加

–add-dynamic-module=/usr/local/src/ngx_http_google_filter_module
–add-module=/usr/local/src/ngx_http_substitutions_filter_module

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.22.0/debian/debuild-base/nginx-1.22.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie' --add-dynamic-module=/usr/local/src/ngx_http_google_filter_module --add-module=/usr/local/src/ngx_http_substitutions_filter_module
make    //千万不要make install,不然就真的覆盖了

4. 将编译后生成的模块copy到/etc/nginx/modules/下:

cp /usr/local/src/nginx-1.20.1/objs/ngx_http_google_filter_module.so   /etc/nginx/modules/

5.将编译后生成的nginx二进制文件替换成原来的nginx二进制文件

cp /usr/sbin/nginx  /usr/sbin/nginx.bak
cp /usr/local/src/nginx-1.20.1/objs/nginx /usr/sbin/nginx

6.更改配置文件

修改nginx主配置文件

vim /etc/nginx/nginx.conf    #添加下面的内容
load_module modules/ngx_http_google_filter_module.so;

修改nginx站点配置文件

vim /etc/nginx/conf.d/google.sharespace.top.conf #添加下面了内容(获取ssl证书可参考https://technology.sharespace.top/certbot/)
server {
        listen       80;
        server_name google.sharespace.top;
	return 301 https://google.sharespace.top$request_uri;
}


server {
        listen 443 ssl;
        server_name google.sharespace.top;
        ssl_certificate /etc/letsencrypt/live/google.sharespace.top/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/google.sharespace.top/privkey.pem;
        ssl_session_timeout 10m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;
	ssl_ciphers CHACHA20:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-RC4-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:RC4-SHA:!aNULL:!eNULL:!EXPORT:!DES:!3DES:!MD5:!DSS:!PKS;
	ssl_session_cache builtin:1000 shared:SSL:10m;


	resolver 8.8.8.8 8.8.4.4 valid=300s;
	resolver_timeout 5s;

        location / {
		google on;
		google_scholar on;
		google_language en;  #语言偏好,默认使用 zh-CN (中文)
        }

}

检查语法重启服务

nginx -t && systemctl restart  nginx

7.测试

留言

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