how to do Traefik routing using URL prefix (ike Nginx)

435 Views Asked by At

There is a simple Node-Express application. running on 8080 port in docker

var express = require('express');

var app = express();

app.get("/", function(req, res) {
    res.send(`
    <h1> Application written in  Node js</h1>
    <h3> Home page </h3>
    `)
});

app.get("/info", function(req, res) {
    res.send(`
    <h1> Application written in  Node js</h1>
    <h3> Info page </h3>
    `)
});

app.get("/api", function(req, res) {
  res.send({success: 1})
});

const PORT = process.env.NODE_DOCKER_PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}.`);
});

I made this Traefik config. I want to redirect any incoming request /app to my nodejs docker.

example:

  1. localhost/node should go to my express route /
  2. localhost/node/info should go to my express route /info
  3. localhost/node/api should go to my express route /api
version: '3.1'

services:
  nodeapp:
    build: ./node_app
    container_name: nodeapp
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.node-server.rule=Host(`localhost`) && PathPrefix(`/app`)"
      - "traefik.http.services.node-server.loadbalancer.server.port=8080"
    networks:
      - proxy

  reverse-proxy:
    image: traefik:v2.10
    command: --api.insecure=true --providers.docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - proxy


networks:
  proxy:

I tried Traefik middleware like these:stripprefix.prefixes and stripprefixregex.regex, but still did not find any solution.

Please help. thanks in advance

1

There are 1 best solutions below

1
On

services:
  nodeapp:
    build: ./nodeapp
    containername: nodeapp
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.node-server.rule=Host(localhost) && PathPrefix(/app)"
      - "traefik.http.routers.node-server.entrypoints=web"
      - "traefik.http.routers.node-server.service=node-service"
      - "traefik.http.services.node-service.loadbalancer.server.port=8080"
      - "traefik.http.middlewares.stripprefix.stripprefix.prefixes=/app"
      - "traefik.http.routers.node-server.middlewares=stripprefix"
    networks:
      - proxy

  reverse-proxy:
    image: traefik:v2.10
    command: --api.insecure=true --providers.docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
    networks:
      - proxy

networks:
  proxy:

Explanation of changes:

  1. Added entrypoints=web to the router definition. This specifies that requests will be received on the web entrypoint.
  2. Added service configuration for node-server using node-service as the service name.
  3. Added middleware configuration for stripprefix to remove the /app prefix from the request before forwarding to the backend service.
  4. Included middlewares=stripprefix to apply the stripprefix middleware to the router.

With this configuration, requests made to localhost/app will be redirected to your Node.js application at port 8080, and the /app prefix will be stripped before forwarding to your Node.js routes. For example:

  • localhost/app will be redirected to your Node.js route /
  • localhost/app/info will be redirected to your Node.js route /info
  • localhost/app/api will be redirected to your Node.js route /api