|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
proxy_cache_path /data/nginx/cache/one levels=1:2 keys_zone=one:10m max_size=10g;proxy_cache_key "$host$request_uri";server {listen 80;server_name www.fedora.hk fedora.hk;rewrite ^(.*) https://www.fedora.hk$1 permanent;} upstream google { server 173.194.127.144:80 max_fails=3; server 173.194.127.147:80 max_fails=3; server 173.194.127.148:80 max_fails=3; server 173.194.127.145:80 max_fails=3; server 173.194.127.146:80 max_fails=3; }server { listen 443; server_name www.fedora.hk fedora.hk; ssl on; ssl_certificate /usr/local/nginx/conf/fedora.crt; ssl_certificate_key /usr/local/nginx/conf/fedora.key;location / { proxy_cache one; proxy_cache_valid 200 302 1h; proxy_cache_valid 404 1m; proxy_redirect https://www.google.com/ /; proxy_cookie_domain google.com fedora.hk; proxy_pass http://google; proxy_set_header Host "www.google.com"; proxy_set_header Accept-Encoding ""; proxy_set_header User-Agent $http_user_agent; proxy_set_header Accept-Language "zh-CN"; 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"; sub_filter www.google.com www.fedora.hk; sub_filter_once off;}} |
解释下吧:
- 1、这里监听了80和443端口,用了ssl加密,高大上。ssl证书是免费的,startssl,自己去申请个放上就可以了。
- 2、定义了个upstream google,放了5个谷歌的ip,你也可以多放几个达到负载均衡的效果,不过在这的作用是把请求分发到不同的服务器上,避免同一个IP请求过多出现验证码。
- 3、设置了反向代理缓存,某些资源不用重复去请求谷歌获取,加快搜索速度。
- 4、proxy_redirect https://www.google.com/ /; 这行的作用是把谷歌服务器返回的302响应头里的域名替换成我们的,不然浏览器还是会直接请求www.google.com,那样反向代理就失效了。
- 5、proxy_cookie_domain google.com fedora.hk; 把cookie的作用域替换成我们的域名。
- 6、proxy_pass http://google; 反向代理到upstream google,会随机把请求分配到那几个ip。忘记说了,那几个ip可以在自己的vps或服务器上使用nslookup www.google.com获取。
- 7、proxy_set_header Accept-Encoding “”; 防止谷歌返回压缩的内容,因为压缩的内容我们无法作域名替换。
- 8、proxy_set_header Accept-Language “zh-CN”;设置语言为中文
- 9、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”; 这行很关键,传固定的cookie给谷歌,是为了禁止即时搜索,因为开启即时搜索无法替换内容。还有设置为新窗口打开网站,这个符合我们打开链接的习惯。
- 10、sub_filter www.google.com www.fedora.hk;当然是把谷歌的域名替换成我们的了,注意需要安装nginx的sub_filter模块