How to map api nodes with nginx location?

Viewed 121

I am trying to run node app on nginx.

Here is my nginx configuration.

location /first {
proxy_pass http://localhost:3000;
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;
}

And here is node app router.

app.use('/', indexRouter);
app.use('/api', apiRouter);

But when I access server via x.x.x.x/first I get 404 errors. What is the reason. Please let me know if you know.

1 Answers

This is likely because of your locations configuration on the NGINX side.

So based on what I guess you want to do:

/first/ -> Node Index Route
/first/api -> Node API Route

This would result in the following configuration

server {

  ....


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

}
Related