502 Bad Gateway after trying to setup www redirect to domain without www

If you’re looking for help, please provide information about your environment. If you delete this template and don’t provide any information, your topic will be automatically closed.

If you aren’t running the latest version of Ghost, the first thing we’ll ask you to do is update to the latest version of Ghost.

  • What’s your URL? Listed in the nginx files below

  • **What version of Ghost are you using?**1.24.2
    And

  • How was Ghost installed and configured? digital ocean ghost droplet configured to setup secondary domain and ssl, then configured for primary

  • What Node version, database, OS & browser are you using? node 16.20.1, mysql, ubuntu 22, safari

  • What errors or information do you see in the console? I don’t. ghost doctor did not return errors.

my files: /etc/nginx/sites-available/justmillit.com.conf

server {
    listen 80;
    listen [::]:80;

    server_name justmillit.com;
    root /var/www/ghost/system/nginx-root; # Used for acme.sh SSL verification (https://acme.sh)


    ssl_certificate /etc/letsencrypt/justmillit.com_ecc/fullchain.cer;
    ssl_certificate_key /etc/letsencrypt/justmillit.com_ecc/justmillit.com.key;
    include /etc/nginx/snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2369;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 50m;
}

/etc/nginx/sites-available/www.justmillit.com.conf

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name www.justmillit.com;
    return 301 https://justmillit.com$request_uri;

    ssl_certificate /etc/letsencrypt/www.justmillit.com/fullchain.cer;
    ssl_certificate_key /etc/letsencrypt/www.justmillit.com/www.justmillit.com.key;
    include /etc/nginx/snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2369;

    }

    location ~ /.well-known {
        allow all;
    }

    client_max_body_size 1g;
}
  • What steps could someone else take to reproduce the issue you’re having? I’m not sure! The site was working before I attempted to get the secondary domain setup, and I don’t know why my change isn’t working right. I changed the files so www.justmillit.com should redirect to justmillit.com, and added links to each domain’s SSL files from let’s encrypt.

Welcome to the Ghost community, @melaniecarr23.

A couple of observations.

  1. Ghost will only work with one URL, i.e., WWW or non-WWW. It looks like the installer created the first config, but not the second. Did you create this file?

  2. The gateway error implies that the non-WWW site is not set up in Nginx. Did you create a symbolic link to sites-enabled? Nonetheless, don’t do this until you have a working site.

  3. Non-HTTP isn’t redirecting to HTTPS.

Please confirm your preferred URL, and "url": from config.json.production, and your choice when installing Ghost.

Finally, did you use the 1-click installer? This indicated that SSL isn’t correctly configured.

I did the 1 click installation, and originally setup www. Problem is, most folks will enter the domain without www, so I was trying to setup SSL for both and have one redirect to the other.

from the json file: “url”: “https://justmillit.com

Yes, I setup the symlink for both www and non-www in nginx.

I don’t get it.

Currently, neither WWW nor non-WWW appear to work. I suggest you revert your changes, so the site works, and then go through the necessary changes.

Please note:

  • Ghost can only have one domain pointed at it, which is defined in config.production.json, and the SSL certificate is created for this.
  • You can add server blocks to the top of the working Nginx config to redirect non-HTTPS to HTTPS, and WWW to non-WWW, e.g.:
server {
    listen 80;
    listen 443 ssl;
    server_name www.justmillit.com;
    return 301 $scheme:justmillit.com$request_uri;
}

Change the Ghost config:

# Change your config
ghost config url https://my-canonical-domain.com
# Generate an SSL setup for the domain
ghost setup nginx ssl

# Verify your Nginx config
sudo nginx -t
# Reload nginx with the new config
sudo nginx -s reload