NGINX 403 Forbidden: Using a Custom Error Page

Updated: January 20, 2024 By: Guest Contributor Post a comment

Introduction

Encountering a ‘403 Forbidden’ error can be a jarring experience for users navigating a website. Instead of presenting the end-user with the default NGINX error message, you have the option to create a custom error page that aligns with your website’s aesthetic and user experience. This tutorial provides an in-depth look at how to setup a custom 403 Forbidden error page in NGINX, including best practices and troubleshooting tips.

What is a 403 Forbidden Error?

A ‘403 Forbidden’ error occurs when a server understands the client’s request but refuses to authorize it. This is often due to permission settings on the requested resource. While permissions issues are the most common cause, the error can also result from other configurations such as faulty .htaccess rules or incorrect file ownership and permissions.

Setting Up a Custom 403 Page in NGINX

To set up a custom 403 page in NGINX, you need to have administrative access to your server’s NGINX configuration files. Ensure you also have a 403 error page designed and ready to be served (e.g., 403.html).

Create a Custom 403 Page

<h1>Custom Error Page</h1>
<p>Oops, you don't have permission to access this.</p>

Update NGINX Configuration

Next, you will need to update your server’s NGINX configuration:

server {
    listen      80;
    server_name yourdomain.com;
    root        /var/www/html;

    error_page  403 /custom_403.html;
    location = /custom_403.html {
        internal;
    }

    location /protected/ {
        deny all;
        return 403;
    }
}

This configuration will display the custom_403.html page whenever a 403 error is triggered. You also see that the ‘deny all;’ directive within a protected directory results in a 403 error.

Testing Your Configuration

After saving your configuration changes, you should restart NGINX to apply them.

sudo service nginx restart

Navigating to a Forbidden Resource

To ensure everything is set up correctly, attempt to access the resource you have restricted. You should now see your customized 403 error page instead of the default NGINX error message.

Common Pitfalls and Troubleshooting

If your custom error page is not showing, consider the following:

  • Did you load the correct file path to the custom error page?
  • Did you forget to restart NGINX after updating the configuration?
  • Is there file permission allowing NGINX to read the error page file?
  • Do the directory permissions of the error page location permit NGINX to access the file?

Conclusion

Customizing error pages in NGINX not only benefits user experience, but can also provide clearer instructions to assist users. While setting a custom 403 Forbidden page is relatively straightforward, attention to detail is key to ensure that visitors encounter a helpful error page, rather than being turned away by a default and stern error message.

Remember to keep your error pages simple, reflective of your brand, and informative to maintain a positive user experience even when a visit doesn’t go as planned.