Uncategorized

Nginx responded with 500 but no error in log.

Today I tried to deploy my Laravel app on the cloud using Nginx and PHP-FPM on the front layer just for testing. I got stuck that Nginx gives 500 status code without any clear reason. I spent hours to find the root cause until I eventually find it. This documents briefly explain the approach I took to deduce the problem.

Check Nginx error log

First thing that I do was to check Nginx error log, it usually exists on /var/log/nginx/error.log. When a new request made to the server, there is no new error on the log. Instead, Nginx logged the request on access.log with 500 status code on the row. The fact that there is no error from Nginx point of view meaning that it successfully forwarded the request to the next layer: php-fpm.

Check php-fpm log

I checked php-fpm log to see if I can get any clue only to find nothing. Not even any requests add any data on the log.

Check Laravel log

Last, I tried to check the Laravel error and access log on storage/logs and yet find no clue about what happened.

Checking File Permission

A lot of times, nginx cannot proceed the request due to permission problems. But if that’s the case, usually we’ll see the error log on /var/log/nginx/error.log. Nevertheless, I checked if there is any weird permission and compare it with file permissions on my local and see no problem.

Real root cause: .env file

The last thing I did was checking .env file, I was aware that the APP_ENV and APP_URL was still set to default value, but I don’t think it would bring this kind of problem. If any, I should be able to see the error page from Laravel instead of empty 500 like currently. So I opened the file only to find that I mistakenly put random string at the end of the file. I assume this string makes Laravel failed to parse the .env file. So I remove that random string and voila, it is now showing the normal Laravel error.

Conclusion

When you found an error in Laravel (or other applications) that you can’t easily figure out what’s going on from the log, it is likely because the error happened on the very early of app lifecycle. So, try to give a moment to check the app configuration, you probably find something in there.