nginx tip: 500 Internal Server Error error solution

  • 2020-05-06 12:16:51
  • OfStack

More and more sites are now using 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 site in Russia, Rambler.ru, and has been operating 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 10 times that of Apache in the same environment.

But a lot of people get 500 errors with Nginx, and from what I've seen, a lot of that is because the file opening handle is too small.

USES this command under linux to increase the file handle opened by the process.
ulimit -SHn 51200
The default is only 1000 when the number of links is small and it is not visible. Using this method can effectively prevent 500 errors.
When I visited the website today, I occasionally encountered 500 Internal Server Error error page.
Check the relevant information and think that the access is too large, the system kernel process is limited.

The answer is:

$ ulimit -n
11095

The program can only open 11,095 files. The ulimit command sets the number of file descriptors a process can have 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 configuration host is 2G, and CPU is 2.8G,)

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


Adjusted for
 
events { 
worker_connections 10240; 
} 


The above problem will still occur, using
[root@qimutian nginx]# cat /proc/sys/fs/file-max
8192
The maximum number of open files on the file system is
[root@qimutian nginx]# ulimit -n
1024
The program is limited to opening 1024 files,
Adjust
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 CentOS5 files open to
After using ulimit-a, we found that OPEN FILES could not exceed 1024 by default. There were 500 errors in the stress test conducted yesterday. For details, please check
nginx appears as 500 Internal Server Error
When I got up in the morning, I found that I had adjusted
in the following way Method 1 (permanent adjustment)

vi /etc/security/limits.conf

Add
at the end of the file
* soft nofile 8192
* hard nofile 20480


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

Method 2 (temporary)

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

500 Internal Server Error
1. The hard drive is full of

Use df-k to see if the hard drive is full. Clean up your hard drive to resolve 500 errors. nginx if access log is turned on, 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 is not a syntax error; nginx will prompt you when the configuration file is started if it 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. 500 errors can also occur if some variables in the configuration file are set incorrectly, such as a reference to a variable with no value.

3. If none of the above problems exist, it may be that the number of simulates is too much, so it is necessary to adjust the number of nginx.conf's concurrency setting number

The solution:

1 open/etc security/limits. conf file, plus two

 
* soft nofile 65535 
* hard nofile 65535 


2 open/etc/nginx/nginx conf
Add an
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 


After restarting the nginx error log, there is no 500 error reported.


4. There may be a problem with the database. I did not find any problem in the nginx log php, and finally found that the database could not be accessed

Related articles: