Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx does not redirect 404 errors

Tags:

nginx

in my website i have only two pages "index.php" and "page.php", whatever i type i get redirected to localhost, for instance if i go to localhost/randompage instead of being redirected to 404.html i go to localhost (i.e index.php)

here is my 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;
    }

}

what should i change?

like image 326
Rachid O Avatar asked Dec 02 '25 04:12

Rachid O


1 Answers

I had a similar problem. Commenting out this line fixed it:

try_files $uri $uri/ /index.php;

try_files checks to see if the given URI works. If not, it tries to see if the given URI works as a directory. Failing that, it redirects to /index.php. Without this line, it'll serve up the 404 file indicated with error_page.

Hope that works for you.

like image 147
Gavin St. Ours Avatar answered Dec 05 '25 15:12

Gavin St. Ours