首页 > nginx相关 > nginx配置文件中的rewrite参数中-f -d -e的使用误解

nginx配置文件中的rewrite参数中-f -d -e的使用误解

2009年3月8日 发表评论 阅读评论

今天在写一个rewrite的时候总是不对,当中用到了文件不存在的判断,如下:

1
2
3
4
if (!-f $request_filename){
     rewrite ^(.*)$ http://site.com$1 permanent;
     break;
}

重温这几个参数的含义
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录

安装我原来的理解是如果需要判断请求的目录是否存在就用d,比如/dira/
需要判断请求的目录是否存在就用f,比如/dira/filea
如果文件或者目录之一有不存在的话就用e

原来我以为/dira/filea 假如dira和filea都不存在的话随便用那个参数都能匹配
今天经过实际使用发现我完全搞错了,如果真正请求遇到上面这个目录和文件都不存在的话 !-f 和 !-d 是没法匹配的,只能用 !-e 。
正确的解释是
-e和!-e用来判断是否存在文件目录





分类: nginx相关 标签: ,
  1. ik
    2009年3月28日17:08 | #1

    if (-d $request_filename) {
    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
    }

    location /{
    rewrite “^/date/([0-9]{6})/([0-9]+)/$” index.php?action=index&setdate=$1&page=$2 last;
    rewrite “^/page/([0-9]+)/$” index.php?page=$1 last;
    rewrite “^/category/([0-9]+)/([0-9]+)/$” index.php?cid=$1&page=$2 last;
    rewrite “^/category/(.+)/([0-9]+)/$” index.php?curl=$1&page=$2 last;
    rewrite “^/(archives|search|reg|login|index|links|aboutus)/$” index.php?action=$1 last;
    rewrite “^/(comments|tagslist|index)/([0-9]+)/$” index.php?action=$1&page=$2 last;
    rewrite “^/tag/(.+)/?([0-9]+)/$” index.php?action=tags&item=$1&page=$2 last;
    rewrite “^/ais/([0-9]+)/([0-9]+)/$” index.php?action=show&id=$1&page=$2 last;
    rewrite “^(.+)/([0-9]+)/$” index.php?action=show&alias=$1&page=$2 last;
    rewrite ^/user/(.+)/$ index.php?action=finduser&user=$1 last;
    rewrite ^/uid/(.+)/$ index.php?action=finduser&uid=$1 last;
    }

    帮我分析下我这个rewrite看看,我这整了半天还是不行。

  2. 2009年3月28日20:17 | #2

    @ik
    你是要实现什么目的呢

  1. 本文目前尚无任何 trackbacks 和 pingbacks.