我有以下托管图像服务的 nginx 配置:
upstream thumbor {
server localhost:8888;
}
server {
listen 80;
server_name my.imageserver.com;
client_max_body_size 10M;
rewrite_log on;
location ~ /images {
if ($arg_width="10"){
rewrite ^/images(/.*)$ /unsafe/$1 last;
}
rewrite ^/images(/.*)$ /unsafe/$1 last;
}
location ~ /unsafe {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://thumbor;
proxy_redirect off;
}
location = /favicon.ico {
return 204;
access_log off;
log_not_found off;
}
}
我正在尝试重写以下 URL:
从
my.imageserver.com/images/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg
到
my.imageserver.com/unsafe/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg
这很简单,问题开始于当我想要允许查询字符串转到 url 的路径时,如下所示:
从
my.imageserver.com/images/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg?width=150&height=200&mode=smart
到
/my.imageserver.com/unsafe/150x200/smart/Jenesis/EmbeddedImage/图像/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg
如果查询字符串的顺序不重要的话,效果会更好。
我尝试使用:$arg_width,但似乎不起作用。
在 ubuntu 上使用 nginx 1.6.1。
非常感谢您的帮助。
答案1
你可以这样做:
location ~ /images {
rewrite '^/images/(.*)$' '/unsafe/${arg_width}x${arg_height}/${arg_mode}/$1?' last;
}
Nginx 重写日志:
"^/images/(.*)$" matches "/images/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg", request: "GET /images/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg?width=150&height=200&mode=smart HTTP/1.1"
rewritten data: "/unsafe/150x200/smart/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg", args: "", request: "GET /images/Jenesis/EmbeddedImage/image/jpeg/jpeg/9f5d124d-068d-43a4-92c0-1c044584c54a.jpeg?width=150&height=200&mode=smart HTTP/1.1"
但最好反其道而行之,因为您可以使用更简洁的方式在 nginx 配置的一行中验证参数。