I’m trying to deploy my app on a server with docker compose:
services:
frontend:
container_name: frontend
depends_on:
- backend
build:
context: frontend
dockerfile: ./Dockerfile
ports:
- 80:80
environment:
NODE_EVN: production
networks:
- app-network
backend:
container_name: backend
depends_on:
- db
build:
context: backend
dockerfile: ./Dockerfile
ports:
- 8000:8000
environment:
NODE_EVN: production
DB_URL: mongodb://db/dbname
networks:
- app-network
db:
container_name: db
image: mongo:latest
ports:
- 27017:27017
volumes:
- dbname:/data/spares
networks:
- app-network
volumes:
dbname:
networks:
app-network:
My frontend container has ngingx installed to deliver compiled react app and serve as a reverse proxy to backend api. nginx.conf:
{
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/v1/ {
proxy_pass http://backend:8000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_cache off;
}
}
Frontend works, but I’m getting 404 when trying to access backend:
frontend | <my ip address> - - [30/Jul/2024:18:34:38 +0000] "GET /api/v1/ HTTP/1.1" 404 153 "-" "curl/8.5.0" "-"
I tried to docker exec to frontend container and curl from there – Ok, got 200 response.
What am I doing wrong? How can I configure nginx to proxy /api/v1/ to backend ?
One small thing. My app is running under /myapp/, but on separate vm.
So they reverse proxied to it:
location /myapp/ {
proxy_pass http://<my vm IP-address>:80/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_cache off;
}
So it looks like
-> reverse proxy to my vm : serve frontend from container and reverse proxy api requests to backend in another container.
I have tried to change locations in my nginx.conf to /spares/ and /spares/api/v1/ accordingly, but it didn’t work.