nginx泛域名反向代理教程

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

Nginx的配置其实很简单

  1. server {
  2. resolver 8.8.8.8;
  3. listen 80;
  4.  
  5. server_name *.google.db.ci;
  6. if ($host ~* "(.*)\.google\.db\.ci") {
  7. set $subdomain $1;
  8. }
  9. location / {
  10. proxy_set_header Referer https://$subdomain.googleusercontent.com;
  11. proxy_pass "https://$subdomain.googleusercontent.com";
  12. }
  13. }

第二种配置:

  1. server {
  2. listen 80;
  3. server_name ~^(.*)\.xxx\.xxxxx\.com$;
  4. location / {
  5. resolver 8.8.8.8;
  6. proxy_pass https://$1.googlevideo.com;
  7. proxy_redirect off;
  8. proxy_set_header Accept-Encoding "";
  9. proxy_set_header User-Agent $http_user_agent;
  10. 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";
  11. }
  12. }
  13. server {
  14. listen 443;
  15. server_name ~^(.*)\.xx\.xxxx\.xxx$;
  16. ssl on;
  17. ssl_certificate /usr/local/nginx/conf/vhost/server/xx.crt;
  18. ssl_certificate_key /usr/local/nginx/conf/vhost/server/xx.key;
  19.  
  20. location / {
  21. resolver 8.8.8.8;
  22. proxy_pass https://$1.googlevideo.com;
  23. proxy_redirect off;
  24. proxy_set_header Accept-Encoding "";
  25. proxy_set_header User-Agent $http_user_agent;
  26. 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";
  27. }
  28. }

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

点赞