我是网络服务器领域的新手。我正在设置一个服务器(example.de),为两个不同的 Docker 化应用程序提供服务。暴露的端口3000(app1)和5000(app2)分别映射到服务器端口9000和9001。请求 url example.de:9000 和 example.de:9001,内容已正确加载。现在,我想使用 NGINX 容器作为反向代理来重定向 URL example.de/app1 和 example.de/app2。
“包括/etc/nginx/mime.types;”默认情况下在 nginx.conf 文件中设置。
默认的.conf是
server {
listen 80;
listen [::]:80;
server_name example.de;
location /app1{
proxy_pass http://app1:3000;
}
location /app2{
proxy_pass http://app2:5000;
}
}
工作的 docker-compose 文件如下所示:
version: '3'
services:
app1:
build:
context: app1
container_name: app1
restart: "<RESTART_POLICY>"
volumes:
- vol_app1:/data
app2:
build:
context: app2
container_name: app2
restart: "<RESTART_POLICY>"
volumes:
- vol_app2:/data
nginx-proxy:
container_name: nginx
build:
context: nginx/
ports:
- "80:80"
depends_on:
- app1
- app2
links:
- app1
- app2
volumes:
- vol_nginx:/etc/nginx/conf.d
volumes:
vol_app1:
driver_opts:
type: none
device: /vol/app1
o: bind
vol_app2:
driver_opts:
type: none
device: /vol/app2
o: bind
vol_nginx:
driver_opts:
type: none
device: /vol/nginx
o: bind
NGINX Dockerfile 是:
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
f 当我访问 url 时,重定向设置类似于 default.confhttp://example.de/app1 http://example.de/app1,浏览器请求/app1不适用于入口点/(xxx.x.xx.xx - - [2019年8月9日:06:31:57 +0000]“获取/app1HTTP/1.1" 404 77 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-")。
UPDATE
所有容器都在同一网络中。Docker 文档 https://docs.docker.com/compose/networking/:
By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
更新二
如果我不重定向,它会通过将上游块添加到 default.conf 来工作:
upstream app1{
server app1:3000;
}
upstream app2{
server app2:5000;
}
server {
listen 80;
listen [::]:80;
server_name example.de;
location / {
proxy_pass http://app1;
}
}
然而,带有 /app1 和 /app2 的路径不会重定向到 docker 容器。