使用gunicorn部署django项目时,发现静态文件加载失败问题,及部署

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              # 监听队列
timeout = 60                 # 超时
worker_class = 'gevent' # 使用gevent模式,还可以使用sync 模式,默认的是sync模式
workers = multiprocessing.cpu_count() * 2 + 1    # 进程数
threads = 2  # 指定每个进程开启的线程数
loglevel = 'info'  # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
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 工作的进程数量;
-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文件修改内容,我没有用反向代理直接在gunicorn里面配置的0.0.0.0:8080,大家可以做为参考

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
    #tcp_nopush     on;

    #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;
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值