Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What to do about 502 error in Nginx

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to do Nginx 502 errors, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Explain the error resolution of Nginx 502 in detail

1. Configuration error

Because nginx cannot find php-fpm, it reports an error. Generally, the path behind fastcgi_pass is misconfigured, which can be followed by socket or ip:port.

two。 Resource exhaustion

When lnmp architecture processes php, nginx directly calls back-end php-fpm services. If the number of requests for nginx is too high and we do not configure enough child processes for php-fpm, then php-fpm will run out of resources. Once the resources are exhausted, nginx cannot find php-fpm.

The solution is to adjust the pm.max_children value in php-fpm.conf to increase it, but it cannot be increased indefinitely. After all, resources are limited. If a 4G memory machine runs php-fpm and nginx, the mysql can be set to 150Jet 8G to 300G, and so on.

3. In addition to the above two errors, there are other reasons, rarely, we can use the nginx error log to troubleshoot vim/usr/local/nginx/ logs/nginx_error.log, we can also find error_log for the log definition level vim/usr/local/nginx/conf/nginx.conf, the default is the most stringent crit on the line, can also be changed to debug display the most comprehensive information, but it is easy to burst our disk.

First of all, we need to give the browser access to modify the nginx configuration file.

[root@wqslinux ~] # vim/usr/local/nginx/conf/vhosts/111.confserver {listen 80; server_name www.111.com; / / Domain address index index.html index.htm index.php; root / data/www/; location ~\ .php$ {include fastcgi_params; fastcgi_pass unix:/tmp/www.sock; / / modify sock # fastcgi_pass 127.0.0.1 listen 9000; fastcgi_index index.php Fastcgi_param SCRIPT_FILENAME / data/www$fastcgi_script_name;}}

Check whether the syntax is normal

[root@wqslinux] # / usr/local/nginx/sbin/nginx-t

Reload the configuration file

[root@wqslinux ~] # / usr/local/nginx/sbin/nginx-s reload [root@wqslinux ~] # / etc/init.d/nginx reload

Check that nginx is run by that user.

[root@wqslinux ~] # ps aux | grep nginx

To edit the php-fpm file, we need to set the user master and group of nginx in this php-fpm file so that 502 is not displayed.

[root@wqslinux] # vim/usr/local/php/etc/php- fpm.confu [global] pid = / usr/local/php/var/run/php-fpm.piderror_log = / usr/local/php/var/log/php- fpm.log [www] listen = / tmp/www.sockuser = php-fpmgroup = php-fpmlisten.owner / / define owner listen.group = nobody / / define generic group pm = dynamicpm.max_children = 50pm.start_servers = 20pm. Min_spare_servers = 5pm.max_spare_servers = 35pm.max_requests = 500rlimit_files = 1024

Restart php-fpm after configuration

[root@wqslinux ~] # / etc/init.d/php-fpm restart

Ps: in addition, it is a problem encountered by many students recently. in this case, socket is used. The default monitoring socket file permissions of version 5.4 (including 5.4) are owner read-only, belonging to groups and other users do not have any permissions. Therefore, the startup user of nginx (we configured nobody) has no way to read the socket file, resulting in 502, which can be found in the error log of nginx. The solution is simple, and there are configurations to avoid this problem in the configuration file given above.

Listen.owner = nobody / / define the owner listen.group = nobody / / define the group

These two configurations define who the owner and group of socket are. There's another way besides this.

Listen.mode = 777

This way nobody can also have read permissions.

The above is all the content of this article "what to do with 502 errors in Nginx". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report