Nginx prompt: 500 Internal Server Error Solution

Source: Internet
Author: User

More and more sites are using Nginx. ("engine x") is a high-performance HTTP and reverse proxy server, and is also an IMAP/POP3/SMTP proxy server. Nginx was developed by the Rambler.ru site with the highest access volume in Russia as Igor Sysoev. It has been running on this site for more than two and a half years. Igor publishes source code in the form of a class BSD license.

In the case of high-concurrency connections, Nginx is a good alternative to Apache servers. Nginx can also be used as a layer-7 Server Load balancer. According to the test results, Nginx 0.6.31 + PHP 5.2.6 (FastCGI) can withstand more than 30 thousand concurrent connections, equivalent to 10 times of Apache in the same environment.

However, many people encounter the 500 error when using Nginx. According to my usage, a large part of the reason is that the file opening handle is too small.

Use this command in linux to add the file handle opened by the process.
Ulimit-SHn 51200
By default, only 1000 cannot be seen when the number of links is small. This method can effectively prevent 500 errors.
When you visit the website today, you will occasionally encounter the Error message page of 500 Internal Server Error.
I checked the relevant information and thought that the access was too large and the system kernel process was restricted.

The answer is as follows:

$ Ulimit-n
11095

The program can only open 11095 files. The ulimit command sets the number of file descriptors that a process can own.
It seems that the number of concurrent simulation is too large. You need to adjust the number of concurrent settings for nginx. conf (I configured the host's memory to 2 GB, And the CPU is 2.8 GB ,)

Copy codeThe Code is as follows: vi/etc/nginx. conf
Events {
Worker_connections 1024;
}

AdjustedCopy codeThe Code is as follows: events {
Worker_connections 10240;
}

The above problem still occurs. Use
[Root @ qimutian nginx] # cat/proc/sys/fs/file-max
8192
Maximum number of files that can be opened in the file system
[Root @ qimutian nginx] # ulimit-n
1024
The program can only open 1024 files
Use [root @ qimutian nginx] # ulimit-n 8192 to adjust it
You can also permanently adjust the number of files to be opened at the end of the Startup file/etc/rc. d/rc. local (add fs. file-max = 8192 at the end of/etc/sysctl. conf)
Ulimit-n 8192
Adjust the number of opened CentOS5 files
Using ulimit-a, we found that the default value of open files cannot exceed 1024. The Error 500 occurred during the stress test yesterday. For details, see
Nginx Error 500 Internal Server Error
In the morning, I checked it and found that it was adjusted as follows:
Method 1 (permanent adjustment)

Vi/etc/security/limits. conf

Add the following at the end of the file:

* Soft nofile 8192
* Hard nofile 20480

Add at the end of vi/etc/sysctl. conf
Fs. file-max = 8192
Restart. When you use ulimit-n, the number of views is 8192.

Method 2 (for temporary use)

Simply enter ulimit-n 8192 on the terminal and press Enter.

500 Internal Server Error supplement:

1. Hard Disk Space is full

Use df-k to check whether the disk space is full. Clear the hard disk space to solve the 500 error. If access log is enabled for nginx, you are advised to disable access log when you do not need it. Access log occupies a large amount of hard disk space.

2. nginx configuration file Error

This does not mean a syntax error. If the configuration file of nginx has a syntax error, a prompt will be displayed at startup. When configuring rewrite, some rules may encounter a 500 error if they are improperly handled. Check your rewrite rules carefully. If some variables in the configuration file are improperly set, a 500 error occurs, for example, a variable without a value is referenced.

3. If none of the above problems exist, it may be that the number of concurrent threads in the simulation is too large. You need to adjust the number of concurrent settings in nginx. conf.

Solution:

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

Copy codeThe Code is as follows: * soft nofile 65535
* Hard nofile 65535

2 open/etc/nginx. conf
Add a row under worker_processes

Copy codeThe Code is as follows: worker_rlimit_nofile 65535;

3. Restart nginx and reload the settings.

Copy codeThe 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 restart, check the nginx error log again. No Error 500 is reported.

4. There may be a database problem. I did not find any problems in the nginx log php log. I finally found that the database could not be accessed. The problem was solved after the modification.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.