Nginx总是得到404

时间:2014-10-27 14:24:44

标签: regex nginx location rewrite

我有两个文件夹:/ var / www / imagestest和/ var / www / imagestest2。在文件夹2中,文件可以按原样存储或由num分割。例如,请求uri /images/0/1.jpg文件可以存储在/var/www/imagestest2/images/0/0/0/0/1.jpg中。正则表达式是正常的,但我不能将nginx配置为在某个时间使用所有文件夹,并且总是为重写的uri获得404。

文件结构:

/var/www/imagestest2/images/2.jpg
/var/www/imagestest2/images/0/0/0/0/1.jpg

请求:

原样:

curl -I imagestest.local/images/2.jpg
HTTP/1.1 200 OK

预计从/var/www/imagestest2/0/0/0/0/1.jpg获取文件:

curl -I imagestest.local/images/0/1.jpg #
HTTP/1.1 404 Not Found

与拆分目录一样:

curl -I imagestest.local/images/0/0/0/0/1.jpg
HTTP/1.1 404 Not Found

Nginx配置:

server {
    listen *:80;
    server_name imagestest.local;
    root /var/www/imagestest/;

    set $other_dir "/var/www/imagestest2";

    location ~ / {
        try_files $uri @other_dir =404;
        error_page 404 = @rewrite;
    }

    #try files as is
    location @other_dir {
        root $other_dir;
    }

    #try for splitted
    location @rewrite {
        root $other_dir;
        rewrite "^/(.*)\/([0-9]{1,3})\/(.*)" "/$1/0/0/0/$2/$3" last; #1-3
    }
}

但是,如果我将设置site_dir并在服务器块中重写 - 我将获得200 OK分割,但无法从文件夹/ var / www / imagestest /中获取文件:

curl -I imagestest.local/images/0/1.jpg
HTTP/1.1 200 OK

server {
    listen *:80;
    server_name imagestest.local;
    root /var/www/imagestest2/;

    set $other_dir "/var/www/imagestest2";

    rewrite "^/(.*)\/([0-9]{1,3})\/(.*)" "/$1/0/0/0/$2/$3" last; #1-3
.....

0 个答案:

没有答案