简体   繁体   English

如果文件存在,则nginx重写

[英]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. 但是下面的块不起作用,nginx只返回原始图像。

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? 我应该如何使用nginx设置执行此操作? Thanks. 谢谢。

The -f operator requires a pathname, use $document_root or $request_filename . -f运算符需要路径名,使用$document_root$request_filename

For example: 例如:

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

Or: 要么:

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

See this document for details. 请参阅此文档了解详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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