Nginx 关于使用正则表达式在位置内的try_文件上的$args的问题

Nginx 关于使用正则表达式在位置内的try_文件上的$args的问题,nginx,location,rewrite,args,Nginx,Location,Rewrite,Args,(对不起,我的英语不好) 我有这样一个URL: map $arg_pic $image_dir { # A subdirectory with this name should not exist. default invalid; ~^images/(?P<img_dir>elements|gallery)/.*\.jpg$ $img_dir; } map $arg_pic $image_name { # The ".*" match

(对不起,我的英语不好)

我有这样一个URL:

map $arg_pic $image_dir {
    # A subdirectory with this name should not exist.
    default invalid;

    ~^images/(?P<img_dir>elements|gallery)/.*\.jpg$      $img_dir;
}

map $arg_pic $image_name {
    # The ".*" match here might be insecure - using something like "[-a-z0-9_]+"
    # would probably be better if it matches all your image names;
    # choose a regexp which is appropriate for your situation.
    ~^images/(elements|gallery)/(?P<img_name>.*)\.jpg$   $img_name;
}

map $arg_type $image_type {
    ~^(?P<img_type>[0-9]{1,3}[a-z]{0,4})$    $img_type;
}

location ~ "^/resize.php$" {
    try_files /images/${image_dir}/${image_name}.jpg /imagenes/elements/thumbs/${image_type}_${image_name}.jpg @phpresize;
}

location @phpresize {
    # No changes from your config here.
    try_files $uri =404;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering on;
    proxy_pass http://www.localhost.com:8080;
}

php检查该映像是否存在并提供服务,如果不存在,则使用type参数中指定的大小在磁盘上创建映像并返回它

我想要的是使用nginx检查磁盘上是否存在该大小的映像,因此在需要创建映像时只运行resize.php

我试过这样做,但我认为location指令没有使用regex对查询参数($args)进行操作,那么lon阳离子与示例URL不匹配:(

需要帮忙吗

我需要重写参数($args)并在try_files指令中使用它们……这可能吗

location ~ "^/resize\.php\?pic=images/(elements|gallery)/(.*)\.jpg&type=([0-9]{1,3}[a-z]{0,4})$)" { 
  try_files /images/$1/$2.jpg /imagenes/elements/thumbs/$3_$2.jpg @phpresize;
}

location @phpresize {
  try_files $uri =404;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_buffering on;
  proxy_pass http://www.localhost.com:8080;
}

您无法匹配
位置中的查询字符串(例如,请参阅和)。根据查询字符串内容不同处理请求的唯一方法是使用
if
和条件重写

但是,如果可以使用
@phpresize
位置配置处理对
/resize.php
的请求,但这些请求没有预期的查询参数,则可以尝试以下操作:

map $arg_pic $image_dir {
    # A subdirectory with this name should not exist.
    default invalid;

    ~^images/(?P<img_dir>elements|gallery)/.*\.jpg$      $img_dir;
}

map $arg_pic $image_name {
    # The ".*" match here might be insecure - using something like "[-a-z0-9_]+"
    # would probably be better if it matches all your image names;
    # choose a regexp which is appropriate for your situation.
    ~^images/(elements|gallery)/(?P<img_name>.*)\.jpg$   $img_name;
}

map $arg_type $image_type {
    ~^(?P<img_type>[0-9]{1,3}[a-z]{0,4})$    $img_type;
}

location ~ "^/resize.php$" {
    try_files /images/${image_dir}/${image_name}.jpg /imagenes/elements/thumbs/${image_type}_${image_name}.jpg @phpresize;
}

location @phpresize {
    # No changes from your config here.
    try_files $uri =404;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering on;
    proxy_pass http://www.localhost.com:8080;
}
map$arg\u pic$image\u dir{
#具有此名称的子目录不应存在。
违约无效;
~^images/(?pelments | gallery)/.*\.jpg$$img|u dir;
}
映射$arg\u图片$image\u名称{
#此处的“*”匹配可能不安全-使用类似于“[-a-z0-9_zation]”的内容
#如果它与您所有的图像名称匹配,可能会更好;
#选择适合您的情况的regexp。
~^images/(elements | gallery)/(P.*)\.jpg$$img\u name;
}
映射$arg\u类型$image\u类型{
~(P[0-9]{1,3}[a-z]{0,4})$$img_型;
}
位置~“^/resize.php$”{
try_files/images/${image_dir}/${image_name}.jpg/imagenes/elements/thumbs/${image_type}{image_name}.jpg@phpresize;
}
位置@phpresize{
#此处的配置没有更改。
try_files$uri=404;
代理设置头主机$Host;
代理集头X-Real-IP$remote\u addr;
proxy\u set\u header X-Forwarded-For$proxy\u add\u X\u Forwarded\u For;
代理缓存打开;
代理通行证http://www.localhost.com:8080;
}