T
T
Tarakkolya2014-10-21 16:54:55
Nginx
Tarakkolya, 2014-10-21 16:54:55

How to correctly register configs for 404 errors in nginx?

Hello, the problem is this: when you call a non-existent page, for example , for mysite.ru/wrongpage
some reason, the main page opens.
configs are:

server{
    listen 80;
    server_name www.mysite.ru;
    rewrite ^ http://mysite.ru$request_uri? permanent; #301 redirect
}
server {
    listen 80;
    server_name mysite.ru;
    root /home/user/mysite;

    location / {
        index index.htm index.html index.php;
        try_files $uri /index.html;
        error_page 404             /404.html;
        error_page 500 502 503 504 /50x.html;
    }

    location = /index.html {
       if ($request_uri = /index.html) {
           rewrite ^ http://mysite.ru? permanent;#301 redirect
       }
    }

    error_log /var/log/nginx/mysite.error.log;
    access_log /var/log/nginx/mysite.access.log;
}

Please tell me how to fix.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Glueon, 2014-10-21
@Tarakkolya

What is the purpose of your /index.html in try_Files?
Tries to find $uri and if it doesn't find it redirects to /index.html.
This is the try_Files syntax:

try_files file ...uri;

The last argument is the URL or named location in case none of the previous paths were found.
In your case, it should be then:
As far as I understand, starting with some version of nginx, it supports the following syntax:
Then, if none of the arguments was found, a 404 HTTP code is generated and then processing goes through the corresponding error_page.

Z
Zaur Ashurbekov, 2014-10-21
@zaurius

error_page 400 404 /404.html;
location = /404.html {
root /home/address
}
well, that's how it is for me.
outside of this:
location / {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question