docker部署django网站(一)

Django
docker
Author

stack

Published

March 25, 2024

记录docker部署django环境遇到的一些问题

##启动gunicorn
../virtualenv/bin/gunicorn -b 0.0.0.0:8001 -w 5 mysite.wsgi:application
  • djang中settiong.py,
    DEBUG=True和False有区别,会影响静态文件找寻

  • 为避免nginx和gunicorn同时占用端口,在nginx配置中转发请求到socket文件, gunicorn再监听该socket文件即可

##nginx配置中需要将请求转移到 ephys.cn.socket
gunicorn -b unix:/tmp/ephys.cn.socket -w 5 mysite.wsgi:application
###/etc/nginx/site-enable/default
server {
    listen 80;
    server_name SITENAME;

    location /static {
        alias /home/USER/sites/SITENAME/source/static;
    }
    location /media {
        autoindex on;
        alias /home/USER/sites/SITENAME/source/media;
    }
    location / {
            proxy_set_header Host $host;
            proxy_pass http://unix:/tmp/SITENAME.socket;

            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        }

}
#后台启动gunicorn
nohup ../virtualenv/bin/gunicorn -b unix:/tmp/ephys.cn.socket -w 5 mysite.wsgi:application > /dev/null 2>&1 &
Back to top