问题
I am new in web-servers. I am setting up a server (example.de) serving two different dockerized applications. The exposed ports 3000 (app1) and 5000 (app2) are mapped in server ports 9000 and 9001 respectively. Requesting the urls example.de:9000 and example.de:9001, the content is properly load. Now, I want to use a NGINX container as reverse proxy to redirect the urls example.de/app1, and example.de/app2.
"include /etc/nginx/mime.types;" is set by default in the nginx.conf file.
The default.conf is
server {
listen 80;
listen [::]:80;
server_name example.de;
location /app1{
proxy_pass http://app1:3000;
}
location /app2{
proxy_pass http://app2:5000;
}
}
The working docker-compose file looks like:
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
The NGINX Dockerfile is:
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
f the redirection is set like the default.conf, when I access the url http://example.de/app1, the browser request for /app1 not for the entrypoint / (xxx.x.xx.xx - - [09/Aug/2019:06:31:57 +0000] "GET /app1 HTTP/1.1" 404 77 "-" "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Firefox/68.0" "-").
UPDATE
All containers are in the same network.
Docker documentation:
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.
UPDATE II
If I do not redirect, it works by adding upstream blocks to the 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;
}
}
Nevertheless, the path with /app1 and /app2 does not redirect to the docker container.
来源:https://stackoverflow.com/questions/57389327/nginx-container-routing-nginx-does-not-redirect-to-docker-containers