nginx tip: 500 Internal Server Error error resolution

  • 2020-05-09 19:41:13
  • OfStack

Now more and more sites are starting to use Nginx,("engine x"), which is a high performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev, the second most visited Rambler.ru site in Russia, and it has been running there for more than two and a half years. Igor publishes the source code as a class BSD license.

In the case of high 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 equivalent to 10 times that of Apache in the same environment.

But a lot of people get 500 errors when they use Nginx, and in my case that's partly because the file opening handle is too small.

Use this command under linux to increment the file handle opened by the process.
ulimit -SHn 51200
By default, only 1000 is used when the number of links is small and cannot be seen. Using this processing method can effectively prevent 500 errors from appearing.
When you visit the website today, you will occasionally encounter an error page for 500 Internal Server Error.
Looked up the relevant information that access is too large, the system kernel process is restricted to appear.

The answer is as follows:

$ ulimit -n
11095

The program is limited to opening 11,095 files. The ulimit command sets the number of file descriptors that a process can own for the current user.
It seems that the number of simulates is too much, so we need to adjust the number of concurrent Settings of nginx.conf (the memory of my configured host is 2G, and CPU is 2.8G).

 
vi /etc/nginx/nginx.conf 
events { 
worker_connections 1024; 
} 


Adjusted for
 
events { 
worker_connections 10240; 
} 


Again, the above problem will appear. Use
[root@qimutian nginx]# cat /proc/sys/fs/file-max
8192
The maximum number of open files on the file system
[root@qimutian nginx]# ulimit -n
1024
The program is limited to opening 1024 files
Adjust it 1 time using [root @qimutian nginx]# ulimit-n 8192
Or permanent can adjust the number of open file at boot file/etc/rc d/rc. Added to the end local (added to the end in/etc sysctl. conf fs. file - max = 8192).
ulimit -n 8192
Adjust the number of open CentOS5 files
When ulimit-a1 is used, it is found that OPEN FILES cannot exceed 1024 by default. In the stress test conducted yesterday, there were 500 errors, please check for details
nginx appears as 500 Internal Server Error
Get up in the morning to look at 1, found that the original is through the following way to adjust
Method 1 (permanent adjustment)

vi /etc/security/limits.conf

Add the following at the end of the document:

* soft nofile 8192
* hard nofile 20480

Also added at the end of vi /etc/ sysctl.conf
fs.file-max=8192
On reboot, the number of views using ulimit-n is already 8192

Method 2 (temporary)

Enter ulimit-n 8192 directly at the terminal and press enter to ok

500 Internal Server Error

1. The hard drive is full

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

2. nginx configuration file error

This does not mean a syntax error, nginx will prompt you at startup if the configuration file has a syntax error. When configuring rewrite, some rules can cause 500 errors if they are not handled properly. Please check your rewrite rules carefully. If some of the variables in the configuration file are set incorrectly, 500 errors can occur, such as a reference to a variable with no value.

3. If none of the above problems exists, it may be because the number of simulates is too much, so it is necessary to adjust the number of concurrent Settings of nginx.conf

The solution:

1 open/etc security/limits. conf files, plus the two sentences

 
* soft nofile 65535 
* hard nofile 65535 


2 open/etc/nginx/nginx conf
Add 1 line below worker_processes

worker_rlimit_nofile 65535; 


Restart nginx and reload the Settings

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 


When I restarted the nginx error log, I didn't find 500 errors.


4. There may be a problem with the database. I did not find any problem in the nginx log, php log.


Related articles: