Haproxy/Nginx 部分 URL 基于哈希上游
Haproxy/Nginx partial URL based hash upstream
我们知道 HAProxy 和 Nginx 可以做基于上游的 URL 散列,但我们如何散列 URL.
的部分
我们有4台后端原图服务器,每台都会存储所有的大尺寸原图文件。图像服务器将根据用户请求动态调整文件大小。 (Tomcat Java 将文件加载到内存中并调整大小然后响应)
原文件为:
http://imageserver.company.com/path/to/imageA.jpg
最终用户将要求:
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
我希望 HAProxy 和 Nginx 对“/path/to/imageA.jpg”进行哈希处理;
Hash (substring (url, 0, find (url, ".jpg/")
知道如何配置吗?
在 nginx 中,您可以使用 map and upstream::hash 指令:
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;
}
}
我不确定 HAProxy 的确切语法是什么,但它的 uri 哈希支持指定 URI 哈希的“depth”。因此,如果 URL 的原始路径具有固定深度,那么您可以使用它(尽管我猜不是这种情况)?
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.
我们知道 HAProxy 和 Nginx 可以做基于上游的 URL 散列,但我们如何散列 URL.
的部分我们有4台后端原图服务器,每台都会存储所有的大尺寸原图文件。图像服务器将根据用户请求动态调整文件大小。 (Tomcat Java 将文件加载到内存中并调整大小然后响应)
原文件为:
http://imageserver.company.com/path/to/imageA.jpg
最终用户将要求:
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
我希望 HAProxy 和 Nginx 对“/path/to/imageA.jpg”进行哈希处理;
Hash (substring (url, 0, find (url, ".jpg/")
知道如何配置吗?
在 nginx 中,您可以使用 map and upstream::hash 指令:
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;
}
}
我不确定 HAProxy 的确切语法是什么,但它的 uri 哈希支持指定 URI 哈希的“depth”。因此,如果 URL 的原始路径具有固定深度,那么您可以使用它(尽管我猜不是这种情况)?
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.