Nginx experiencing 502 gateway errors when accessing my site

Debian 10
Nginx

nginx error log
I’m experiencing 502 gateway errors when accessing my site from time to time.

Ubuntu 20.04, Nginx, PHP-FPM 7.4, Virtualmin, Cloudflare

/etc/php/7.4/fpm/pool.d pastebin

/etc/php/7.4/fpm/pool.d pastepin

nginx error log

```
2021/01/02 17:14:00 [error] 56540#56540: *248074 connect() failed (111: Connection refused) while connecting to upstream, client: 188.114.110.89, server: example.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "www.example.com", referrer: "https://www.example.com/"
2021/01/02 17:14:26 [error] 56540#56540: *248088 connect() failed (111: Connection refused) while connecting to upstream, client: 162.158.165.247, server: example.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "www.example.com", referrer: "https://www.example.com/index.php"
2021/01/02 17:15:14 [error] 56540#56540: *248098 connect() failed (111: Connection refused) while connecting to upstream, client: 162.158.134.121, server: example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "www.example.com"
2021/01/02 17:15:43 [error] 56540#56540: *248109 connect() failed (111: Connection refused) while connecting to upstream, client: 188.114.110.89, server: example.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "www.example.com", referrer: "https://www.example.com/"
```

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 {

	##
	# Basic Settings
	##

	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 Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
	server_names_hash_bucket_size 128;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

nginx - site-available

default
##
# You should look at the following URL’s in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# Getting Started | NGINX
# Pitfalls and Common Mistakes | NGINX
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#       fastcgi_pass unix:/run/php/php7.3-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#	listen 80;
#	listen [::]:80;
#
#	server_name example.com;
#
#	root /var/www/example.com;
#	index index.html;
#
#	location / {
#		try_files $uri $uri/ =404;
#	}
#}

nginx - site-available
mydomailname.com.conf

server {
server_name mydomailname.com www.mydomailname.com;
listen 2xx.1xx.x.xx;
root /home/mydomailname/public_html;
index index.html index.htm index.php;
access_log /var/log/virtualmin/mydomailname.com_access_log;
error_log /var/log/virtualmin/mydomailname.com_error_log;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME /home/mydomailname/public_html$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT /home/mydomailname/public_html;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS $https;
fastcgi_keep_conn on;
location ~ .php(/|$) {
try_files $uri $fastcgi_script_name =404;
fastcgi_pass localhost:8000;
}
fastcgi_split_path_info ^(.+.php)(/.+)$;
listen 2xx.1xx.xx.xx:443 default ssl;
ssl_certificate /home/mydomailname/ssl.cert;
ssl_certificate_key /home/mydomailname/ssl.key;
}

When exactly does that happen or better to ask during what does it happen?

gave up on nginx not much help around better stick to apache.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.