我在我的VM(地址x.y.z.w
)的端口80上使用Nginx docker镜像。因此,当我在浏览器中尝试http://x.y.z.w
时,它向我显示了Nginx索引文件。
现在,我为在端口8081和8082上的同一VM上运行的两个docker镜像配置(基本上是代理传递)。我想要什么:
when I type http://x.y.z.w/a it should go to http://x.y.z.w:8081
when I type http://x.y.z.w/b it should go to http://x.y.z.w:8082
为此,我更改了conf文件中的一部分:
location /a {
rewrite ^/a(.*) /$1 break;
proxy_pass http://x.y.z.w:8081 ;
}
location /b {
rewrite ^/b(.*) /$1 break;
proxy_pass http://x.y.z.w:8082 ;
}
它按预期工作。但是由于所有图像都在同一台机器上(具有相同的IP),所以我想使用
localhost
而不是x.y.z.w
。但是它不适用于localhost
。基本上,我不想在链接中使用硬编码IP(x.y.z.w),因为IP将来可能会更改。
有什么办法,Nginx可以知道运行它的变量IP,我可以使用该IP。或者如何通过一些修改将其与
localhost
一起使用?
最佳答案
您不能使用localhost
寻址另一个Docker容器,因为它引用了容器而不是主机的loopback
。如@Tuan所建议,您可以link
(https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/)容器,以便它们可以通过其名称进行通信。例如。:
docker run --rm -ti --name backend nginx:latest
docker run --rm -ti --name proxy --link backend:backend nginx:latest
现在
proxy
知道了backend
。
关于nginx - 配置Nginx进行路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462948/