Nginx自定义500、502错误页面本地环境不生效排查求助
Hey Nick, welcome to Stack Overflow! 😊 Great job narrowing down that your config works in development but fails locally—this usually points to environment-specific quirks rather than the Nginx config itself. Let’s walk through the most likely fixes for your local setup:
Verify the
error.htmlfile exists and has correct permissions
Since your Nginx root is set to/var/www/html/public, make sureerror.htmlis placed exactly in that directory inside the container. If you’re using a volume mount from your local machine, double-check:- The file is present in your local
publicfolder (and syncing to the container) - The container’s
www-datauser has read access to it. Fix permissions by running this inside the Nginx container:chown www-data:www-data /var/www/html/public/error.html chmod 644 /var/www/html/public/error.html
- The file is present in your local
Rule out VirtualBox shared folder sync issues
Your config already hassendfile off(smart call for Vagrant/VirtualBox), but shared folders can still have sync delays or failures. Try:- Restarting the Nginx container to force it to pick up the file:
docker restart <your-nginx-container-name> - Temporarily copying
error.htmldirectly into the container (bypassing the mount) to test if sync is the problem:docker cp ./local/public/error.html <your-nginx-container-name>:/var/www/html/public/error.html
- Restarting the Nginx container to force it to pick up the file:
Disable browser caching or use incognito mode
Browsers often cache error pages, especially if you’ve tested the default 502 multiple times. Open a private/incognito window and hit your/get_errorendpoint again—this ensures you’re not seeing a cached version of the default page.Inspect Nginx logs for direct clues
Check the Nginx container’s logs to see if it’s having trouble accessingerror.html:docker logs <your-nginx-container-name> | grep error.htmlLook for lines like
Permission deniedorFile not found—these will directly tell you why the custom page isn’t loading.Toggle
fastcgi_intercept_errorsfor PHP-related errors
While your/get_errortest is a pure Nginx 500, if you’re seeing the default 502 when PHP-FPM is down, try settingfastcgi_intercept_errors onin your PHP location block. Right now you have it set tooff—changing this tells Nginx to intercept FastCGI errors and use your custom page instead of letting PHP-FPM send its own. Since your config works in dev, it’s possible that environment has this setting enabled implicitly.
Since your config works in development, the issue is almost certainly a local setup hiccup. Start with checking the file existence/permissions and logs—those are the most common culprits in Docker environments.
内容的提问来源于stack exchange,提问作者Nick Bardi

