如何检查Nginx中是否存在文件?否则运行重写规则 [英] How can I check if file exists in nginx? otherwise run a rewrite rule

查看:105
本文介绍了如何检查Nginx中是否存在文件?否则运行重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

为了即时调整图像大小,我决定使用Kraken或imgix之类的服务.

In order to resize images on the fly, i have decided to use a service like Kraken or imgix.

我的图像将显示为:site.com/img/path-to-img-s250x250.jpg 我想实现的是:如果图像path-to-img-s250x250.jpg存在,那么我将显示它,如果不存在,则运行nginx重写规则以调整原始图像的大小并保存它.

My images will be displayed as so: site.com/img/path-to-img-s250x250.jpg and what i would like to achieve is: if the image path-to-img-s250x250.jpg exists, then i'll display it, and if not then run an nginx rewrite rule to resize the original image and save it.

这可能吗?还是应该使用PHP进行检查?

Is this something possible or should i do the checks with PHP?

关于如何更好地动态调整图像大小,您还有更好的主意吗?

Also do you have any better ideas on how to better deal with resizing images on the fly?

非常感谢

location /img/ {
    try_files $uri @getImg @resize;
}

location @getImg{
    rewrite "img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+).([0-9]{5}).(jpg|jpeg|png|gif|ico)$" /img/$1/$2-$3x$4.$6 break;
}

location @resize{
    rewrite "img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+).([0-9]{5}).(jpg|jpeg|png|gif|ico)$" $scheme://$host/image?path=$1&file=$2&ext=$6&w=$3&h=$4 break;
}

这是图像链接:site.com/img/prods/1002/filename-200x200.12345.jpg

12345用于版本控制

12345 is for the versioning

推荐答案

您可以使用try_files测试一个或多个文件的存在.有关详细信息,请参见此文档.

You can use try_files to test for the existence of one or more files. See this document for details.

使用正则表达式 location捕获URI的元素.正则表达式的位置是按顺序评估的,因此配置文件中的位置可能很重要.有关详细信息,请参见本文档.

Use a regular expression location to capture the elements of the URI. Regular expression locations are evaluated in order, so there position within the configuration file may be significant. See this document for details.

如果try_files语句的最后一个参数是URI.它将生成一个内部重定向.

If the final parameter of a try_files statement is a URI. it will generate an internal redirect.

例如:

location ~ "^/img/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+)\.([0-9]{5})\.(jpg|jpeg|png|gif|ico)$" {
    try_files $uri /img/$1/$2-$3x$4.$6 /image?path=$1&file=$2&ext=$6&w=$3&h=$4;
}

由于许多PHP脚本使用最初请求的URI(REQUEST_URI)来确定路由,因此上述方法不能单独起作用.

The above will not work on its own, as many PHP scripts use the originally requested URI (REQUEST_URI) to determine the route.

您可以创建一个自定义位置来显式设置REQUEST_URISCRIPT_FILENAME参数.

You can create a custom location to explicitly set the REQUEST_URI and SCRIPT_FILENAME parameters.

例如:

location = /image {
    include        fastcgi_param;
    fastcgi_pass   ...;
    fastcgi_param  SCRIPT_FILENAME  $document_root/index.php;
    fastcgi_param  REQUEST_URI      $uri$is_args$args;
}

基于您现有的代码(以上只是一个猜测)而建立此块.我们首先包含全局配置文件,然后覆盖SCRIPT_FILENAMEREQUEST_URI.我以为index.php正在处理/image路由.

Base this block on your existing code (the above is just a guess). We include the global configuration files first, then override SCRIPT_FILENAME and REQUEST_URI. I have assumed that index.php is handling the /image route.

这篇关于如何检查Nginx中是否存在文件?否则运行重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆