[英]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.我正在尝试将 Traefik 设置为一个简单的 Node.js 微服务的反向代理,由 Angular 前端调用,该微服务将被转换为 javascript、css 和 html 并在 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.
现在,我正在尝试让 Node.js 在端口 3000 上进行内部侦听,并且可以从外部 traefik 的公开端点上访问。
I can access the dashboard, and the whoami sample service, but not the endpopints defined in my Node.js microservice.我可以访问仪表板和 whoami 示例服务,但不能访问我的 Node.js 微服务中定义的端点。 I get "Bad Gateway" for endpoints that match the PathPrefix.
对于与 PathPrefix 匹配的端点,我得到“Bad Gateway”。 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.
如果我尝试访问与模式不匹配的端点,我会收到 404“找不到页面”,所以我认为我正确设置了 PathPrefix,我只是没有设置服务和/或后端与前端配合。
My microservice works just fine when called outside of Traefik.在 Traefik 之外调用时,我的微服务运行良好。 I have stripped out the handling for certificates and SSL/TLS from the Node.js script, so that Traefik can handle that part.
我已经从 Node.js 脚本中去除了对证书和 SSL/TLS 的处理,以便 Traefik 可以处理那部分。 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.如果您访问“/”,我的 Node.js 会打印“Hello, world”,如果您访问 /v1/api/authorize”,它会真正工作。同样,只有作为独立的 node.js 应用程序运行,而不是作为服务运行在 docker 服务堆栈中。
What am I missing?我错过了什么? What is causing the "Bad Gateway" errors?
是什么导致了“Bad Gateway”错误? I have a feeling this will be an easy fix, this seems like a straightforward use case for Traefik.
我有一种感觉,这将是一个简单的解决方法,这对于 Traefik 来说似乎是一个简单的用例。
I am using Node.js 10, and Traefik 2.1.0.我使用的是 Node.js 10 和 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 Path(
/
) 表示明确的 / 没有后缀,所以 /bla 不会匹配,即使 /v1/api 匹配第二条规则,也不会匹配
For multiple rules you can use this approach:对于多个规则,您可以使用这种方法:
- "traefik.http.routers.my-app.rule=Host(`subdomain.yourdomain.com`) && PathPrefix(`/v1/api`)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.