from
?django.contrib.staticfiles.urls?
import
?staticfiles_urlpatterns
?
?
urlpatterns?
=
?[
??
url(
'^admin/'
,?admin.site.urls),
]
urlpatterns?
+
=
?staticfiles_urlpatterns()
# 修改settings.py文件的:INSTALLED_APPS INSTALLED_APPS = [ ... 'gunicorn', # 把gunicorn添加到apps中 ]
配置腳本文件啟動django項目:
# gunicorn_config.py
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
bind = '0.0.0.0:8000' ? ? ?# 綁定ip和端口號
# chdir = '/opt/workspace/bookstore' ?# 目錄切換
# backlog = 500 ? ? ? ? ? ? ?# 監(jiān)聽隊列
timeout = 60 ? ? ? ? ? ? ? ? # 超時
worker_class = 'gevent' # 使用gevent模式,還可以使用sync 模式,默認的是sync模式
workers = multiprocessing.cpu_count() * 2 + 1 ? ?# 進程數(shù)
threads = 2 ?# 指定每個進程開啟的線程數(shù)
loglevel = 'info' ?# 日志級別,這個日志級別指的是錯誤日志的級別,而訪問日志的級別無法設(shè)置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
accesslog = "/www/wwwroot/django_DRF_connecting/log/gunicorn_access.log" ?# 訪問日志文件
errorlog = "/www/wwwroot/django_DRF_connecting/log/gunicorn_error.log" ? ?# 錯誤日志文件
讓gunicorn通過守護進程運行,也就是后臺運行。
gunicorn -c gunicorn.conf.py -D django_DRF_connecting.wsgi:application
-c 指定一個配置文件(py文件)
-b 與指定的socket進行綁定
-D 以守護進程形式來運行Gunicorn進程,其實就是將這個服務(wù)放到后臺去運行
-w 工作的進程數(shù)量;
-k 工作進程類型,sync(默認), eventlet, gevent, or tornado, gthread, gaiohttp.
http://docs.gunicorn.org/en/latest/settings.html
以下是將gunicorn.conf.py配置成127.0.0.1:8000的nigix文件修改內(nèi)容,我沒有用反向代理直接在gunicorn里面配置的0.0.0.0:8080,大家可以做為參考
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
? ??
? ? sendfile ? ? ? ?on;
? ? #tcp_nopush ? ? on;文章來源:http://www.zghlxwxcb.cn/news/detail-486601.html
? ? #keepalive_timeout ?0;
? ? keepalive_timeout ?65;
? ? server {
? ? ? ? listen ? ? ? 80;
? ? ? ? server_name ?www.flag.space, 109.39.89.199;
? ? ? ? location / {
? ? ? ? ? ? proxy_pass http://127.0.0.1:8000;
? ? ? ? ? ? #root ? html;
? ? ? ? ? ? #index ?index.html index.htm;
? ? ? ? }
? ? ? ? location /static {
? ? ? ? ? ? alias /opt/workspace/bookstore/static;
? ? ? ? }
? ? ? ? error_page ? 500 502 503 504 ?/50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root ? html;
? ? ? ? }
? ? }
}
?文章來源地址http://www.zghlxwxcb.cn/news/detail-486601.html
到了這里,關(guān)于使用gunicorn部署django項目時,發(fā)現(xiàn)靜態(tài)文件加載失敗問題,及部署的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!