我試圖在配置nginx作爲反向代理之前使用gunicorn來提供靜態文件,並且我有點困惑。Gunicorn如何收集靜態?
當我運行我的應用程序時,它似乎像gunicorn無法找到應用程序中的靜態文件夾。
我有以下腳本來運行我的Django應用程序。
#!/bin/bash
# Start with development server
# echo Start server.
# python manage.py runserver 0.0.0.0:8000
# python manage.py collectstatic --noinput # Collect static files
# # Prepare log files and start outputting logs to stdout
touch /srv/logs/gunicorn.log
touch /srv/logs/access.log
tail -n 0 -f /srv/logs/*.log &
# # Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn django_project.wsgi:application \
--name ds4dems \
--bind 0.0.0.0:8000 \
--workers 3 \
--log-level=info \
--log-file=/srv/logs/gunicorn.log \
--access-logfile=/srv/logs/access.log \
"[email protected]"
結果是樣式和圖像不是從main_app靜態文件夾收集的。
文件夾結構如下。
django_project
---- django_project
---- main_app
-------- static
以下我試圖用開發服務器運行相同的應用程序,並收集靜態數據。然後,我再次與Gunicorn合作,並將樣式和圖像傳送給瀏覽器而不會出錯。
究竟發生了什麼?
這是否與this?是唯一可用的選項來設置nginx讓他看到文件?
我有一張你可能會覺得有用的圖畫:http://djangodeployment.com/2016/11/21/how-django-static-files-work-in-production/。它假設你使用'nginx'或'apache',但即使使用whitenoise,它也可能是有用的,因爲原理是相同的。 –
太好了。我會爲此製作一張海報。 :) – sparaflAsh