Django+Nginx+uWSGIをDocker Containerで動かす時の設定資料の読解
概要
Django+Nginx+uWSGIをDocker Containerで動かす時の設定資料の読解編。マジでよくわからんので、こういう時は写経に限る。
参考サイト
↑このDockerfile, nginx_app.conf, supervisor-app.conf, uwsgi-prams, uwsgi.ini を中心に読解していく。
全体像
全体像のイメージはこんな感じ。ちょっと違うかも?でも大体あってるはず!
Nginx_app.confの読解
# mysite_nginx.conf ''' upstream=アップストリームとは、通信回線で、ネットワークの末端側(個々の利用者や端末、クライアントなど)から中心側(通信施設や集線装置、サーバなど)へ向かう方向 なので、今回は、djangoへ向かうように設定する。 app.sock という指定で、通信するsocketを指定できる。unix独自のシステムみたい。 なぜ、app.sockかというと、app以下にdjangoのファイルを設定するから ''' upstream django { server unix:/home/docker/code/app.sock; # for a file socket # server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server ''' severの設定をする。ここでドメインを指定しないと、ドメインがマッチしない!と言って、表示されないらしい。 listenについては、この記事が面白かったw ''' server { # the port your site will be served on, default_server indicates that this server block # is the block to use if no blocks match the server_name # 80番ポートで、default_serverが聞き耳を立てているという状態のよう。 listen 80 default_server; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media の場所を指定。これで、djangoではなく、nginxがstaticfileを扱うようになる。 # S3のアドレスはここに指定するのかな。 location /media { alias /home/docker/persistent/media; # your Django project's media files - amend as required } # Static media の場所を指定。これで、djangoではなく、nginxがstaticfileを扱うようになる。 # S3のアドレスはここに指定するのかな。 location /static { alias /home/docker/volatile/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. # 最後は、staticfile以外の全てのリクエストをdjango serverに送る。 # uwsgi_params の設定に従って、uwsgiを経由してdjangoに送る。 location / { uwsgi_pass django; include /home/docker/code/uwsgi_params; # the uwsgi_params file you installed } }
わかりづらい用語の解説
socket:nginxとuWSGIをつなぐ接続口みたいなもの。 正直いまだによくわからない。とりあえず、具体的な仕組みは不明。
- めっちゃわかりやすかった参考サイト: 「Can't connect to local MySQL server through socket」エラーについて
server listen: server(今回の場合はnginx)が通信を聞く準備ができてる状態のようです。portを開くとはちょっと違うぽい。あくまでも聞く準備ができているという感じのようです。この記事が面白かったw
まあ、なんとなくわかったようなわからないような。。。
uwsgi.ini の読解
uWSGI で Django を使う方法 — Django 1.4 documentation
[uwsgi] # this config will be loaded if nothing specific is specified # load base config from below ini = :base # %d is the dir this configuration file is in # %dは、このconfigファイルのある場所を表している。 socket = %dapp.sock master = true processes = 4 [dev] ini = :base # socket (uwsgi) is not the same as http, nor http-socket socket = :8001 [local] ini = :base http = :8000 # set the virtual env to use home=/Users/you/envs/env [base] # chdir to the folder of this config file, plus app/website # chdir=チェンジディレクトリ chdir = %dapp/ # load the module from wsgi.py, it is a python path from # the directory above. module=website.wsgi:application # allow anyone to connect to the socket. This is very permissive chmod-socket=666
iniファイルは書式が全然わからん。。。。なんとなくの意味はわかるけど、ちょっと別の方法のようないい気がするな。
uwsgi_params の読解
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 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;
全くわからんがこれはこのままで、設定する必要なしでいいぽい!ラッキー!
公式サイト参照:
Nginx support — uWSGI 2.0 documentation
supervisor-app.conf の読解
# uwsgi を実行 --iniオプションで、uwsgi.iniのpathを指定して初期化する。 [program:app-uwsgi] command = /usr/local/bin/uwsgi --ini /home/docker/code/uwsgi.ini # nginxを実行 [program:nginx-app] command = /usr/sbin/nginx
Dockerfileを読解
FROM ubuntu:14.04 # Install required packages and remove the apt packages cache when done. RUN apt-get update && apt-get install -y \ git \ python \ python-dev \ python-setuptools \ nginx \ supervisor \ sqlite3 \ && rm -rf /var/lib/apt/lists/* RUN easy_install pip # install uwsgi now because it takes a little while RUN pip install uwsgi # setup all the configfiles # daemon off = daemonをoffにする事で、foregroundで実行するようになる。daemonだとコンテナが停止してしまうため。 RUN echo "daemon off;" >> /etc/nginx/nginx.conf # nginx-app.confをコピーしてコンテナに配置する。 COPY nginx-app.conf /etc/nginx/sites-available/default # supervisor-app.conf をコピーしてコンテナに配置する。 COPY supervisor-app.conf /etc/supervisor/conf.d/ # COPY requirements.txt and RUN pip install BEFORE adding the rest of your code, this will cause Docker's caching mechanism # to prevent re-installinig (all your) dependencies when you made a change a line or two in your app. # 先にrequirements.txtだけコピーして、pip installする。 COPY app/requirements.txt /home/docker/code/app/ RUN pip install -r /home/docker/code/app/requirements.txt # add (the rest of) our code COPY . /home/docker/code/ # install django, normally you would remove this step because your project would already # be installed in the code/app/ directory RUN django-admin.py startproject website /home/docker/code/app/ # コンテナの80portを解放 EXPOSE 80 # supervisord -n のオプションは、foregroundで実行するオプションなので、すごく重要! CMD ["supervisord", "-n"]
- daemon off = デーモンをオフにする事で、foregroundで実行するようになる。 参照サイト:
nginxをdockerで動かす時のTips 3選 - インフラエンジニアway - Powered by HEARTBEATS
- supervisord の解説
Running Supervisor — Supervisor 3.3.1 documentation
うーん、わかったようなわからないような〜、iniファイルだけはいまいち理解できないなぁ。