Feb 9, 2013

f Comment

Web Server: How Do You Fix 500 Internal Error?

Amazon Recently I found that my Nginx web server serving traffic to http://www.mensfashionforless.com/ is returning 500 Internal Server Errors. I immediately got alarmed and started to look into the causes. I was able to fix the 500 internal error which was actually caused by the FastCGI PHP engine that processes PHP scripts.

Here are the steps to find and fix the 500 internal server errors.

Read Your Web Server's Error Log
I use Nginx and in Nginx's configuration file I set my own error log file location. In that error log I saw the following log entry (let's call it Log Entry #1):

2012/02/09 17:33:17 [alert] 10255#0: 512 worker_connections are not enough
And the following log entry (let's call it Log Entry #2):

2012/01/31 17:26:58 [error] 7538#0: *101 FastCGI sent in stderr: "PHP Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocate 261900 bytes) in Unknown on line 0" while reading response header from upstream, client:, server: www.mensfashionforless.com, request: "HEAD /gray-hoodie.html HTTP/1.0", upstream: "fastcgi://", host: "www.mensfashionforless.com"
Incidentally your web server's access log entry related to this error may look something like this: - - [09/Feb/2012:23:07:38 +0000] "GET /nordstrom-red-shirt.html HTTP/1.0" 500 0 "-" "Wget/1.12 (linux-gnu)"

Fix the Error Indicated by the Web Server's Error Log
To fix the error indicated by Log Entry #1 simply increase 'worker_connections' in Nginx's configuration file. My Nginx's configuration is located at /etc/nginx/nginx.conf and I put the following lines in the file:
events {
    worker_connections  1024;
Restart the web server. If your web server is not Nginx but generates the similar error simply look up the server's manual to see how to do the same thing.

To fix the error indicated by Log Entry #2 simply increase 'memory_limi' in php.ini as follows:

memory_limit = 128M
My php.ini is located at /etc/php5/cgi/php.ini.

Incidentally increasing the maximum amount of memory a PHP script may consume is usually not the answer. The real fix would be fix the script that causes the memory leak.

I looked through the PHP source code and found that I was putting large amounts of data into a variable. I changed it so that I only put the data I need into the variable and the error was gone without having to change 'memory_limit'.

If your web server's error log shows other errors you don't know how to fix let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael