Home > database >  How to setup subpath proxy in nginx
How to setup subpath proxy in nginx

Time:03-17

My upstream server running on PORT - 3000

var express = require('express')
var app = express()

app.get('/', function (req, res) {
  res.send('Hello World!')
})

app.get('/test',function(req,res){
  res.send('<h1>Testing...</h1>')
})

app.listen(3000, function () {
  console.log('Listening on port 3000...')
})

I can easily see both routes in the browser

enter image description here

enter image description here

Setting up the proxy from ngnix -> Node

I tried the following configurations

location ^~ /* {
    try_files $uri $uri/ =404;
    proxy_pass http://localhost:3000/$1;
}

========================================
location / {
    try_files $uri $uri/ =404;
    proxy_pass http://localhost:3000/;
}

========================================
location /* {
    try_files $uri $uri/ =404;
    proxy_pass http://localhost:3000/$1;
}

All the above configuration is loading http://localhost/ but 404 on http://localhost/test

CodePudding user response:

try_filesserves static files. Remove this line.

location / {
    proxy_pass http://localhost:3000/;
}
  • Related