I’m developing a web server that uses nodejs react for the frontend and java spring boot in the backend.
I’m creating a container for my application with docker and i’m using nginx to do reverse-proxy between the frontend and the backend. The problem is that, when i try to communicate from the frontend with the backend i have the error 502 Bad Gateway and in the nginx server console:
connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: , request: "POST /api/authenticate HTTP/1.1", upstream: "http://172.20.0.3:9093/authenticate", host: "localhost:3050", referrer: "http://localhost:3050/"
This is my default.conf:
upstream backend{
server b-applicationgateway:9093;
}
server {
listen 80;
location /api/ {
proxy_pass http://backend/;
}
location / {
proxy_pass http://f-frontend:3000;
}
}
These are the nginx and frontend dockerfiles:
nginx
FROM nginx
COPY ./default.conf /etc/nginx/conf.d/default.conf
frontend
FROM node:14.14.0-alpine
WORKDIR /app
COPY ./package.json ./
RUN npm i
COPY . .
CMD ["npm", "run", "start"]
Finally, this is the docker-compose.yaml file:
version: '3.5'
services:
f-frontend:
stdin_open: true
environment:
- CHOKIDAR_USEPOLLING=true
build:
dockerfile: Dockerfile.dev
context: ./Frontend/my-app
networks:
n-frontend:
aliases:
- f-frontend
ports:
- "3000:3000"
b-applicationgateway:
build:
context: Backend/
dockerfile: applicationGateway.containerfile
hostname: b-applicationgateway
image: b-applicationgateway:latest
ports:
- "9093:9093"
networks:
backend:
int-net:
n-frontend:
aliases:
- b-applicationgateway
depends_on:
- b-datamanager
- b-authenticationmanager
- b-assetmanager
nginx:
depends_on:
- b-applicationgateway
- f-frontend
restart: always
build:
context: ./nginx
dockerfile: Dockerfile.dev
ports:
- "3050:80"
networks:
n-frontend:
aliases:
- nginx
networks:
backend:
n-frontend:
external: true
name: n-frontend
int-net:
external: true
name: int-net
volumes:
data:
I intentionally deleted some other backend services because i think they are useless for this question, since if i run only the backend part everything works.
The gateway controller (in the backend) has these annotations:
@RestController
@RequiredArgsConstructor
@Slf4j
@CrossOrigin(origins = {"http://localhost:3000", "http://localhost:3050"}, allowCredentials = "true")
I checked the docker network n-frontend with the command ‘docker network inspect n-frontend’ , and all the containers are present
"Containers": {
"3343cfd269f670df0c9d8abfaa102cf0662d4a10cb7fb8eeb92b71f98917df2d": {
"Name": "webplatform-f-frontend-1",
"EndpointID": "ca720661c9b5fd6b72d7c95c5ae9f81ccb8712c124407f69a5564050bc5ceba2",
"MacAddress": "02:42:ac:14:00:02",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
},
"548a82a6daf4e6cb9645330c2e5c210cf0071b937e4bdf2668818d4a60218462": {
"Name": "webplatform-b-applicationgateway-1",
"EndpointID": "9f6efba31f2055969d10f5badebf1ec9386f1743fe0dbf703b013139edacd122",
"MacAddress": "02:42:ac:14:00:03",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
},
"b3dd4141e266277b440320c36685dcdfce9a8ab17c899b06b90ba59e102d1354": {
"Name": "webplatform-nginx-1",
"EndpointID": "e0f616f2d75c829956be1a61814cff6db23c7ecd3786a0143e4f90747f68750a",
"MacAddress": "02:42:ac:14:00:04",
"IPv4Address": "172.20.0.4/16",
"IPv6Address": ""
}
},
Does anyone have a solution? Thanks a lot in advance.