Caddy used for reverse proxying to node.js instances?

I’ve currently got a node.js server that I call mainServer.js. It’s currently serving a bunch of my domains ad handling the routes for those based on the domain name in the request header.

I’d like to delegate serving of my sites to each individual site and have a main server just proxy the requests based on the domain name. So I’d have say server1 listening on port 8001 and caddy would forward server1.com to that port.

I’m wondering how would I implement https in this case? Delegate it to caddy and it will worry about that for all my domains? Or have each node.js server handle their own cert?

Another thing is these servers (server1, server2) will be serving files for a website. Do I need to do anything extra in a caddyfile than just :

server1.com {
reverse-proxy localhost:8001
}

server2.com {
reverse-proxy localhost:8002
}

Terminating TLS at Caddy is a very common approach, it’s the easiest to set up.

The only thing wrong with your config is that the directive is reverse_proxy (underscores) not reverse-proxy.

Thanks! Ok I’ve got that working. I removed my cert code from the node.js server app and let Caddy do it but now there’s a new problem…

In the node server, I’m serving static files as well as sending an index.html file. The index.html file seems to work and loads fine but the static files aren’t loaded and the page doesn’t display. I don’t want to be specifying the path to the static files in my caddy file. I’d like to keep caddy as a proxy server that routes based on domain name to a specific local port and my node app should be the one serving the files.

To give more info my node server looks like this:

app.use(
  '/static',
  express.static('/var/www/server1/build')
)

app.get('/*', (req, res) => {
res.sendFile(
  '/var/www/server1/build/index.html'
})

app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`)
})

This is a react app so it loads index.html and then index.html loads a bunch of chunks of .cc and .js files. The folder structure for the build folder looks like this:

build -

  • manifest.json
  • indes.html
  • static
    • js
    • css
    • media

My caddy file looks like this:

mydomain.com {
reverse_proxy localhost:8001
}

When I load up mydomain.com in a browser, it loads the index.html file properly but then for some reason, the contents of the manifest.json file in the dev tools is the same contents as my index.html file. My .css files also have the same content as the index.html file.

It’s as if the static directory in node is not being served and every request to a file is just being sent the contents of index.html again.

Is there something I’m missing?

Ok I think I figured it out…

It was an express problem…

I was doing app.get(’/*’) which was just sending the index.html file for all requests. I removed the * and it worked.