Search code examples
nginxsslgunicornlets-encryptcertbot

Nginx loading without gunicorn when theres no www in the domain name


I get a 404 Not Found nginx/1.18.0 (Ubuntu) error when I look up my domain without having www in the domain name I searched. It finds nginx so it obviously finds my server but I can't find someone not being able to load gunicorn just without www. My name servers all points to the write ip address the ones with my domain name on the left are where I've either left it blank or put @ in the field. I've gone through the guides I've used to make sure I've done every step is there some config I might've messed up?

My error logs in /var/log/nginx don't show anything relevant and neither do my logs at the /var/log/webapp so I can't figure out why gunicorn wouldn't bee loading without www

I found it might have to do with my config file in nginx itself so here is my /etc/nginx/sties-enabled/webapp

server {
        server_name www.websitename.com;

        location /static {
                alias /home/sai/webapp/website/static;
        }

        location / {
                proxy_pass http://localhost:8000;
                include /etc/nginx/proxy_params;
                proxy_redirect off;
        }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.websitename.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.websitename.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.websitename.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        server_name www.websitename.com;
    return 404; # managed by Certbot


}

I've found that my error might be that certbot is only redirecting www.websitename.com to https so I'm gonna try and figure out how to add websitename.com to that because just adding another if under makes my nginx not be able to restart


Solution

  • Make this your conf then run sudo certbot --nginx -d websitename.com -d www.websitename.com and it should create ssl certs for both your domains and work

    server {
            server_name websitename.com www.websitename.com;
    
            location /static {
                    alias /home/sai/webapp/website/static;
            }
    
            location / {
                    proxy_pass http://localhost:8000;
                    include /etc/nginx/proxy_params;
                    proxy_redirect off;
            }
    
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/www.websitename.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/www.websitename.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    
    }```