Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly deploy/setup NestJS backend using Nginx?

I'm trying to deploy my webapplication using Angular and NestJs using Nginx on an Ubuntu remote server. I got the frontend working on https://ikse.fransenit.nl/products but cannot get the backend to work. It was working fine locally. When I try to go to /api/products I get a 502 bad gateway.

When starting the NestJS backend: nestjs starting

nginx config

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

    root /var/www/ikse/html;

    server_name ikse.fransenit.nl www.ikse.fransenit.nl;
    
    index index.html index.htm;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
                # proxy_pass http://localhost:8080;
                # proxy_http_version 1.1;
                # proxy_set_header Upgrade $http_upgrade;
                # proxy_set_header Connection 'upgrade';
                # proxy_set_header Host $host;
                # proxy_cache_bypass $http_upgrade;
        }

        location /api/ {
            proxy_pass https://localhost:3000;
        }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/ikse.fransenit.nl/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/ikse.fransenit.nl/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
}

What am I missing and/or doing wrong?

like image 938
Jessey Fransen Avatar asked Apr 27 '26 00:04

Jessey Fransen


1 Answers

I don't know if you resolved it, but I think you should add /api/ at the end of https://localhost:3000 in the proxy_pass option. Because with the current configuration you need to make https://doma.in.com/api/api/profile

like image 61
ruben_conmag Avatar answered Apr 28 '26 15:04

ruben_conmag