我有两个Docker容器:
1- Api
2-授权
从授权中,如果用户请求通过了身份验证,我希望将其重定向到我的if ( itsAuthenticated){
req.redirect('http://api:8080/users/something')
// req is the normall express request object
}
。
所以在我的身份验证容器中,我有:
http://api:8080
8080
是指我的另一个容器,它暴露了docker-compose
,并且已将其链接到我的 api:
build: ./api
ports:
- "8080:8080"
links:
- mongo // api needs mongo and it's working fine
volumes:
- ./api:/code
authorization:
build: ./authorization
ports:
- "8088:8088"
volumes:
- ./authorization:/code
links:
- api // here I have a link to api container
...
文件中,如下所示:
authorization
注意:
如果我从API
到我的if ( itsAuthenticated){
const result = await http.get('http://api:8080/users/something');
console.log('**** result',result);
}
发出HTTP请求,则该请求有效。
Bellow工作正常:
get
因此,基本上,连接正常,Docker
请求正常,重定向在redirect
外部完全正常,但是在容器内部时,http://www.google.com
不起作用。
感谢帮助。
更新
刚刚意识到,如果我将请求重定向到http://api:8080/users/something
,我会得到一个结果,但是当我将其重定向到{{1}}时,它会立即消失
我确定我在某处缺少一些愚蠢的东西:)
答案 0 :(得分:0)
发送重定向呼叫时,将发生以下三个步骤:
http://localhost:8088/
并发送登录请求。http://api:8080
。” http://api:8080
联系。由于重定向确实会在浏览器中生效,因此您必须发送回浏览器可以理解的URL,这意味着它不能使用Docker内部DNS名称。
简单的,面向开发的答案是使用http://localhost:8080
作为重定向地址。由于您使用的是Express,因此类似
req.redirect(`http://${req.hostname}:8080/users/something`)
可能会工作(使用Request.hostname),尽管这会迫使您的应用程序了解其他服务的发布端口。