环境:
$ php --version
:PHP7.2.24 (cli)$ nginx -v
:nginx版本: nginx/1.14.0 (Ubuntu)问题陈述:
在尝试POST
请求时,我从NGINX获得了一个特定端点上的502个错误网关错误,同时在API请求中包含了文件。
但是,它在其他端点上正常工作,在那里我不需要在请求中添加文件。
这里有什么问题吗?
错误
NGINX 502坏网关
日志
$ sudo tail -30 /var/log/nginx/error.log
[error] 4713#4713: *705118 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 111.11.11.111, server: domain.com, request: "POST /action/api/path HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "domain.com", referrer: "domain.com/path"
$ sudo tail /var/log/php7.2-fpm.log
WARNING: [pool www] child 28524 exited on signal 11 (SIGSEGV - core dumped) after
NOTICE: [pool www] child 8033 started
文件和配置:
/etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
# max post size
client_max_body_size 100M;
}
/etc/nginx/sites-available/domain.com
server {
listen 443;
server_name domain.com;
root /path/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.chained.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
server {
listen 80;
server_name domain.com;
rewrite ^/(.*) https :// domain.com/$1 permanent;
}
/etc/php/7.2/fpm/pool.d/www.conf
(池指令)
[www]
user = www-data
group = www-data
listen = /run/php/php7.2-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
/etc/php/7.2/fpm/php-fpm.conf
(全球指令)
[global]
pid = /run/php/php7.2-fpm.pid
error_log = /var/log/php7.2-fpm.log
include=/etc/php/7.2/fpm/pool.d/*.conf
cURL请求
private $headers = [
'Accept: application/json',
'Content-Type: application/json',
];
private $baseURL = 'http://otherdomain.in/api/v1/';
private function postRequest($data, $endpoint) {
if ( !is_null($this->apiToken) ) {
$authorization = "Authorization: Bearer {$this->apiToken}";
array_push($this->headers, $authorization);
}
$url = "{$this->baseURL}/{$endpoint}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$responseJSON = curl_exec($ch);
$response = json_decode($responseJSON, TRUE);
return $response
}
编辑1:
我使用下面的fastcgi
重新启动了query进程
$ sudo service php7.2-fpm restart
$ sudo tail /var/log/php7.2-fpm.log
NOTICE: systemd monitor interval set to 10000ms
WARNING: [pool www] child 28870 exited on signal 11 (SIGSEGV - core dumped) after 53.229996 seconds from start
NOTICE: [pool www] child 28879 started
NOTICE: Terminating ...
NOTICE: exiting, bye-bye!
NOTICE: fpm is running, pid 29564
NOTICE: ready to handle connections
NOTICE: systemd monitor interval set to 10000ms
WARNING: [pool www] child 29592 exited on signal 11 (SIGSEGV - core dumped) after 17.115362 seconds from start
NOTICE: [pool www] child 29596 started
编辑2:
我发现我的opcache
已经在评论了。因此,按照下面的answer,禁用或增加内存限制是没有意义的
/etc/php/7.2/fpm/php.ini
[opcache]
; Determines if Zend OPCache is enabled
;opcache.enable=0
; Determines if Zend OPCache is enabled for the CLI version of PHP
;opcache.enable_cli=0
; The OPcache shared memory storage size.
;opcache.memory_consumption=196
编辑3:
当我按照下面的listen
更改query时
/etc/php/7.2/fpm/pool.d/www.conf
listen = /run/php/php7.2-fpm.sock
至
listen = 127.0.0.1:9000
$ sudo service php7.2-fpm restart
Job for php7.2-fpm.service failed because the control process exited with error code.
See "systemctl status php7.2-fpm.service" and "journalctl -xe" for details.
$ systemctl status php7.2-fpm.service
● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2021-11-13 06:13:51 UTC; 1min 21s ago
Docs: man:php-fpm7.2(8)
Process: 30120 ExecStart=/usr/sbin/php-fpm7.2 --nodaemonize --fpm-config /etc/php/7.2/fpm/php-fpm.conf (code=exited, status=78)
Main PID: 30120 (code=exited, status=78)
systemd[1]: Starting The PHP 7.2 FastCGI Process Manager...
php-fpm7.2[30120]: [13-Nov-2021 06:13:51] ERROR: unable to bind listening socket for address '127.0.0.1
php-fpm7.2[30120]: [13-Nov-2021 06:13:51] ERROR: FPM initialization failed
systemd[1]: php7.2-fpm.service: Main process exited, code=exited, status=78/n/a
systemd[1]: php7.2-fpm.service: Failed with result 'exit-code'.
systemd[1]: Failed to start The PHP 7.2 FastCGI Process Manager.
编辑4:
正如@viktorovsky在answer中指出的那样。我已经将PHP的版本从7.4更改为7.2,因为我只有php7.2。
看起来您使用了两个版本的PHP7.2和7.4
/etc/nginx/sites-available/domain.com
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
..
}
仍有相同的原木
$ sudo tail -f /var/log/nginx/error.log
[error] 23250#23250: *74 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 111.11.11.111, server: domain.com, request: "POST /action/api/path HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "domain.com", referrer: "domain.com/path"
编辑5:
http {
fastcgi_read_timeout 400s;
# max post size
client_max_body_size 100M;
send_timeout 10m;
client_header_timeout 10m;
client_body_timeout 10m;
large_client_header_buffers 8 1024k;
server {
location / {
proxy_buffer_size 1024k;
proxy_buffers 4 1024k;
proxy_busy_buffers_size 1024k;
}
}
}
发布于 2021-11-13 11:56:31
看起来您使用了2个版本的PHP,7.2和7.4
环境:PHPVersion$ PHP --版本:PHP7.2.24 (cli)
但是在/etc/nginx/sites-available/domain.com
配置中:
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
..
}
您可以去掉7.2,只使用7.4,然后再检查php日志。
https://stackoverflow.com/questions/69927311
复制相似问题