NGINX + PHP-FPM ERROR 502 FIX
Budget: $30 – $250 USD
My server continuously gets 502 errors after TCP connections build up and the upstream server (php-fpm) just crashes down and stops responding to Nginx servers request. Need to find a permanent solution for this issue and investigate the problem and why it is causing it.
It solves if run command restarting/reloading php-fpm but builds up back again pretty quickly again.
Below in the image you can see how TCP connections establish and stay, until reach maximum pm.max_children, which was 300, then changed to 100.
Errors:
________
2022/04/19 13:11:18 [error] 32821#0: *47 connect() to unix:/run/php-fpm/www.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: *.*.*.*, server: *.*.*.*, request: "GET /some/path HTTP/1.0", upstream: "fastcgi://unix:/run/php-fpm/www.sock:", host: "backendservers"
________
2022/04/19 14:03:25 [error] 33785#0: *1904 upstream timed out (110: Connection timed out) while reading response header from upstream, client: *.*.*.*, server: *.*.*.*, request: "GET /some/path HTTP/1.0", upstream: "fastcgi://unix:/run/php-fpm/www.sock", host: "backendservers"
________
[19-Apr-2022 00:48:25] WARNING: [pool www] server reached max_children setting (300), consider raising it
[19-Apr-2022 09:02:00] WARNING: [pool www] server reached pm.max_children setting (100), consider raising it
SETUP: *nginx.conf*
===============
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
}
===============
HOST SETUP: *default.conf*
===============
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name *.*.*.*;
root /var/www/html/some/path;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri.html $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri = 404;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
===============
It solves if run command restarting/reloading php-fpm but builds up back again pretty quickly again.
Below in the image you can see how TCP connections establish and stay, until reach maximum pm.max_children, which was 300, then changed to 100.
Errors:
________
2022/04/19 13:11:18 [error] 32821#0: *47 connect() to unix:/run/php-fpm/www.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: *.*.*.*, server: *.*.*.*, request: "GET /some/path HTTP/1.0", upstream: "fastcgi://unix:/run/php-fpm/www.sock:", host: "backendservers"
________
2022/04/19 14:03:25 [error] 33785#0: *1904 upstream timed out (110: Connection timed out) while reading response header from upstream, client: *.*.*.*, server: *.*.*.*, request: "GET /some/path HTTP/1.0", upstream: "fastcgi://unix:/run/php-fpm/www.sock", host: "backendservers"
________
[19-Apr-2022 00:48:25] WARNING: [pool www] server reached max_children setting (300), consider raising it
[19-Apr-2022 09:02:00] WARNING: [pool www] server reached pm.max_children setting (100), consider raising it
SETUP: *nginx.conf*
===============
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
server_names_hash_bucket_size 64;
}
===============
HOST SETUP: *default.conf*
===============
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name *.*.*.*;
root /var/www/html/some/path;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
try_files $uri $uri.html $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri = 404;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
}
===============