1

Angular always returns the index.html file content as a response (instead of JSON data) when calling NestJS (NodeJS) APIs with an Nginx reverse proxy when I access to my webapp from a public IP x.x.x.x but when I access it from localhost (same machine where Nginx is installed), everything works well.

Here is my nginx.conf content:

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

    server_name _;
    root   C:/Project/front;

    location / {
        index index.html;
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        rewrite ^/api/(.*)$ /$1 break;
        proxy_pass http://localhost:3001/;
    }
    
}

Appreciate your help. Thanks !

3
  • Btw I ran nodejs with 'node main.js --host=0.0.0.0'
    – MHDaouas
    Commented Apr 3, 2023 at 22:44
  • Hi there! Just to have a little bit of context...Is NestJS API exposed on /api path? If that's not the case...did you try removing the rewrite nginx rule? For example...just using: location /api { proxy_pass http:// localhost:3001} (blank space must be removed)
    – lmoglia
    Commented Apr 4, 2023 at 0:21
  • Yes NestJS API is exposed behind /api path. My problem is that it seems like it's interpreted as an Angular page instead of an API.
    – MHDaouas
    Commented Apr 4, 2023 at 6:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.