0

I have connected my Django (DRF) to Gunicorn and Nginx and put it all in docker.

When I load mysite.com/admin/ it looks bad, just plain text. So it seems like it does not load any static file. However, in Browser Console there are zero errors. enter image description here

Moreover, I see that all the static files have been successfully loaded from server (all the /static/ requests are HTTP 200) and I can open them right in my browser by putting url: mysite.com/static/admin/css/base.css. And this file will successfully open. But admin site does not want to apply it. enter image description here On localhost with Debug=True everything is working fine too.

On main web site all the /media/ is working good too, so the problem is only within /static/.

nginx.conf

events {}
http {
    server {
        listen 80;
        server_name mysite.com;
        server_tokens off;

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 80;
        server_name api.mysite.com;
        server_tokens off;

        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 443 ssl;
        server_name mysite.com;
        server_tokens off;

        ssl_certificate  /etc/nginx/certs/mysite.com-chain.pem;
        ssl_certificate_key /etc/nginx/certs/mysite.com-key.pem;

        location / {
            proxy_pass http://10.0.2.237:3004; # React Frontend
        }

    }

    server {
        listen 443 ssl;
        server_name api.mysite.com;
        server_tokens off;

        ssl_certificate  /etc/nginx/certs/api.mysite.com-chain.pem;
        ssl_certificate_key /etc/nginx/certs/api.mysite.com-key.pem;

        location /media/ {
            autoindex on;
            alias /django-media/;
        }

        location /static/ {
            autoindex on;
            alias /django-static/;
        }

        location / {
            try_files $uri @proxy_api;
        }

        location @proxy_api {
            proxy_set_header X-Forwarded-Proto https;
            proxy_set_header X-Url-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass   http://10.0.2.237:7000; # Django Gunicorn Backend
        }


    }
}

docker-compose.backend.yml

version: '3.8'
services:
  postgres_db:
    container_name: postgres-prod
    image: postgres
    ports:
      - "5543:5543"
    volumes:
      - postgres_data_prod:/var/lib/postgresql/data/
    env_file:
      - .env.production
    command: -p 5543
  web:
    container_name: django-backend-prod
    build: .
    env_file:
      - .env.production
    ports:
      - "7000:8001"
    entrypoint: ./entrypoint.production.sh
    volumes:
      - .:/code
      - media:/code/media
      - static:/code/project/static
    depends_on:
      - postgres_db
    networks:
      - default
    restart: always

volumes:
  media:
    driver_opts:
      type: none
      device: ${PWD}/media
      o: bind
  postgres_data_prod:

networks:
  default:
    external:
      name: sentry-net

docker-compose.frontend.yml (in different directory, static and media are connected through volumes)

version: '3.8'

services:
  frontapp:
    container_name: React-Frontend-PROD
    build:
      context: .
      dockerfile: Dockerfile

    volumes:
      - '.:/app'
      - '/app/src'
      - '/app/node_modules'
    ports:
      - "3004:3000"
    env_file:
      - .env.production

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - .nginx/nginx.conf:/etc/nginx/nginx.conf
      - .certs/:/etc/nginx/certs
      - django-backend-prod_media:/django-media
      - django-backend-prod_static:/django-static
    links:
      - frontapp
    depends_on:
      - frontapp
    networks:
      - default

volumes:
  django-backend-prod_media:
    external: true
  django-backend-prod_static:
    external: true


networks:
  default:
    external:
      name: sentry-net
Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
NIKITOS
  • 23
  • 3
  • all your configuration looks fine. try opening the page in incognito and check – LiquidDeath Jul 20 '22 at 09:30
  • Yeah, I tried this on separate devices, different browsers, but still Django just doesn't want to apply all those styles, even if it successfully loads it – NIKITOS Jul 20 '22 at 09:34

0 Answers0