Can't access nextcloud from the outside - error 502

Hi Guys,

Background: UNRAID, MariaDB, NextCloud, Letsencrypt.

I have installed all the apps and they’re all working (I can access NC from the outside prior to installing Letsencrypt) but after installing Letsencrypt I’m unable to access from the outside, Cloudflare is giving error 502 (bad gateway). We have our own domain and I have an ‘A’ record pointing to our external IP which is mapped/natted into my NC server and I can also telnet to port 443 (my NC port). Letsencrypt was able to verify identity and cert was good. These are the NC config.php and NC subdomain proxy-conf. Any help is appreciated.

config.php (some info are change)
$CONFIG = array (
‘memcache.local’ => ‘\OC\Memcache\APCu’,
‘datadirectory’ => ‘/data’,
‘instanceid’ => ‘ocr0kaov7upo’,
‘passwordsalt’ => ‘yayayayayayayaya’,
‘secret’ => ‘yayayayayayayaya’,
‘trusted_domains’ =>
array (
0 => ‘’,
1 => ‘’,
‘overwrite.cli.url’ => ‘’,
‘overwritehost’ => ‘’,
‘overwriteprotocol’ => ‘https’,
‘dbtype’ => ‘mysql’,
‘version’ => ‘’,
‘dbname’ => ‘nextcloud’,
‘dbhost’ => ‘’,
‘dbport’ => ‘’,
‘dbtableprefix’ => ‘oc_’,
‘dbuser’ => ‘user’,
‘dbpassword’ => ‘password’,
‘installed’ => true,
‘mail_smtpmode’ => ‘smtp’,
‘mail_smtpauthtype’ => ‘LOGIN’,
‘mail_from_address’ => ‘nextcloud’,
‘mail_domain’ => ‘’,
‘mail_smtphost’ => ‘’,
‘mail_smtpport’ => ‘25’,

letsencrypt nextcloud.subdomain.config

server {
    listen 443 ssl;

    server_name nextcloud.*;

    include /config/nginx/ssl.conf;

    client_max_body_size 0;

    location / {
        include /config/nginx/proxy.conf;
        resolver valid=30s;
        set $upstream_nextcloud nextcloud;
        proxy_max_temp_file_size 2048m;
        proxy_pass https://$upstream_nextcloud:443;


I’m able to log in now but come across another issue. We’re using cloudflare and if I only enable DNS I can log in to NC but the certificate is not recognized and if I enable DNS + http proxy, the certificate is installed but I’m getting Error 502 Bad gateway (I can telnet to port 443). Thanks