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.
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.
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.
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.
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With