django 项目部署用到的docker-compose 文件

有三个文件需要配置:

参考官方教程:

https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

  1. docker-compose.yml
# yaml 配置
version: '3'
services:
  web:
    build: .
    ports:
     - "9001:9000"
     - "8002:8002"
    volumes:
      - ../erp:/usr/src/app/erp
      - /tmp:/tmp

  nginx:
    image: nginx:1.19.6
    network_mode: host
    volumes:
#     - ./templates:/etc/nginx/templates
     - ./nginx:/etc/nginx/conf.d
     - /tmp:/tmp
#    environment:
#     - NGINX_HOST=foobar.com
#     - NGINX_PORT=9002
  1. nginx.conf
upstream django {
    server unix:///tmp/mysite.sock; # for a file socket
    # server 0.0.0.0:8002; # for a web port socket (we'll use this first)
}
server {
    listen       8001;
    listen  [::]:8001;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        #root   /usr/share/nginx/html;
        #index  index.html index.htm;
    uwsgi_pass  django;
    include     /etc/nginx/conf.d/uwsgi_params;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

  1. uwsgi.ini
# mysite_uwsgi.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /usr/src/app/erp
# Django's wsgi file
module          = ERP.wsgi
# the virtualenv (full path)
;home            = /home/yterp/erp/venvProject/django1.10

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 4
threads         = 8
# the socket (use the full path to be safe
;socket          = 0.0.0.0:8002
socket    = /tmp/mysite.sock
# http = 0.0.0.0:9000
# ... with appropriate permissions - may be needed
chmod-socket    = 666
# clear environment on exit
vacuum          = true

# 开启守护进程, 日志打印到文件
;daemonize       = logs/uwsgi.log
# 日志大小, 这里设50m
log-maxsize     = 50000000
# 进程文件
pidfile         = ./starerp.pid

static-map = /static/=./static/
  1. 由于使用的socket转发,nginx配置中还需要,uwsgi_parms,可以从github下载
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;
posted @ 2021-01-26 14:35  那时一个人  阅读(118)  评论(0)    收藏  举报