How would I set up Nginx so that it uses SvelteKit (node-adapter) with SSL (Certbot)?

21 Views Asked by At

I have coded up a sveltekit website, but now I am trying to self-host it. I already have a domain that points to the raspberry pi's IP address, but I have so far only managed to get a http connection to work. Whenever I try to add SSL either through PorkBun or Cerfbot, it gives me a "too many redirects" error.

My not working /etc/nginx/sites-available/default file:

upstream sveltekit {
    server 127.0.0.1:3000;
    keepalive 8;
}

server {
    server_name domain.com;
    access_log  /var/log/nginx/domain.com.access.log;
    error_log  /var/log/nginx/domain.com.error.log;

    location / {
        try_files $uri $uri/ @sveltekit;
    }
        
    location @sveltekit {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Sendfile-Type X-Accel-Redirect;
        proxy_pass http://sveltekit;
        proxy_redirect off;

        error_page 502 = @static;
    }
    
    location @static {
        try_files $uri /index.html =502;
    }

    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}

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


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name domain.com;
    return 404; # managed by Certbot
}

Nginx code worked when I just used the answer from How to deploy a svelte kit app after build using nginx as web server, but stopped working the moment I tried to add SSL.

I have forwarded both ports 80 and 443 so that shouldn't be an issue. As of right now with the code above: using the domain gives a "too many redirects" error, typing the IP address directly gives nginx 404 not found, and typing the local IP address also gives nginx 404 not found. BTW, sorry if I ask specific/spoon-feeding questions. I am new to Nginx and SSL.

0

There are 0 best solutions below