一场关于 thumbor 涉及到的Nginx 转发需求

需求:

1> 在一个图片 url 后面添加:  ?resize=axb (a b 任意数字) 返回缩略图尺寸,url 保持不变
2> 在一个图片 url 后面添加: ?stype=xxlarge  返回以下产品规定的 xxlarge 尺寸大小
3> 在一个图片 url 中  类型_图片.jpg  返回产品规定的尺寸大小

1张图片:
http://thumbor-myimage.testrenren.tagtic.cn/donewsfiles/20200708/1555/0e0f495c-918e-4356-aade-cf9e7efbedfc.jpeg  
通过缩略图:
尺寸规则如下:
1.
xxlarge=1600x10240
xlarge=1024x10240
large=720x10240
main=200x600
head=100x300
tiny=50x50
origin=原图尺寸

2. resize=axb 尺寸

如:http://thumbor-myimage.testrenren.tagtic.cn/donewsfiles/20200708/1555/0e0f495c-918e-4356-aade-cf9e7efbedfc.jpeg?resize=100x300  返回 100x300 的缩略图尺寸

http://thumbor-myimage.testrenren.tagtic.cn/donewsfiles/20200708/1555/0e0f495c-918e-4356-aade-cf9e7efbedfc.jpeg?stype=main  返回 main 规定的缩略图尺寸

http://thumbor-myimage.testrenren.tagtic.cn/donewsfiles/20200708/1555/main_0e0f495c-918e-4356-aade-cf9e7efbedfc.jpeg  返回 main 规定的缩略图尺寸

  

thumbor 服务已经部署完毕,可以正常使用;
部署 Nginx;

因图片需要上cdn ,因此nginx 配置中不能涉及到 rewrite 规则,只能用  proxy_pass ;以下为 nginx 转发规则:

upstream mybaidu {
  server 10.16.0.237:31009;  # 缩略图服务

}
server {
      server_name  thumbor-myimage.testrenren.tagtic.cn;  #内网测试域名
      sendfile off;

      location / {
          root  /usr/local/nginx/html/;
          ssi on;
          ssi_silent_errors on;
          ssi_types text/shtml;
          index index.html;
          proxy_hide_header Content-Type;
          resolver 10.4.1.14;
}

# 第一个Location 内容主要匹配第三个需求,即 _图片.jpg, $1 $2 $3 $4 分别为  Location 当中括号里面内容,为图片的具体路径;
          location ~  ^/(\w+)/(\d+)/(\d+)/main_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/200x600/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def;      

           }
          location ~  ^/(\w+)/(\d+)/(\d+)/xxlarge_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/1600x10240/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def; 
           }
          location ~  ^/(\w+)/(\d+)/(\d+)/xlarge_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/1024x10240/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def; 
           }
          location ~  ^/(\w+)/(\d+)/(\d+)/large_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/720x10240/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def; 
           }
          location ~  ^/(\w+)/(\d+)/(\d+)/head_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/100x300/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def; 
           }
          location ~  ^/(\w+)/(\d+)/(\d+)/tiny_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/50x50/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def; 
           }
          location ~  ^/(\w+)/(\d+)/(\d+)/origin_(.+\.(jpeg|jpg|gif|png))   { 
               set $def   /unsafe/0x0/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
               proxy_pass http://mybaidu$def; 
           }

# 第二个Location 内容主要匹配 第一个、第二个 规则,默认返回原图, 根据if 判断进行变量赋值;

          location ~  ^/(\w+)/(\d+)/(\d+)/(.+\.(jpeg|jpg|gif|png))   { 
              
             set $abc   /unsafe/0x0/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
          
             if ($arg_resize != "") {
               set $abc   /unsafe/$arg_resize/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }
            
             if ($arg_stype = "xxlarge") {
               set $abc   /unsafe/1600x10240/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }
             
             if ($arg_stype = "main") {
               set $abc   /unsafe/200x600/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }

             if ($arg_stype = "xlarge") {
               set $abc   /unsafe/1024x10240/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }

             if ($arg_stype = "large") {
               set $abc   /unsafe/720x10240/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }

             if ($arg_stype = "head") {
               set $abc   /unsafe/100x300/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }

             if ($arg_stype = "tiny") {
               set $abc   /unsafe/50x50/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }

             if ($arg_stype = "origin") {
               set $abc   /unsafe/0x0/thumbor-image.testrenren.tagtic.cn/$1/$2/$3/$4;
              }

       
            
             #add_header aa $abc;
             proxy_pass http://mybaidu$abc;

          }

  

posted @ 2020-07-13 15:18  lixinliang  阅读(692)  评论(0编辑  收藏  举报