反向代理制作镜像站

Posted 黑白之道

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反向代理制作镜像站相关的知识,希望对你有一定的参考价值。


大家好,我是你们的老朋友Alex。今天教大家反向代理某网站…emmmm 勿喷~


首先申请一个域名,不申请也是可以的最好再申请一个SSL证书


https://github.com/Neilpang/acme.sh最新消息已支持野卡

具体怎么申请证书和配置我就不多说了,以前的文章我有写到。注意在编译nginx的时候加入http_sub_module

./configure --add-module=/root/ngx_brotli--add-module=/root/nginx-ct-1.3.2 --with-openssl=/root/openssl-OpenSSL_1_0_2h--with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module--with-http_sub_module


之后编辑nginx.conf文件

强制https跳转

return 301  https://www.xxxxx.win$request_uri;

之后修改server段增加443监听

   server {

       listen       443 ssl;

       server_name  域名;

             ssl on;

             #通信协议

             ssl_protocols TLSv1.2;

             #HTTPS证书

             ssl_certificate     /root/fullchain.cer;

             ssl_certificate_key /root/XXX.key;


             ssl_session_cache   shared:SSL:1m;

             ssl_session_timeout  5m;


             ssl_ciphers HIGH:!aNULL:!MD5:!EXPORT56:!EXP;

             ssl_prefer_server_ciphers  on;


       location / {

           proxy_pass  反代网址;

           sub_filter  反代网址 你的域名;

           #字符串只进行一次替换,即只替换第一个被匹配的字符串

                            sub_filter_once  off;

                            proxy_set_header  Host         "反代网址";

                            proxy_set_header  Referer      $http_referer;

                            proxy_set_header  X-Real-IP    $remote_addr;

                            proxy_set_header  User-Agent   $http_user_agent;

                            proxy_set_header  X-Forwarded-For   $proxy_add_x_forwarded_for;

                            proxy_set_header  X-Forwarded-Proto https;

                            #防止谷歌返回压缩的内容,因为压缩的内容无法替换字符串

                            proxy_set_header  Accept-Encoding   "";

                            proxy_set_header  Accept-Language   "zh-CN";

                            #cookie的作用域

                            proxy_cookie_domain  反代网址 你的域名;

                            #传固定的 cookie 给谷歌,是为了禁止即时搜索,因为开启即时搜索无法替换内容

                            proxy_set_header Cookie   "反向代理制作镜像站REF=ID=047808f19f6de346:U=0f62f33dd8549d11:FF=2反向代理制作镜像站D=en-US:NW=1:TM=1325338577反向代理制作镜像站M=1332142444:GM=1:SG=2:S=rE0SyJh2W1IQ-Maw";

                            #启用proxy_cache缓存

                            proxy_cache                 proxycache;

                            proxy_cache_valid           304 2h;

                            proxy_cache_valid           403 444 2h;

                            proxy_cache_valid           404 2h;

                            proxy_cache_valid           500 502 2h;

                            #proxy_cache_use_stale       invalid_header http_404 http_500http_502;

                            proxy_cache_use_staleupdating error timeout invalid_header http_500 http_502;

                            proxy_cache_lock            on;

                            proxy_cache_lock_timeout    5s;

       }

       auth_basic "Authorization";

       auth_basic_user_file /usr/local/nginx/html/passwdfile;

}


在启用proxy_cache 缓存的时候,需要在前面添加

http{                    proxy_cache_path /home/nginx/proxy_cache/cache levels=1:2 keys_zone=proxycache:60m max_size=120minactive=2h use_temp_path=on;

         proxy_temp_path   /home/nginx/proxy_cache/temp;

         proxy_cache_key   $host$uri;

}


以下参数可选,在http段中增加upstream指定域名解析,指定多个上游服务器来减少弹出异常流量判断验证码的几率

http{

         ......

         #weight 表示权重,数值越高被分配到的几率越大

         #下列 ip 多数为通过ipip.net 获取

         #你也可以自行添加更多 ip

         upstreamwww.xxxxx.com {

         #中国香港

         server216.58.221.68:443 weight=6;

         #中国台湾

         server74.125.23.99:443 weight=5;

         #日本东京都东京

         server172.217.25.68:443 weight=4;

         #日本东京都东京

         server216.58.200.196:443 weight=4;

         #日本大阪府大阪

         server216.58.197.4:443 weight=3;

         #新加坡

         server74.125.130.147:443 weight=2;

         #美国

         server216.58.217.196:443 weight=1;

         server172.217.11.164:443 weight=1;

         #美国

         server74.125.28.104:443 weight=1;

         #美国

         server74.125.28.147:443 weight=1;

         #美国华盛顿州西雅图

         server172.217.3.196:443 weight=1;

         }

}

站点启用密码验证,使用openssl对明文密码加密

Openssl passwd即可出现加密密码


最后创建认证文件以及配合nginx认证

echo "username:密文密码" > passwdfile

编辑nginx增加

双引号内写入表单的提示语,内容随意

auth_basic "Authorization";

这里填入认证文件的绝对路径

auth_basic_user_file .../passwdfile;


其实还有一种办法来实现简单反向代理,添加两个模块

ngx_http_google_filter_module


ngx_http_substitutions_filter_module


下载模块

git clone https://github.com/cuber/ngx_http_google_filter_module

git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module

编译进去

./configure \

--prefix=.../ \

--add-module=.../ngx_http_google_filter_module \

--add-module=.../ngx_http_substitutions_filter_module

替换 Nginx 二进制文件

cp ../sbin/nginx ../nginx.bak

cp ./objs/nginx ../sbin/

最后编辑nginx.conf

server {

                   listen 监听端口;

                   server_name 你的域名;

                   ssl on;

                   ssl_protocols TLSv1.2;

                   ssl_certificate ~/站点证书

                   ssl_certificate_key ~/站点证书密钥

                   location / {

                            #谷歌搜索

                            google on;

                            #谷歌学术

                            google_scholar off;

                            #语言偏好

                            google_language "zh-CN";

                            #指定任何搜索引擎都不允许爬取此镜像

                            google_robots_allow off;

                   }

}

教程结束!

你会喜欢






以上是关于反向代理制作镜像站的主要内容,如果未能解决你的问题,请参考以下文章

结合Nginx反向代理搭建域名被墙网站的镜像网站

月考结果公布,自己搭建 Google 反向代理网站

nginx反向代理网站镜像

nginx反向代理网站镜像

linux_nginx反向代理

反向代理的有趣用法