简体   繁体   中英

How to use Traefik to reverse proxy an already running node service?

I've got a node process running on port 3000 using pm2. I want to configure Traefik so that it reverse proxies this service on port 80.

Following this excellent blog post, I was able to quickly start Traefik using docker compose and set up a skeleton config for the node-server.

However, that example assumes the node process is hosted inside a docker as well. I couldn't get this to work for my node process (*) so I just want to be able to configure Traefik by pointing to port 3000 in some way. Seems straightforward but couldn't get it to work.

I'm stuck with the following config (which is a mix of various blog-posts without actually knowing what I'm doing):

services:
  reverse-proxy:
    image: traefik:v2.4
    container_name: "traefik"
    command:
      - "--api.insecure=true"
      - "--api.dashboard=true"
      - "--api.debug=true"
      - "--providers.docker=true"
      - "--log.LEVEL=DEBUG"
      - "--entryPoints.web.address=:80"
      - "--entryPoints.websecure.address=:443"
      - "--providers.docker.exposedbydefault=false"
      - "--certificatesresolvers.myresolver.acme.httpchallenge=true"
      - "--certificatesresolvers.myresolver.acme.httpchallenge.entrypoint=web"
      - "--certificatesresolvers.myresolver.acme.email=xxxx@xxx.com"
      - "--certificatesresolvers.myresolver.acme.storage=/letsencrypt/acme.json"
    ports:
      - "443:443"
      - "80:80"
      - "8080:8080"
    volumes:
      - "./letsencrypt:/letsencrypt"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"

  node-server:
    loadBalancer:
      servers:
        - url: http://127.0.0.1:3000/
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.node-server.rule=Host(`xxxxxx.com`)"
      - "traefik.http.routers.node-server.entrypoints=websecure"
      - "traefik.http.routers.node-server.tls.certresolver=myresolver"
      - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https"
      - "traefik.http.routers.redirs.rule=hostregexp(`{host:.+}`)"
      - "traefik.http.routers.redirs.entrypoints=web"
      - "traefik.http.routers.redirs.middlewares=redirect-to-https"

This gives the error: 'Unsupported config option for services.node-server: 'loadBalancer'"

Long story short: how would I configure Traefik to just reverse proxy a service running on port 3000?

*) A total newbie to Docker and I couldn't get the situation to work, where the node process depends on custom javascript modules in a parent directory. Perhaps there's a way to do this and I could do it in the 'host node in docker' way instead. I'm all ears

A few months ago I have configured a reverse proxy, here you go my configuration:

version: '3'

services:
  reverse-proxy:
    image: traefik:v2.5
    container_name: selling-point-reverse-proxy
    ports:
      - 80:80
      - 8080:8080
    volumes:
      # Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
    command:
      # Enables the web UI
      - --api.insecure=true
      # Tells Traefik to listen to docker
      - --providers.docker
      # Creates a new entrypoint called web
      - --entrypoints.web.address=:80
      # Disable container exposition
      - --providers.docker.exposedByDefault=false
      # Traefik matches against the container's labels to determine whether to create any route for that container
      - --providers.docker.constraints=Label(`traefik.scope`,`selling-point`)
    networks:
      - selling-point
  api:
    image: selling-point-api
    container_name: selling-point-api
    build: 
      context: ./selling-point-api
    labels:
      # Tells Traefik where to redirect the request if the url has the specified prefix
      - traefik.http.routers.api.rule=PathPrefix(`/api`)
      # Attaches a middleware for forwarding the authentication
      - traefik.http.routers.api.middlewares=forward-auth,latency-check
      # Attaches entrypoints
      - traefik.http.routers.api.entrypoints=web
      # Exposes container
      - traefik.enable=true
      # Matcher for creating a route
      - traefik.scope=selling-point
      # Creates a service called selling-point-api
      - traefik.http.services.selling-point-api.loadbalancer.server.port=3000
      # Attach the container to a service
      - traefik.http.routers.api.service=selling-point-api
      # Creates circuit breaker middleware
      - traefik.http.middlewares.latency-check.circuitbreaker.expression=LatencyAtQuantileMS(50.0) > 100
    volumes:
      - ./selling-point-api/src:/app/src
    networks:
      - selling-point
    environment:
      WAIT_HOSTS: mysql:3306
      DATABASE_URL: mysql://root:huachinango@mysql:3306/selling_point
      NODE_ENV: development
  auth:
    image: selling-point-auth
    container_name: selling-point-auth
    build: 
      context: ./selling-point-auth
    labels:
      # Tells Traefik where to redirect the request if the url has the specified prefix
      - traefik.http.routers.auth.rule=PathPrefix(`/auth`)
      # Creates a forward auth middleware
      - traefik.http.middlewares.forward-auth.forwardauth.address=http://auth:3000/auth/authorize
      # Attaches entrypoints
      - traefik.http.routers.auth.entrypoints=web
      # Exposes container
      - traefik.enable=true
      # Matcher for creating a route
      - traefik.scope=selling-point
      # Creates a service called selling-point-auth
      - traefik.http.services.selling-point-auth.loadbalancer.server.port=3000
      # Attach the container to a service
      - traefik.http.routers.auth.service=selling-point-auth
      # Attaches a circuit breaker middleware
      - traefik.http.routers.auth.middlewares=latency-check
    environment:
      WAIT_HOSTS: mysql:3306
      IGNORE_ENV_FILE: 'true'
      DATABASE_URL: mysql://root:huachinango@mysql:3306/selling_point
      PASSWORD_SALT: $$2b$$10$$g0OI8KtIE3j6OQqt1ZUDte
      NODE_ENV: development
    volumes:
      - ./selling-point-auth/src:/app/src
    networks:
      - selling-point
  mysql:
    image: mysql:5
    environment:
      MYSQL_ROOT_PASSWORD: huachinango
      MYSQL_DATABASE: selling_point
    networks:
      - selling-point
    volumes:
      - mysql-db:/var/lib/mysql

volumes:
  mysql-db:

networks:
  selling-point:
    name: selling-point
    driver: bridge

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM