我已经看到了使用gunicorn和nginx托管django应用程序的两种策略。
网络端口上运行gunicorn。
例子(来自http://goodcode.io/blog/django-nginx-gunicorn/):
nginx配置:
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 10;
proxy_read_timeout 10;
proxy_pass http://localhost:8000/;
}
启动时将gunicorn绑定到UNIX套接字
例如 http://michal.karzynski.pl/blog/2013/06/09/django-nginx-gunicorn-virtualenv-supervisor/
nginx配置
upstream hello_app_server {
server unix:/tmp/gunicorn.sock fail_timeout=0;
}
...
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://hello_app_server;
break;
}
}
gunicorn配置
...
bind = 'unix:/tmp/gunicorn.sock'
...