【发布时间】:2019-06-28 09:36:12
【问题描述】:
我喜欢用两个可选参数匹配位置,所以它匹配
/images/my.jpg
/images/my.jpg/200
/images/my.jpg/200/
/images/my.jpg/200/400
/images/my.jpg/200/400/
我正在使用三个匹配组,以便可以在 $image、$width 和 $height 变量中获取匹配的部分。
我试过这个查询。它仅在给出完整位置时才有效,但如果缺少其中一个参数则不匹配
~ ^/images/(?<img>.+?)/(?<width>\d+)/(?<height>\d+)?$
我也试过了,完全不行
~ ^/images/(?<img>.+?)?(/?<width>\d+)?(/?<height>\d+)?$
请帮忙 - 我这里有输入数据。 https://regex101.com/r/qU7tI7/6
这里是nginx配置
server {
listen 80;
server_name f.myhost.com;
set $sitename f.myhost.com;
root $root_folder/myhost.com/files;
set $path_info "";
location / {
root /var/www/myhost.com/app/api/;
include conf.d/site.template;
add_header X-test "root";
}
location /files {
root /var/www/myhost.com/app/;
include conf.d/site.template;
}
location ~ ^/images/(?<img>[^/]+)(/(?<width>\d*))?(/(?<height>\d*))?$ {
# root /var/www/myhost.com/files/;
alias /var/www/myhost.com/app/test/$image;
# include conf.d/site.template;
add_header X-test "test";
add_header X-width "$width";
add_header X-height "$height";
add_header X-name "$image";
image_filter resize $width -;
image_filter_jpeg_quality 75;
image_filter_buffer 8M;
}
}
【问题讨论】:
-
试试:
^/images/(?<img>[^/]+)(/(?<width>\d*))?(/(?<height>\d*))?$ -
感谢 Richard,它匹配除了 regex101 的最后一个情况之外的所有情况,但又不匹配 nginx regex101.com/r/qU7tI7/8
-
我错过了最后一个案例,它需要在
$之前再添加一个/?。你是什么意思“不在 Nginx 中”?您的位置是否有冲突? -
用我的配置更新帖子
-
您的配置中有一个错字 - 您在某些地方有
img,在其他地方有image。
标签: nginx nginx-location