i am deploying my app on AWS instance. i have both frontend and backend as docker image.
i want to use nginx. when i run this config, and go to AWS given public URL, then 502 is returned by nginx.
/api
/docs
they are working but
/admin
/
returning 502.
here is my conf files:
docker-compose.yml:
backend:
container_name: backend
image: ${BACKEND_IMAGE_NAME}:${IMAGE_TAG}
command: python manage.py runserver 0.0.0.0:8000
env_file:
- .env
expose:
- 8000
frontend:
container_name: frontend
image: ${FRONTEND_IMAGE_NAME}:${IMAGE_TAG}
expose:
- 3002
env_file:
- .env
nginx:
build: ./nginx
ports:
- 80:80
depends_on:
- backend
- frontend
here is nginx conf:
upstream backend {
server backend:8000;
}
upstream frontend {
server frontend:3002;
}
server {
listen 80;
location /api/ {
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /docs/ {
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /admin/ {
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location / {
proxy_pass http://frontend; # Proxy to frontend on port 3002
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
# Ensure that Nginx handles large files and requests well
client_max_body_size 100M;
# Timeout settings to prevent 502 Bad Gateway
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
}
}```
nginx docker file:
FROM nginx:1.27
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d```
front end docker file:
# Use a Node.js base image
FROM node:18-alpine
# Set working directory
WORKDIR /app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the rest of the application code
COPY . .
# Build the app for production
RUN npm run build
# Expose the port your app runs on
EXPOSE 3002
# Start the app
CMD ["npm", "run", "start"]
backend docker file:
FROM python:3.12
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app/src
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app/
EXPOSE 8000