Solution for Nginx 500 Internal Server Error Error


Today, when using Nginx, 500 errors occurred, so I searched for 1 error code and shared it with everyone.

500 (Internal server error) The server encountered an error and could not complete the request. 501 (not yet implemented) server does not have the function to complete the request. For example, when the server does not recognize the request method, the server may return this code. The 502 (Error Gateway) server, acting as a gateway or proxy, received an invalid response from the upstream server. 503 (service unavailable) Server is currently unavailable (due to overload or downtime maintenance). Usually, this is only a temporary state. 504 (Gateway Timeout) The server, acting as a gateway or proxy, did not receive the request from the upstream server in time. The 505 (HTTP version is not supported) server does not support the version of the HTTP protocol used in the request.

Nginx 500 Error (Internal Server Error Internal Server Error): 500 Error is an internal server error, where the server encounters an unexpected condition and cannot fulfill the request.

In the case of highly concurrent connections, Nginx is a good alternative to Apache servers. Nginx can also be used as a 7-tier load balancing server. According to the test results, Nginx 0.6. 31 + PHP 5.2. 6 (FastCGI) can withstand more than 30,000 concurrent connections, which is 10 times that of Apache in the same environment.

However, many people will have 500 errors when using Nginx. According to my use, there are the following situations.   

1. Is there insufficient disk space?

Use df-k to see if your hard disk is full. Cleaning the hard disk space can solve 500 errors. nginx If access log is turned on, it is better to turn off access log when it is not needed. access log takes up a lot of hard disk space.

2. Error in nginx profile?

This does not mean syntax errors. If the nginx configuration file has syntax errors, it will be prompted at startup time. When configuring rewrite, some rules are not handled properly and 500 errors occur. Please carefully check your own rewrite rules. If some variables in the configuration file are set incorrectly, 500 errors will also occur, such as referencing a variable with no value.

3. If the above problems do not exist, it may be that there are too many simulated concurrency settings, so it is necessary to adjust the concurrency settings of nginx. conf under 1

4. There is also the failure caused by the full use of Linux index node (inode), df-i

500 questions caused by full index node (inode) https://www.ofstack.com/article/175430. htm

inode translates into Chinese as an indexed node. After each storage device (such as hard disk) or partition of storage device is formatted as a file system, it should have two parts, one part is inode, the other part is Block, and Block is used to store data. inode is the information used to store these data, including file size, owner, user group, read and write permissions, etc. inode indexes information for each file, so you have the value of inode. According to the instruction, the operating system can find the corresponding file as quickly as possible through inode value. While the server has Block left, inode is full, so when creating a new directory or file, the system prompts that there is not enough disk space.

1 general analysis ideas:

(1) Look at nginx error log, look at php error log

nginx Error Log error. log Default Location:/usr/local/nginx/logs

(2) If it is too many open files, modify the worker_rlimit_nofile parameter of nginx, use ulimit to view the system open file limit, and modify /etc/security/limits. conf

(3) If it is a script problem, you need to fix the script error and optimize the code

(4) If all kinds of optimizations are done well, and too many open files still appears, it is necessary to consider load balancing and distribute traffic to different servers.

The solution is:

1 Open the/etc/security/limits. conf file and add two sentences

The code is as follows:

* soft nofile 65535
* hard nofile 65535

2 Open/etc/nginx/nginx. conf

Add 1 line below worker_processes

The code is as follows:

worker_rlimit_nofile 65535;

3 Restart nginx and reload settings

The code is as follows:

kill -9 `ps -ef | grep php | grep -v grep | awk '{print $2}'`
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 100 -u www-data -f /usr/bin/php-cgi
killall -HUP nginx

After restarting, look at the error log of nginx, and there is no error reported by 500.

4. It may be a database problem. I didn’t find any problems in nginx log and php log. Finally, I found that the database could not be accessed, and the problem was solved after correction.

If the above method still can’t solve the problem, it may be that there is an error in configuration or program.

1 Look at the error log of nginx to find the possible cause.

If you are prompted that some PHP extensions are not installed, go to php. ini to open the corresponding extension or install the corresponding extension, restart nginx and php-fpm, and refresh the page again.

2 If there is a problem with the database connection, 500 errors may occur, but 1 in the log will definitely reflect it

Prompt the corresponding database connection in the log has a problem, it is necessary to check whether the database connection is correct. According to the log, modify the corresponding file, and after the database problem is solved, the page will return to normal.