# 502错误

### php7.0+nginx+mysql+centos7.0

#### 排查步骤一： 查看nginx日志

* 若日志没有报什么错，一切正常，显然不是nginx出的问题；跳过此

```bash
# 1、 FastCGI进程是否已经启动
ps aux | grep php

# 2、 FastCGI worker进程数是否不够； 判断是否接近FastCGI进程，接近配置文件中设置的数值，表明worker进程数设置太少；
netstat -anpo | grep “php-cgi” | wc -l


# 3、 FastCGI执行时间过长 （根据实际情况调高以下参数值）
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

# 4、FastCGI Buffer不够 （nginx和apache一样，有前端缓冲限制，可以调整缓冲参数）
fastcgi_buffer_size 32k;
fastcgi_buffers 8 32k;


# 5、 其它原因， CPU和内存爆满接近100%， 优化 || 升级配置
```

#### 排查步骤一： 查看php日志

nginx 502 Bad Gateway出现的原因：

> WARNING: \[pool www] server reached pm.max\_children setting (5), consider raising it 解决方法

```bash
# 设置子进程最大数值; 原是 5 个进程
pm.max_children = 30 
```

> php-fpm.conf 配置 性能优化 参数详解

```bash
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/ local/php/var/log/php-fpm.error.log
log_level = notice

[www]
listen = /tmp/php-cgi.sock
listen.backlog = -l
listen.allowed_clients = 127.o.o.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic               ; 或 pm = static 表示使用哪种进程数量管理方式
pm.max_children = 6        ; 静态方式下开启的php-fpm进程数量
pm.start_servers = 4       ; 动态方式下的起始php-fpm进程数量
pm.min_spare_servers = 4   ; 动态方式下的最小php-fpm进程数量
pm.max_spare_servers = 6   ; 动态方式下的最大php-fpm进程数量
request_terminate_timeout = 100
request_slowlog_timeout = 10s ;配置php慢日志时间
slowlog = var/log/slow.log    ;配置php慢日志文件
rlimit_files = 1024           ;配置php-fpm进程可打开的最大文件句柄数,默认1024
```

总结：内存小的建议用动态（pm = dynamic），内存大的建议用静态（pm = static）。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/nginx/502-cuo-wu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
