nginx-proxy not forwarded to docker-container?
I maintain a system consisting of two docker-containers, frontend and backend. The frontend-container hosts an nginx-webserver with a flutter web app while the backend provides a REST-interface that is used by the frontend. Both, frontend and backend are separate projects and both work fine independently. Both are within the same docker-network (“my-network”) that I create externally.
docker network create my-network
Here is the docker-compose.yml of the backend:
version: '3'
services:
my-backend:
image: my-backend
ports:
- "8080:8080"
networks:
- my-network
networks:
my-network:
driver: bridge
external: true
And here is the one of the frontend:
version: '3'
services:
my-frontend:
image: my-frontend
ports:
- "4200:80"
networks:
- my-network
networks:
my-network:
driver: bridge
external: true
Everything works fine, I can eg use curl http://my-backend:8080/somepath
from within the my-frontend-container.
My nginx.conf is as follows:
events {}
http {
include /etc/nginx/conf/mime.types;
server {
listen 80 default_server;
root '/usr/share/nginx/html';
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location /backend {
proxy_pass http://my-backend:8080;
}
location /example {
proxy_pass https://example.com;
}
}
}
Like I said, from the frontend’s container I can resolve my-backend:8080
using curl. I also can access the frontend from a browser (using http://localhost:4200) and also the example-forward (http://localhost:4200/example), yet, http://localhost:4200/backend does not reach its destination (while http://localhost:8080 works perfectly fine). After some timeout nginx returns a 404. Could someone please explain to me what is the issue?
Docker-images that I use are latest nginx and latest openjdk.
EDIT:
I changed the nginx.conf file slightly by adding
location /backend {
resolver 127.0.0.11; # added this line also to http and server-section
set $backend http://ausanz-backend:8080;
proxy_pass $backend;
}
Furthermore I use nginx-debug. In the debug-output I see that the IP-address is resolved correctly (some parts of the debug-output):
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http request line: "GET /backend/deployment-info HTTP/1.1"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http uri: "/backend/deployment-info"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 test location: "backend"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 using configuration "/backend"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 rewrite phase: 3
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script value: "http://my-backend:8080"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script set $backend
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script var: "http://my-backend:8080"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script copy: "Host"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script var: "my-backend:8080"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script copy: "Connection"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http script copy: "close"
my-frontend_1 | "GET /backend/deployment-info HTTP/1.0
my-frontend_1 | Host: my-backend:8080
my-frontend_1 | Connection: close
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http upstream resolve: "/backend/deployment-info?"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 name was resolved to 172.19.0.2
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 connect to 172.19.0.2:8080, fd:13 #7
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http upstream connect: -2
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http upstream request: "/backend/deployment-info?"
my-frontend_1 | 2022/11/08 12:56:02 [debug] 30#30: *5 http proxy status 404 "404 Not Found"
I verified that curl 172.19.0.2:8080/deployment-info
is in fact my-backend.