简体   繁体   中英

Haproxy/Nginx partial URL based hash upstream

We know that HAProxy and Nginx can do the URL hash based upstream but how can we hash the part of the URL.

We have 4 back-end original image servers, each will store all the original large size image files. The image server will resize the file based on user request on the fly. (Tomcat Java load the file into memory and resize then response)

The original file is:

http://imageserver.company.com/path/to/imageA.jpg

The end-user will request:

httpurl://imageserver.company.com/path/to/imageA.jpg/crop/400x300.jpg
httpurl://imageserver.company.com/path/to/imageA.jpg/400x224.jpg
httpurl://imageserver.company.com/path/to/imageA.jpg/1280x720.jpg

I would like HAProxy and Nginx will do the hash on "/path/to/imageA.jpg";

Hash (substring (url, 0, find (url, ".jpg/")

Any idea of how to config?

In nginx you can use the map and upstream::hash directives:

 map $uri $image_hash {
    default $uri;
    "~(?<image_path>.+(?:jpg|png))/" $image_path;
}

upstream image_backends {
    hash $image_hash;
    server server1;
    server server2;
    server server3;
    server server4;
}

server {
   ...

   location / {
      # add debug header to view the hash
      add_header ImageHash $image_hash;

      proxy_pass http://image_backends;
   }

}

I'm not sure what the exact syntax would be for HAProxy, but it's uri hash supports specifying the " depth " of the URI hash. So if the original path to the URL has a fixed depth then you can use that (though I'm guessing that's not the case)?

  The "depth" parameter indicates the maximum directory depth to be used to compute the hash. One level is counted for each slash in the request. If both parameters are specified, the evaluation stops when either is reached. 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM