简体   繁体   中英

nginx rewrite if file exist

My script modifies image and returns the result.

I want to process the image only if it exists.

However the following block won't work, nginx just returns the original image.

location ~* \.(jpg|jpeg|png)$ {
    if (-f $uri) {
        rewrite (.*) /imagemagic.php?src=$1 last;
    }
}

I initially used this inside the script to check, however I don't like it sitting in the script file...

if (!file_exists($_GET['src'])) {
    http_response_code(404);
    exit();
}

How am I supposed to do this using nginx settings? Thanks.

The -f operator requires a pathname, use $document_root or $request_filename .

For example:

if ( -f $document_root$uri ) { ... }

Or:

if ( -f $request_filename ) { ... }

See this document for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM