简体   繁体   中英

Trying to set up Traefik as a reverse proxy for node.js and nginx

I am trying to set up Traefik as a reverse proxy for a simple Node.js microservice to be called by an Angular front-end which will be transpiled down to javascript, css, and html and run on nginx. Right now, I am trying to get the Node.js to listen internally on port 3000, and be reachable on exposed endppoints from outside traefik.

I can access the dashboard, and the whoami sample service, but not the endpopints defined in my Node.js microservice. I get "Bad Gateway" for endpoints that match the PathPrefix. If I try to get to an endpoint that does not match the pattern, I get a 404 "Page not found", so I think I have the PathPrefix set up correctly, I just don't have the services setup and/or the backend mated up with a front end.

My microservice works just fine when called outside of Traefik. I have stripped out the handling for certificates and SSL/TLS from the Node.js script, so that Traefik can handle that part. I am pretty confident that part is working too.

My Node.js prints "Hello, world" if you access "/", and does real work if you got to /v1/api/authorize". Again, only if run as a standalone node.js app, not as a service in the docker service stack.

What am I missing? What is causing the "Bad Gateway" errors? I have a feeling this will be an easy fix, this seems like a straightforward use case for Traefik.

I am using Node.js 10, and Traefik 2.1.0.

version: "3.3"

services:
  reverse-proxy:
    image: "traefik:latest"
    command:
      - --entrypoints.web.address=:80
      - --entrypoints.web-secure.address=:443
      - --providers.docker=true
      - --api.insecure
      - --providers.file.directory=/configuration/
      - --providers.file.watch=true
      - --log.level=DEBUG
    ports:
      - "80:80"
      - "8080:8080"
      - "443:443"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "/home/cliffm/dev/traefik/configuration/:/configuration/"

  auth-micro-svc:
    image: "auth-micro-svc:latest"
    labels:
      - traefik.port=3000
      - "traefik.http.routers.my-app.rule=Path(`/`)"
      - "traefik.http.routers.my-app.rule=PathPrefix(`/v1/api/`)"
      - "traefik.http.routers.my-app.tls=true"

  whoami:
    image: containous/whoami:latest

  redis:
    image: redis:latest
    ports:
      - "6379:6379"

Late to the party, but at the very least, you are overriding configuration here:

- "traefik.http.routers.my-app.rule=Path(`/`)"
- "traefik.http.routers.my-app.rule=PathPrefix(`/v1/api/`)"

Second line overrides bottom line, so the first line is essentially ignored. Even if it it would work with both rules, they are exclusive. Path( / ) means explicitly / without suffixes, so /bla would not match, neither will /v1/api even though it matches the second rules

For multiple rules you can use this approach:

- "traefik.http.routers.my-app.rule=Host(`subdomain.yourdomain.com`) && PathPrefix(`/v1/api`)"

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