502 Bad Gateway after updating PHP 7.3 to 8.1

Nextcloud version: 25.0.1
Operating system and version: Debian 10)
Apache or nginx version: 1.14.2
PHP version: 8.1

The issue you are facing:
Getting frequent 502 Bad Gateway errors. I suspect something is wrong in my configuration of PHP or nginx.

Is this the first time you’ve seen this error?:
Problem has been persistent since upgrading PHP and needing to update Nextcloud to maintain compatibility.

Steps to replicate it:
Refresh nextcloud page.

The output of your Nextcloud log in Admin > Logging:

Access Forbidden

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => 'xxx',
  'passwordsalt' => 'xxx',
  'secret' => 'xxx',
  'trusted_domains' =>
  array (
    0 => 'example.com',
    1 => '192.168.1.xxx',
  ),
  'datadirectory' => '/var/www/nextcloud/data',
  'dbtype' => 'mysql',
  'version' => '25.0.1.1',
  'overwrite.cli.url' => 'example.com/nextcloud',
  'dbname' => 'nextcloud',
  'dbhost' => 'localhost',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'mysql.utf8mb4' => true,
  'dbuser' => 'username',
  'dbpassword' => 'password',
  'installed' => true,
  'maintenance' => false,
  'chunkSize' => '5120MB',
  'updater.secret' => 'xxx',
  'theme' => '',
  'loglevel' => 2,
  'default_phone_region' => 'us',  # added to eliminate warning on admin console
#   'memcache.local' => '\OC\Memcache\APCu', # added for mem caching, which also eliminates a warning on admin console. Commented because it was not working.
);

The output of your nginx log in /var/log/____:

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/*;
}


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


2022/12/15 20:40:42 [error] 31048#31048: *20479 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET /nextcloud/ocs/v2.php/apps/notifications/api/v2/notifications HTTP/2.0", upstream: "fastcgi://127.0.0.1:9000", host: "example.com"
2022/12/15 20:41:01 [error] 31048#31048: *28606 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "PROPFIND /nextcloud/remote.php/dav/files/ben/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "example.com"
2022/12/15 20:41:31 [error] 31048#31048: *28460 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "PROPFIND /nextcloud/remote.php/dav/files/ben/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "example.com"
2022/12/15 20:41:34 [error] 31048#31048: *28795 directory index of "/var/www/" is forbidden, client: 192.168.0.116, server: example.com, request: "GET / HTTP/1.1", host: "192.168.0.202"
2022/12/15 20:41:34 [error] 31048#31048: *28798 directory index of "/var/www/" is forbidden, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET / HTTP/1.1", host: "example.com"

you’re not really running example.com, are you? :wink:
It should work if you delete/correct that line.
GOOD LUCK!

Thank you for the suggestion but I manually edited out the real domain name and replaced it with example.com for posting on this forum. In the real config file my real domain is present.

I think I have found the problem. In /etc/nginx/sites-available/nextcloud my configuration of the php handler I had a line for both a socket and a network server when I needed to use one or the other. I had the socket configured in php8.1-fpm but not the network location, so nginx kept trying to connect to php8.1-fpm over both the socket and the network and all the network requests failed because there was no configuration in php-fpm for the network. Commenting out the network server caused the errors to stop being produced in my /var/log/nginx/error.log.

upstream php-handler {
    server unix:/var/run/php/php8.1-fpm.sock;
    #server 127.0.0.1:9000;
}
1 Like