Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

Docker + Django + Vue + Nginx configuration not reading CSS or JS files (Ioading wrong MIME type)

I have been working on a project that uses Django REST Framework for an API with an embedded Vue frontend. The Vue frontend is located within the Django project folder and uses Django templates for authentication.

I have been trying to get this project working with Docker using Nginx as the server. I have it mostly working but have been struggling with getting the media and static folders to work properly. In my most recent version, Nginx is able to find them but the browser is giving a warning that they are being read as text/html and have the incorrect MIME type.

I already have "include /etc/nginx/mime.types" included in my Nginx conf file, attempted to add a location function for css and js but nothing seems to work.

My docker-compose file looks like this:

version: '3'

services:
  backend:
    build:
      context: ./django
    command: >
        sh -c "python3 manage.py wait_for_database &&
            python3 manage.py collectstatic --no-input &&
            python3 manage.py makemigrations &&
            python3 manage.py migrate --no-input &&
            gunicorn django.wsgi -b 0.0.0.0:8000"
    networks:
      - django-nginx
    volumes:
      - ./django:/app
      - django-static:/django/static
      - django-media:/django/media
    environment:
      - DB_HOST=db
      - DB_NAME=keeptrackdb
      - DB_USER=postgres
      - DB_PASS=postgres
    depends_on:
      - db

  db:
    image: postgres:10-alpine
    environment:
      - "POSTGRES_HOST_AUTH_METHOD=trust"
      - POSTGRES_DB=djangodb
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432"
    networks:
      - django-nginx

  nginx:
    build:
      context: .
      dockerfile: nginx/Dockerfile
    ports:
      - 80:80
    networks:
      - django-nginx
    volumes:
      - ./nginx/prod.conf:/etc/nginx/nginx.conf:ro
      - django-static:/app/static
      - django-media:/app/media
    depends_on:
      - backend
      - db

volumes:
  django-static:
  django-media:

networks:
  django-nginx:
    driver: bridge

And my Nginx conf file looks like this:

user  nginx;
worker_processes  1;

events {
  worker_connections  1024;
}

http {
  include /etc/nginx/mime.types;
  client_max_body_size 100m;

  upstream backend {
    server backend:8000;
  }

  server {
    listen 80;
    charset utf-8;

    root /dist/;
    index index.html;

    # backend urls
    location ~ ^/(admin|api|accounts|media) {
      proxy_redirect off;
      proxy_pass http://backend;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
    }

    location /media/ {
      autoindex on;
      alias app/media;
    }

    location /static/ {
      autoindex on;
      alias app/static;
    }

    # frontend
    location / {
      try_files $uri $uri/ @rewrites;
    }

    location @rewrites {
      rewrite ^(.+)$ /index.html last;
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...