So I'm trying to setup traefik so I can have multiple apps as routes from one domain.
IE. domain.com/app1, /app2, etc
With this config
version: "3.7"
services:
myapp:
container_name: "myapp-frontend"
image: linux-node-build
deploy:
labels:
- traefik.enable=true
- traefik.http.services.myapp-service.loadbalancer.server.port=8080
- traefik.http.routers.myapp.entrypoints=web
- traefik.http.routers.myapp.rule=PathPrefix(`/app1`)
- traefik.http.routers.myapp.middlewares=redirect@file
# http to https redirect working fine
- traefik.http.routers.myapp-tls.rule=PathPrefix(`/app1`)
- traefik.http.routers.myapp-tls.entrypoints=websecure
- traefik.http.routers.myapp-tls.tls=true
networks:
- traefik-public
networks:
traefik-public:
external: true
And express app
const express = require('express');
const cors = require('cors');
const path = require('path');
const bodyParser = require('body-parser');
const PORT = process.env.PORT || 8080;
const HOST = '0.0.0.0';
const app = express();
app.use(cors());
app.options('*', cors());
app.use(bodyParser.json()); // handle json data
app.use(bodyParser.urlencoded({ extended: true })); // handle URL-encoded data
app.get('/health', (req, res) => { res.status(200).send('Healthy!') });
app.use("/static", express.static(path.join(__dirname, 'build/static'), { maxAge: '30m' }));
app.use("/", express.static(path.join(__dirname, 'build'), { maxAge: '30m' }));
app.get('/*', (req, res) => {
res.set('Cache-Control', 'public, max-age=1800'); // 30m
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
My app is running and if I hit the PathPrefix of /app1 I get a page that tries to load resources like css/js
The problem is that the resources are just loaded as html so I get a white page.
I've tried stripping the path prefix and adding a homepage in package.json but neither worked.
Oddly this works perfectly fine if instead of doing a PathPrefix I use Host(app.localhost
) and remove the homepage in package.json. My react app renders with all the css/js/img assets.