DjangoでNginxのUnix Socketを利用する
Unix Socketを使う
upstream blog_app_server {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
server unix:/opt/example.com/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.example.com example.com;
server_tokens off;
access_log /opt/example.com/logs/nginx-access.log;
error_log /opt/example.com/logs/nginx-error.log;
location /static/ {
alias /opt/example.com/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://blog_app_server;
break;
}
}
# 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;
}
}
Dockerの場合
Docker間はVolumeでマウントする
What's should Django ALLOWED_HOSTS be when using a unix socket?
uWsgi
Nginx: プロキシ・リバースプロキシ・WEBサーバー Gunicorn: APサーバー uWsgi: GateWay, Interface
公式ページ
- Nginx
- Gunicorn - Python WSGI HTTP Server for UNIX
- PEP 333 -- Python Web Server Gateway Interface v1.0
参考
- Flask + uWSGI + Nginx でハローワールドするまで @ さくらのVPS (CentOS 6.6)
- 1台のサーバ上にDockerを使って複数サービス運用してみる
- DockerでPython+uWSGI+Nginxの環境を作成
- docker-compose up で Django + PostgreSQL + Nginx + Gunicorn を起動してみる
- Deploy Django, Gunicorn, NGINX, Postgresql using Docker
- Djangoの環境をDocker化する(Docker + Django + Gunicorn + nginx)その1