nginx不会重定向404错误

时间:2013-11-03 23:46:12

标签: nginx

在我的网站上我只有两个页面“index.php”和“page.php”,无论我输入什么,我都会被重定向到localhost,例如,如果我去localhost / randompage而不是被重定向到404.html我转到localhost(即index.php)

这是我的conf:

server {
    listen       80;
    server_name  localhost;
root   html;
index  index.php;

    location / {

        if ($request_uri !~* '\/|page\.php\?.*') {
            return 404;
        }

        try_files $uri $uri/ /index.php;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   html;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    location ~ \.php$ {
    try_files  $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

我应该改变什么?

1 个答案:

答案 0 :(得分:5)

我有类似的问题。评论这一行修正了它:

try_files $uri $uri/ /index.php;

try_files检查给定的URI是否有效。如果没有,它会尝试查看给定的URI是否作为目录。如果做不到,它会重定向到/index.php。如果没有这一行,它将提供用error_page指示的404文件。

希望对你有用。

相关问题