我正在尝试设置一个nginx容器,它将在"http://server_ip/“路径上显示nginx html页面,并在"/app”路径上显示tutum/hello-world容器。作为后续,我希望只能从"http://server_ip/app“路径而不是通过http://server_ip:1500到达"hello-world”容器。
我创建了以下docker-compose:
version: '3'
services:
proxy:
container_name: proxy
image: nginx
ports:
- "80:80"
volumes:
- $PWD/html:/usr/share/nginx/html
- $PWD/config/nginx.conf:/etc/nginx/conf.d/nginx.conf
networks:
- backend
webapp:
container_name: webapp
image: tutum/hello-world
ports:
- "1500:80"
networks:
- backend
networks:
backend:然后我有了下面的nginx.conf文件:
server {
listen 80; # not really needed, but more informative
location = / {
root /usr/share/nginx/html;
}
location = /app/ {
proxy_pass http://localhost:1500/;
}
}如果我尝试通过它们的http://server_ip:PORT到达每个容器,我就会到达那里。如果我尝试http://server_ip/app,我得到“404not found”。我遗漏了什么?我是不是把conf文件放错文件夹了?如何将"hello-world“的可用性限制在"http://server_ip/app”路径上,而不是通过"http://server_ip:1500"“。
发布于 2021-02-21 20:56:13
您的容器使用“后端”docker网络,正如您在撰写文件中所述。在其中,它们通过服务名称相互联系,所以从"proxy“服务可以访问http://webapp (或http://webapp:80)上的"webapp”服务,从"webapp“服务可以访问http://proxy或(http://proxy:80)上的"proxy”。
在您的计算机上,如果您键入http://localhost:1500/,您将访问webapp服务;如果您键入http://localhost:80/,您将访问代理服务。端口映射1500:80表示您的计算机1500端口映射到webapp容器80端口。
所以在nginx.conf中这样做:
proxy_pass http://webapp:80/;另外,如果您想使您的webapp不能从localhost:1500上的主机访问,请删除webapp服务规范中的端口部分:
version: '3'
services:
proxy:
container_name: proxy
image: nginx:1.11
ports:
- "80:80"
volumes:
- $PWD/html:/usr/share/nginx/html
- $PWD/nginx.conf:/etc/nginx/conf.d/default.conf
networks:
- backend
webapp:
container_name: webapp
image: tutum/hello-world
networks:
- backend
networks:
backend:https://stackoverflow.com/questions/66302375
复制相似问题