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

Pull-down refresh and pull-up loading components based on Vue encapsulation

Based on Vue and native javascript encapsulation,...

How to create a responsive column chart using CSS Grid layout

I have been playing around with charts for a whil...

Node.js returns different data according to different request paths.

Table of contents 1. Learn to return different da...

Detailed explanation of three ways to connect Docker containers to each other

There are three ways to interconnect and communic...

Summary of Mysql high performance optimization skills

Database Command Specification All database objec...

MySQL 5.6 root password modification tutorial

1. After installing MySQL 5.6, it cannot be enabl...

Comparison of the efficiency of different methods of deleting files in Linux

Test the efficiency of deleting a large number of...

Implementation of Docker private warehouse registry deployment

As more and more Docker images are used, there ne...

MySQL Error 1290 (HY000) Solution

I struggled with a problem for a long time and re...

Implementation of Docker deployment of SQL Server 2019 Always On cluster

Table of contents Docker deployment Always on clu...

How to get the dynamic number of remaining words in textarea

I encountered a case at work that I had never wri...

How to redirect nginx directory path

If you want the path following the domain name to...

How to install MySQL 5.7.29 with one click using shell script

This article refers to the work of 51CTO blog aut...

Python 3.7 installation tutorial for MacBook

The detailed process of installing python3.7.0 on...