Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple 404 error pages in nginx

Tags:

nginx

I am running nginx server. I want to serve a custom error page for a particular request only. For-example for request

http://localhost/abc1 & http://localhost/abc2

if these pages are not there I want to serve a custom error page. This custom error page should appear only for above two mentioned links, rest of the page errors can show the default error page. I have tried different configuration but nothing seems to work. Thoughts

Regards, Farrukh Arshad.

like image 399
Farrukh Arshad Avatar asked Oct 29 '13 19:10

Farrukh Arshad


People also ask

How does nginx handle 404 error?

The error usually is displayed to a user via a simple default HTML page. Fortunately, you can configure NGINX to display custom error pages to your site's or web application's users. This can be achieved using the NGINX's error_page directive which is used to define the URI that will be shown for a specified error.

How do I create a 404 page in nginx?

Put your custom error pages in the /usr/share/nginx/html directory where Nginx sets its default document root. You'll make a page for 404 errors called custom_404. html and one for general 500-level errors called custom_50x.

What is 404 not found nginx?

Essentially, the “404 error” indicates that your or your visitor's web browser was connected successfully to the website server or the host. However, it was unable to locate the requested resource, such as filename or any specific URL.


2 Answers

Your answer is quite correct, but as you said, you only defined them for the html's, remove the extension and it should work for the whole directory, no need to repeat the root, just define it in the server block scope

server {
    listen 80;
    root   /var/www/nginx-default;

    location /abc1 {        
        error_page 404 /special_error.html;
    }

    location /abc2 {
        error_page 404 /special_error2.html;
    }
}
like image 199
Mohammad AbuShady Avatar answered Sep 21 '22 21:09

Mohammad AbuShady


Ok, I found the answer. The trick is you have to define error_page explicitly for all those special locations. Here is the configuration which worked for me.

location / {
    root   /var/www/nginx-default;
    index  index.html index.htm;
    error_page 404 /404.html;
}

location /abc1.html {
    root   /var/www/nginx-default;
    error_page 404 /special_error.html;
}
location /abc2.html {
    root   /var/www/nginx-default;
    error_page 404 /special_error2.html;
}

I am not good with nginx, but I have noticed it depends on the search pattern you give in "location" tag. I have tried different things and those failed . Forexample the above rules will ONLY work for

http://localhost/abc1.html 

and fail for

http://localhost/abc1 

so your "location" search pattern should be good if you want to cover second case. Probably some nginx guru can shed some more light on this. Thanks.

like image 37
Farrukh Arshad Avatar answered Sep 23 '22 21:09

Farrukh Arshad