2013-05-08 143 views
0

ARGS(对不起我的英文不好)

我有这样一个URL:

http://www.domain.com/resize.php?pic=images/elements/imagename.jpg&type=300crop

如果图像存在,并且成为在PHP检查,如果没有,则使用type参数中指定的大小在磁盘上创建映像并将其返回。

我想要的是检查图像是否以nginx的大小存在于磁盘上,因此只有在需要创建图像时才运行resize.php。

我想这一点,但我认为该位置指令不会对使用正则表达式查询参数($参数)进行操作,然后loncation不匹配样品网址:(

任何帮助吗?

我需要重写的参数($参数),并在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; 
} 

回答

1

location你所无法比拟的查询字符串(例如,见herehere)。根据查询字符串内容的不同处理请求的唯一方法是使用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; 
}