When nginx returns a 400 Bad Request, visitors see a bare default error page that gives no context and can make your site look broken. This guide shows you how to replace that with a custom, branded error page—so users get a clear message and a way back to your site. You’ll need access to your nginx configuration file (usually nginx.conf or a site-specific file in sites-available) and the ability to reload nginx.
What You’ll Need
- Root or sudo access to the server running nginx.
- An HTML file for the custom error page (e.g.,
/var/www/html/400.html). - A text editor (like nano or vim) to edit nginx config files.
- Basic familiarity with the command line.
Step-by-Step
- Create your custom 400 error page. Write an HTML file with a helpful message and a link back to your homepage. For example, create
/var/www/html/400.htmlwith content like:<!DOCTYPE html> <html> <head><title>Bad Request</title></head> <body> <h1>400 - Bad Request</h1> <p>The request could not be understood by the server.</p> <p><a href="/">Go to homepage</a></p> </body> </html>
Save the file and ensure it is readable by the nginx user (usually
www-data). - Locate your nginx configuration. Run
nginx -tto check the config file path, or look in/etc/nginx/. The main file isnginx.conf, but you’ll often edit a server block insites-available/(e.g.,/etc/nginx/sites-available/default). - Open the server block. Use a text editor:
sudo nano /etc/nginx/sites-available/default(adjust the path to your setup). - Add the error_page directive. Inside the
server { ... }block, add a line like:error_page 400 /400.html;
This tells nginx to serve
/400.htmlwhen a 400 error occurs. If your custom page is in a different location, use a URI that maps to it (e.g.,error_page 400 /custom_errors/400.html;). - Make sure the location is accessible. If the page is not in the root directory, you may need to add a
locationblock to serve it. For example, if your root is/var/www/htmland the file is/var/www/html/400.html, the URI/400.htmlworks automatically. For a subdirectory, add:location = /400.html { root /var/www/html; internal; }The
internaldirective prevents direct access to the error page (only nginx can serve it). - Test the configuration. Run
sudo nginx -tto check for syntax errors. If it says “syntax is ok� and “test is successful,� proceed. - Reload nginx. Apply the changes with
sudo systemctl reload nginx(orsudo nginx -s reload). No downtime is needed. - Verify it works. Trigger a 400 error by sending a malformed request. For example, using curl:
curl -i -X TRACE http://yourdomain.com/(or any method that nginx rejects). You should see your custom page instead of the default nginx error.
Common Mistakes
- Forgetting the
internaldirective. Without it, users can directly access/400.html, which is fine but not ideal. More importantly, if the page is missing, nginx will return a 404 for the error page, causing a loop. - Using a full URL in
error_page. The directive expects a URI, not a URL. Use/400.html, nothttp://example.com/400.html. - Not testing after editing. A typo in the config can break nginx entirely. Always run
nginx -tbefore reloading. - Placing the directive in the wrong block. If you have multiple server blocks, make sure you edit the one that handles the traffic you’re testing. Use
server_nameto identify it. - File permissions. If nginx cannot read the HTML file, it will return a 403 or 500. Ensure the file is readable by the nginx user (e.g.,
sudo chmod 644 /var/www/html/400.html).
Tips
- Use a single error page for multiple codes. You can specify multiple codes:
error_page 400 404 500 /error.html;but then you need to handle them differently if you want distinct messages. - Include a link back to your homepage. A lost user is more likely to stay if they can navigate back easily.
- Keep the page small. Error pages should load quickly, so avoid large images or scripts.
- Log the error. Use
error_page 400 = @error_400;with a named location if you want to log the error or perform additional processing. But for simple redirects, the basic directive is enough. - Test with different request methods. A 400 can be triggered by malformed headers, invalid HTTP versions, or bad request lines. Use curl with
--http1.0or a raw socket to simulate.
Troubleshooting
If your custom page doesn’t appear, try these steps:
- Check nginx error logs. Look in
/var/log/nginx/error.logfor messages like “directory index of … is forbiddenâ€� or “open() … failedâ€�. This will tell you if the file is missing or unreadable. - Verify the error code. Use
curl -ito see the actual HTTP status. If you get a 404 instead of 400, your request isn’t triggering the error you think. - Make sure the error_page directive is in the correct server block. If you have multiple sites, the wrong block may be handling the request. Use
nginx -Tto dump the full config and check which server block matches your domain. - Clear your browser cache. Your browser might be showing a cached version of the old error page.
- Try a different URI. If you use a subdirectory, ensure the location block is defined correctly. A common mistake is using
aliasinstead ofroot. - If you get a 500 Internal Server Error, it often means the error page file itself is not accessible. Check permissions and the path in the
rootdirective.
Frequently Asked Questions
Can I redirect to an external URL for a 400 error?
Yes, you can use a full URL in the error_page directive, but nginx will issue an HTTP redirect (302) to that URL. This is not recommended because it changes the status code and may confuse users or SEO. Better to serve a local page.
Will this affect SEO?
No, a 400 error is a client error, and serving a custom page doesn’t change the HTTP status code. The response code remains 400, which is correct. Search engines will still see it as a bad request.
Can I use a PHP file as the error page?
Yes, you can. Just make sure the location block includes fastcgi_pass or include fastcgi_params; to process PHP. However, for error pages, a static HTML file is simpler and more reliable.
How do I test a 400 error if I don’t know how to trigger one?
You can use curl with a malformed request, like curl -i -X FOO http://example.com/ (an invalid method). Or use curl --http1.0 --request-target "*" http://example.com/ to send an invalid request target.
What if I have multiple server blocks? Do I need to add the directive to each?
Yes, each server block that should use the custom error page needs its own error_page directive. You can also place it in the http block to apply globally, but then all sites will use the same page.
Is there a way to redirect only for certain types of 400 errors?
No, nginx doesn’t distinguish between different causes of a 400 error. The directive applies to all 400 responses. If you need different handling, you’d have to use a more complex setup with error_page and named locations, but that’s rarely necessary.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Secure Boot Battlefield 6 ASUS: Fix & Enable Guide
- Best Tech Websites to Follow for Pros & Beginners
- Second Wind Quest Walkthrough: Where Winds Meet6.00 min/km
- Enshrouded Weapon Upgrade Guide: How to Improve Gear5.00 min/km
- Connect Phone to Controller for Cloud Gaming
- Python Search Highlighting: Snippets & Matches