2013-05-08 140 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; 
}