Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwarding port to Node.js app with Nginx and routing

I run my node app on localhost:3000 and it is serving a default page for the route /. If I access http://localhost:3000 the default page is displayed accordingly. I have also running a Nginx server that is basically configured as followed:

server {
    listen       80;
    server_name  localhost;

    location /node_app {
        proxy_pass  http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

If I run http://localhost/node_app now, my node app throws an error saying that it cannot find route /node_app.

How can I configure either my node app or the nginx server in a way that I can access the app by calling http://localhost/node_app, yet the app itself thinks it is at /?

Update

If I add a / to http://127.0.0.1:3000 it is actually matching /node_app to the / route. But now every stylesheet for instance within the default page is now pointing to the wrong path.

like image 697
Amberlamps Avatar asked Jan 11 '23 19:01

Amberlamps


1 Answers

After experimenting a bit around I finally got the configuration right to work exactly how I wanted the server to work:

server {
    location /node_app/ {
        proxy_pass  http://localhost:3000/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Lesson learned: Remember the slashes!

like image 125
Amberlamps Avatar answered Jan 17 '23 16:01

Amberlamps