nginx泛域名反向代理教程

通过Nginx进行泛域名反代
感觉有点拗口,实际情况是这样,我们最近抓取了大量的Google Play数据,其中图片都是存在googleusercontent.com 的子域下,国内无法直接访问,所以只能在香港的VPS上做一个简单的反代,然后再在国内CDN做一个双层的反代。

Nginx的配置其实很简单

server {
        resolver 8.8.8.8;
        listen 80;

        server_name *.google.db.ci;
        if ($host ~* "(.*)\.google\.db\.ci") {
            set $subdomain $1;
        }
        location / {
            proxy_set_header Referer  https://$subdomain.googleusercontent.com;
            proxy_pass  "https://$subdomain.googleusercontent.com";
        }
}

第二种配置:

server { 
listen 80; 
server_name ~^(.*)\.xxx\.xxxxx\.com$; 
location / { 
resolver 8.8.8.8; 
proxy_pass https://$1.googlevideo.com; 
proxy_redirect off; 
proxy_set_header Accept-Encoding ""; 
proxy_set_header User-Agent $http_user_agent; 
proxy_set_header Cookie "PREF=ID=041208f19f6de346:U=0f62f33dd8549d11:FF=2:LD=zh-CN:NW=1:TM=1325338577:LM=1332142444:GM=1:SG=2:S=rE0SyJh2W1IQ-Maw"; 
} 
} 
server { 
listen 443; 
server_name ~^(.*)\.xx\.xxxx\.xxx$; 
ssl on; 
ssl_certificate /usr/local/nginx/conf/vhost/server/xx.crt; 
ssl_certificate_key /usr/local/nginx/conf/vhost/server/xx.key; 

location / { 
resolver 8.8.8.8; 
proxy_pass https://$1.googlevideo.com; 
proxy_redirect off; 
proxy_set_header Accept-Encoding ""; 
proxy_set_header User-Agent $http_user_agent; 
proxy_set_header Cookie "PREF=ID=041208f19f6de346:U=0f62f33dd8549d11:FF=2:LD=zh-CN:NW=1:TM=1325338577:LM=1332142444:GM=1:SG=2:S=rE0SyJh2W1IQ-Maw"; 
} 
}

暂时先不管头或者缓存,反正这样能用了。

点赞