简体   繁体   中英

nginx and django setting for docker-compose

I have setting with nginx and django (named python container).

I can access see the top page with localhost:8000 , however I cannot fetch the static file nor use API localhost:8000/api/items

I am newbee for nginx and django so still confusing.

I am planning the setting like this belo

normal files browser ->8000 -> nginx -> 8001 ->django

for static file

django ->8000 -> nginx

am it correct???or where should I fix??

These are settings below.

docker-composer.yml

version: '3'
services:
  python:
    container_name: python
    build: ./python
    command: uwsgi --socket :8001 --module myapp.wsgi --py-autoreload 1 --logto /tmp/mylog.log

    expose:
      - "8001"
  nginx:
    image: nginx:1.13
    container_name: nginx
    ports:
      - "8000:8000"
    volumes:
      - ./nginx/conf:/etc/nginx/conf.d
      - ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
      - ./nginx/static:/static
    depends_on:
      - python

app_nginx.conf

upstream django {
    ip_hash;
    server python:8001;
}

server {
    listen      8000;
    server_name 127.0.0.1;
    charset     utf-8;

    location /static {
        alias /static;
    }

    location / {
        uwsgi_pass  django;
        include     /etc/nginx/uwsgi_params;
    }
}

server_tokens off;

uwsgi_prams

uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;

Static files are generated on django management command collectstatic .

If run in django container - these files will be generated and present only in django container. So, STATIC_ROOT should be shared with nginx - ie one common docker volume mounted both to django container STATIC_ROOT and to nginx container static files path.

This command may be used as part of entrypoint script of django container to be auto-run on every start.

  python:
    volumes:
      - static-volume:/app/static

  nginx:
    volumes:
      - static-volume:/static

volumes:
  static-volume:

Or static files can be generated and provided to nginx /static in other way, ie as an artifact during ci build / deploy.


Or you can use whitenoise and make django serve its static files (but not media files).

Also, files can be stored (and distributed by) in cloud like Amazon S3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM