Nginx服务器没有正确重定向

时间:2014-07-04 19:25:11

标签: php linux nginx

我在nginx中遇到404错误,当我应该重定向时。 我来自Apache,这是我第一次玩Nginx。 我想它必须是一个我忽视的小错误。

这是代码:

server {
    listen   80 default_server;
    server_name  localhost;
    root   /var/www/resizing/public_html;
    index index.php index.html index.htm;

    location ~ ^/(.*)/cache/(.*)$
    {
            try_files $uri @resize;
            expires 4h;
    }

    location / {
            expires 4h;
    }

    location ~ \.php(.*)$  {
           #fastcgi_pass 127.0.0.1:8000;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location @resize {
            rewrite ^/(.*)/cache/(.*)$ /resize.php?dir=$1&path=$2;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }



location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
}

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
#   proxy_pass http://127.0.0.1:8080;    
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
#   root /usr/share/nginx/html;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
#   try_files $uri =404;
#   fastcgi_pass unix:/var/run/php5-fpm.sock;
#   fastcgi_index index.php;
#   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#   include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}

}

例如,如果我键入:localhost / cache / 150x200-0 / originals / 1.jpg我得到404错误。

1 个答案:

答案 0 :(得分:1)

问题是URI为/cache/150x200-0/originals/1.jpg,并且与配置中的location ~ ^/(.*)/cache/(.*)$不匹配。

正确的位置看起来像(假设实际需要“dir”组件):

location ~ ^(/.*)?/cache/(.*)$ {
    ...
}

请注意,rewrite中使用的location @resize存在同样的问题。