vi /usr/local/nginx/conf/vhost/www.abc.com.conf
    # 自行套用为你的域名
    # 打开后按 I键,进入编辑模式,然后套用最下面的配置文件示例,粘贴进去后,按 Esc键 退出编辑模式,然后输入 :wq 保存并退出。
        #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }

HTTP 示例

    server
        {
            listen 80;
            server_name www.abc.com;
            
            if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
            return  403;
            }
      
            location / {
            sub_filter www.baidu.com www.abc.com;
            sub_filter_once off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Referer http://www.baidu.com;
            proxy_set_header Host www.baidu.com;
            proxy_pass http://www.baidu.com;
            proxy_set_header Accept-Encoding "";
            }
    }

HTTPS 示例

    server
        {
            listen 80;
            listen 443 ssl;
            ssl on;
            ssl_certificate /root/ssl.crt;
            ssl_certificate_key /root/ssl.key;
            ssl_session_cache shared:SSL:10m;
            ssl_session_timeout  10m;
            server_name www.abc.com;
            add_header Strict-Transport-Security "max-age=31536000";
            
            if ( $scheme = http ){
                return 301 https://$server_name$request_uri;
            }
            
            if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
            return  403;
            }
      
            location / {
            sub_filter www.baidu.com www.abc.com;
            sub_filter_once off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Referer https://www.baidu.com;
            proxy_set_header Host www.baidu.com;
            proxy_pass https://www.baidu.com;
            proxy_set_header Accept-Encoding "";
            }
    }

重启nginx

service nginx restart

另一种方式

Nginx 的配置文件

### server 段 ###
server {
        listen 监听端口;

        server_name 你的域名;

        # 为了安全考虑(例如 IP 被墙),强烈建议使用 HTTPS
        ssl on;
        ssl_protocols TLSv1.2;
        ssl_certificate ~/站点证书
        ssl_certificate_key ~/站点证书密钥

        location / {
            proxy_pass                          https://www.google.com;

            #把返回的 302 重定向的域名替换成你的。这里关闭
            proxy_redirect                      off;

            #替换指定字符串
            sub_filter                          www.google.com 你的域名;
            #字符串只进行一次替换,即只替换第一个被匹配的字符串。这里关闭。
            sub_filter_once                     off;

            # 指定头部:
            proxy_set_header  Host              "www.google.com";
            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                 www.google.com 你的域名;
            #传固定的 cookie 给谷歌,是为了禁止即时搜索,因为开启即时搜索无法替换内容
            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";

            # 启用 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_500 http_502;
            proxy_cache_lock            on;
            proxy_cache_lock_timeout    5s;
        }
}

指定 Google 上游服务器

这里通过在 http 段(即全局配置 nginx.conf)中,配置 upstream 指定域名解析。
指定多个 Google 服务器的 ip 可能能够减少被判异常流量弹验证码的几率。
对于获取 Google 服务器 ip 的方式,这里介绍两种:
在 vps 上多次执行 ping www.google.com
通过 www.ipip.net/dns.php 选择国外节点进行 dns 查询
在 nginx.conf 中写入配置:

http{

    ......

    # weight 表示权重,数值越高被分配到的几率越大
    #下列 ip 多数为通过 ipip.net 获取
    #你也可以自行添加更多 ip

    upstream www.google.com {

    #中国香港 google.com
    server 216.58.221.68:443 weight=6;
    #中国台湾 google.com
    server 74.125.23.99:443 weight=5;
    #日本东京都东京 google.com
    server 172.217.25.68:443 weight=4;
    #日本东京都东京 google.com
    server 216.58.200.196:443 weight=4;
    #日本大阪府大阪 google.com
    server 216.58.197.4:443 weight=3;
    #新加坡 google.com
    server 74.125.130.147:443 weight=2;
    #美国 我的小鸡 ping www.google.com 所得
    server 216.58.217.196:443 weight=1;
    server 172.217.11.164:443 weight=1;
    #美国 google.com
    server 74.125.28.104:443 weight=1;
    #美国 google.com
    server 74.125.28.147:443 weight=1;
    #美国华盛顿州西雅图 google.com
    server 172.217.3.196:443 weight=1;

    }

}