nginx Tip: 500 Internal Server Error workaround _nginx_ Script Home

nginx prompt: 500 Internal Server Error solution

Updated: April 17, 2013 19:15:07 Author:
This article summarizes a lot of reasons for nginx prompt 500 Internal Server Error and solution analysis. Friends who need to know can refer to it

Nginx ("engine x") is a high-performance HTTP and reverse proxy server, as well as an IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev for Russia's second most visited Rambler.ru site, where it has been running for more than two and a half years. Igor distributes the source code as a BSD-like license. Nginx is a good alternative to the Apache server for high concurrent connections. Nginx can also be used as a Layer 7 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, equivalent to 10 times the number of Apache in the same environment. But many people will get 500 errors when using Nginx, based on my use of the case, a large part of the reason is because the file open handle is too small related. Use this command under linux to increase the file handle opened by the process. ulimit-SHn 51200 default only 1000 when the number of links is small can not be seen, using this method can effectively prevent 500 errors. When I visit the website today, I occasionally encounter a 500 Internal Server Error page. After checking the relevant information, it is believed that the access is too large and the system kernel process is limited. 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 have for the current user. It seems that the number of concurrent simulations is too much, we need to adjust the number of concurrent Settings in nginx.conf, (my configuration host has 2G memory and 2.8G CPU).

Copy codeThe code is as follows:

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


Be adjusted to
Copy codeThe code is as follows:

events {
worker_connections 10240;
}


The above problems will still occur, use
[root@qimutian nginx]# cat /proc/sys/fs/file-max
8192
Maximum number of files that can be opened on a 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
Or permanently adjust the number of open files 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 CentOS5 files open
Use ulimit -a, it is found that OPEN FILES cannot exceed 1024 by default, yesterday in the stress test, there were 500 errors, please check for details
500 Internal Server Error occurs in nginx
When I got up in the morning, I found that it was adjusted in the following way
Method 1 (Permanent Adjustment)

vi /etc/security/limits.conf

At the end of the document add:

* soft nofile 8192
* hard nofile 20480

Add to the end of vi /etc/sysctl.conf
fs.file-max=8192
After restarting, the number of views using ulimit -n is already 8192

Method 2 (for temporary use)

Enter ulimit -n 8192 in the terminal and press Enter

500 Internal Server Error Added:

1. The hard disk space is full

Run the df -k command to check whether the hard disk space is full. Cleaning up hard disk space can solve 500 errors. If access log is enabled in nginx, it is best to disable access log when it is not needed. The access log occupies a large amount of disk space.

2. The nginx configuration file is incorrect

nginx does not refer to syntax errors. If the configuration file has syntax errors, it will prompt you at startup. When configuring the rewrite rule, 500 errors may occur if some rules are improperly handled. Please check your rewrite rule carefully. 500 errors can also occur if some variables in the configuration file are incorrectly set, such as referencing a variable with no value.

3. If the above problems do not exist, it may be that the number of concurrent simulations is too much, and you need to adjust the number of concurrent Settings in nginx.conf

The solution is:

1 open the/etc/security/limits file, plus the two sentences

Copy codeThe code is as follows:

* soft nofile 65535
* hard nofile 65535


2 Open /etc/nginx/nginx.conf
Add a line below 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 the restart, I looked at the nginx error log again, and no 500 errors were found.


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

Related article

  • nginx日常维护常用命令

    nginx routine maintenance commands

    This article mainly introduces nginx daily maintenance such as nginx start, restart, close and other common commands, need friends can refer to the next
    2014-03-03
  • 添加Nginx代理配置只允许内部IP访问的实现方法

    Add an implementation method to configure the Nginx proxy to allow only internal IP access

    In this article, Xiaobian gives you an article about adding Nginx proxy configuration to allow only internal IP access to the implementation method of the article, friends in need can learn.
    2019-10-10
  • 开启Nginx时端口被占用提示:Address already in use

    The port is occupied when Nginx is enabled. "Address already in use" is displayed

    This article mainly introduces the port is occupied when opening Nginx: Address already in use solution, this article through two methods to introduce the Nginx start, stop and restart operation method, need friends can refer to
    2018-09-09
  • 反向代理缓存的详细介绍

    A detailed introduction to reverse proxy caching

    This article mainly introduces the detailed introduction of reverse proxy cache related information, I hope that through this article we can master the application of reverse proxy cache use method, need friends can refer to
    2017-09-09
  • nginx动态添加访问白名单的方法

    nginx dynamically adds a method for accessing the whitelist

    This article mainly introduces nginx dynamic add access to the white list method, Xiaobian feel very good, now share to you, also give you a reference. Let's take a look
    2017-02-02
  • Nexus使用nginx代理实现支持HTTPS协议

    Nexus uses the nginx proxy to support HTTPS

    This article mainly introduces the use of nginx proxy to support HTTPS protocol, the article through the example code is very detailed, for everyone's study or work has a certain reference learning value, need friends can refer to it
    2020-05-05
  • Nginx中break与last的区别详析

    A detailed analysis of the difference between break and last in Nginx

    This article mainly introduces the relevant information about the difference between the break and the last in Nginx. The article introduces it in great detail through the example code, which has certain reference value for your study or work. The friends who need it will study together with the small series below
    2021-01-01
  • nginx代理postgresql的实现示例

    Example implementation of nginx proxy postgresql

    This article mainly introduces the implementation of nginx agent postgresql example, the article through the example code introduction is very detailed, for everyone's study or work has a certain reference learning value, the need of friends below with the small series to learn it
    2023-10-10
  • Linux安装Nginx步骤详解

    Install Nginx on Linux

    This article mainly introduces the steps of Linux installation Nginx, this article gives you a very detailed introduction through the form of illustrations, has a certain reference value for your study or work, the need of friends can refer to the next
    2020-11-11
  • Nginx中keepalive配置小结

    Summary of keepalive configuration in Nginx

    This article introduces the concept and function of Nginx keepalive configuration, describes the basic principle and advantages of keepalive connection, and gives some common configuration examples, which has certain reference value, interested can understand
    2023-09-09

Latest comments