Solution to Nginx 500 Internal Server Error

Solution to Nginx 500 Internal Server Error

Today, when I was using Nginx, a 500 error occurred. I searched for the error code and shared it with everyone.

500 (Internal Server Error) The server encountered an error and could not complete the request.
501 (Not Implemented) The server does not have the functionality to complete the request. For example, a server might return this code when the server does not recognize the request method.
502 (Bad Gateway) The server, while acting as a gateway or proxy, received an invalid response from an upstream server.
503 (Service Unavailable) The server is currently unavailable (either because it is overloaded or down for maintenance). Usually, this is only a temporary condition.
504 (Gateway Timeout) The server, acting as a gateway or proxy, did not receive a request from an upstream server in a timely manner.
505 (HTTP Version Not Supported) The server does not support the HTTP protocol version used in the request.

Nginx 500 error (Internal Server Error): A 500 error refers to an internal server error, which means that the server encountered an unexpected situation and was unable to fulfill the request.

In the case of high concurrent connections, Nginx is a good alternative to Apache server. 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 handle more than 30,000 concurrent connections, which is 10 times that of Apache under the same environment.

But many people will encounter 500 errors when using Nginx. According to my experience, there are the following situations.

1. Is there insufficient disk space?

Use df -k to check whether the hard disk space is full. Clearing up hard drive space can resolve 500 errors. If nginx has access log enabled, it is best to disable access log if it is not needed. The access log will take up a lot of hard disk space.

2. Is the nginx configuration file wrong?

This does not refer to syntax errors. If there are syntax errors in the nginx configuration file, it will be prompted when it is started. When configuring rewrite, some rules may not be handled properly and a 500 error may occur. Please check your rewrite rules carefully. A 500 error may also occur if some variables in the configuration file are not set correctly, such as referencing a variable that has no value.

3. If none of the above problems exist, it may be that the number of simulated concurrency is too large, and you need to adjust the concurrency setting number of nginx.conf

4. Another problem is that the Linux index node (inode) is full and causes failure. df -i

500 problem caused by inode fullness https://www.jb51.net/article/175430.htm

Inode is translated into Chinese as index node. After each storage device (such as a hard disk) or a partition of a storage device is formatted as a file system, it should have two parts, one is the inode and the other is the Block. Block is used to store data. The inode is used to store this data information, including file size, owner, user group, read and write permissions, etc. Inode indexes information for each file, so there is an inode value. The operating system can find the corresponding file as quickly as possible through the inode value based on the instructions. Although there are still blocks left on the server, the inode is full. Therefore, when creating a new directory or file, the system prompts that there is insufficient disk space.

General analysis ideas:

(1) Check the nginx error log and the php error log

nginx error log error.log default location: /usr/local/nginx/logs

(2) If there are 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 there are still too many open files after all optimizations are done, you should consider load balancing to distribute the 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 a line below worker_processes

The code is as follows:

worker_rlimit_nofile 65535;

3 Restart nginx and reload the 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, I looked at the nginx error log and found no 500 error.

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

If the problem still cannot be solved by the above method, there may be an error in the configuration or program.

1 Check the nginx error log to find the possible cause.

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

2 If there is a problem with the database connection, a 500 error may also occur, but it will definitely be reflected in the log

If the log indicates that there is a problem with the corresponding database connection, you need to check whether the database connection is correct. According to the log, modify the corresponding file. After the database problem is resolved, the page will return to normal.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of PHP+nginx service 500 502 error troubleshooting ideas
  • nginx prompt: 500 Internal Server Error error solution
  • Nginx worker_connections configuration is too low, resulting in 500 errors
  • nginx prompt: Solution to 500 Internal Server Error

<<:  Teach you how to implement a react from html

>>:  Detailed explanation of MySQL cluster: one master and multiple slaves architecture implementation

Recommend

JavaScript Basics: Error Capture Mechanism

Table of contents Preface Error Object throw try…...

HTML+CSS to achieve drop-down menu

1. Drop-down list example The code is as follows:...

18 Web Usability Principles You Need to Know

You can have the best visual design skills in the...

Sample code for a simple seamless scrolling carousel implemented with native Js

There are many loopholes in the simple seamless s...

Detailed explanation of this pointing problem in JavaScript

Preface The this pointer in JS has always been a ...

JavaScript to display hidden form text

This article shares the specific code of JavaScri...

Complete MySQL Learning Notes

Table of contents MyISAM and InnoDB Reasons for p...

Introduction to several ways to introduce CSS in HTML

Table of contents 1. Embed CSS styles directly in...

Detailed explanation of creating stored procedures and functions in mysql

Table of contents 1. Stored Procedure 1.1. Basic ...

Detailed examples of float usage in HTML/CSS

1. Basic usage examples of float 1. Let's fir...

Classification of web page color properties

Classification of color properties Any color can ...

Solution to the problem that Centos8 cannot install docker

Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...