前言

在公司科学上网使用谷歌经常出现很长一段时间访问不了,严重影响工作效率,没办法只能自己搭建一个镜像网站。

正文

环境 debian + nginx
  1. 安装 nginx

    apt install nginx
    
  2. 增加 nginx 配置,在/etc/nginx/conf.d文件夹内新增google.com.conf配置文件,配置文件内容为:

     server {
         server_name mygoogle.com;
         location / {
             proxy_pass https://www.google.com/;
             proxy_redirect https://www.google.com/ /;
             proxy_cookie_domain google.com mygoogle.com;
             proxy_set_header User-Agent $http_user_agent;
    
             # 设置这个可以避免跳回google
             proxy_set_header Cookie "PREF=ID=047808f19f6de346:U=0f62f33dd8549d11:FF=2:LD=zh-CN:NW=1:TM=1325338577:LM=1332142444:GM=1:SG=2:S=rE0SyJh2W1IQ-Maw";
    
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
             subs_filter  http://www.google.com https://mygoogle.com;
             subs_filter  https://www.google.com https://mygoogle.com;
             subs_filter  http://google.com https://mygoogle.com;
             subs_filter  https://google.com https://mygoogle.com;
         }
     }

    注:请手动更改配置中的mygoogle.com为自己的域名地址

  3. 载入配置

    nginx -s reload
  4. 添加 DNS 解析记录
    将自己的域名添加一条指向该台服务器 IP 的 DNS 解析记录,访问域名即可实现访问谷歌。
  5. 增加 Basic Auth 认证
    如果不想自己的谷歌镜像被别人乱用,可以增加 Basic Auth 来限制其他人使用。

    使用下列命令生成密码:

    printf "your_username:$(openssl passwd -crypt your_password)\n" >> /etc/nginx/conf.d/passwd
    

    配置 nginx,用 vim 修改刚刚的配置文件增加下列内容

    ...
    location / {
         auth_basic "Hello World";
         auth_basic_user_file conf.d/passwd;
         ...
         proxy_pass https://www.google.com/;
    ...
    

    然后nginx -s reload重启 nginx 生效。