简体   繁体   中英

Gunicorn throws error 403 when accessing static files

python==2.7.5 , django==1.11.10 , gunicorn==19.7.1 , RHEL 7.4

I have a django project at my job written not by me. It was in eventcat user's home directory and with time we ran out of available space on the disk. I was to move the project to /data/ . After I moved the project directory and set up a new environment I faced the problem that static files are not loaded and throwing 403 forbidden error.

Well, I know that gunicorn is not supposed to serve static files on production, but this is an internal project with low load. I have to deal with it as is.

The server is started with a selfwritten script (I changed the environment line to new path):

#!/bin/sh
. ~/.bash_profile
. /data/eventcat/env/bin/activate
exec gunicorn -c gunicorn.conf.py eventcat.wsgi:application

The gunicorn.conf.py consists of:

bind = '127.0.0.1:8000'
backlog = 2048
workers = 1
worker_class = 'sync'
worker_connections = 1000
timeout = 120
keepalive = 2
spew = False
daemon = True
pidfile = 'eventcat.pid'
umask = 0
user = None
group = None
tmp_upload_dir = None
errorlog = 'er.log'
loglevel = 'debug'
accesslog = 'ac.log'
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s"'
proc_name = None 

def post_fork(server, worker):
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def pre_fork(server, worker):
    pass

def pre_exec(server):
    server.log.info("Forked child, re-executing.")

def when_ready(server):
    server.log.info("Server is ready. Spawning workers")

def worker_int(worker):
    worker.log.info("worker received INT or QUIT signal")
    import threading, sys, traceback
    id2name = dict([(th.identm, th.name) for th in threading.enumerate()])
    code = []
    for threadId, stack in sys._current_frames().items():
        code.append("\n# Thread: %s(%d)" % (id2name.get(threadId, ""), threadId))
        for filename, lineno, name, line in traceback.exctract_stack(stack):
            code.append('File: "%s", line %d, in %s' %(filename, lineno, name))
            if line:
                code.append(" %s" % (line.strip()))
    worker.log.debug("\n".join(code))

def worker_abort(worker):
    worker.log.info("worker received SIGABRT signal")

All the files in static directory are owned by eventcat user just like the directory itself. I couldn't find any useful information in er.log and ac.log .

The server is running on https protocol and there is an ssl.conf in project directory. It has aliases for static and media pointing to previous project location and I changed all these entries to the new ones. Though I couldn't find where this config file is used.

Please, advise how can I find out what is the cause of the issue. What config files or anything should I look into?

UPDATE : Thanks to @ruddra, gunicorn wasn't serving static at all. It was httpd that was. After making changes in httpd config everything is working.

As far as I know, gunicorn does not serve static contents. So to serve static contents, its best to use either whitenoise or you can use NGINX, Apache or any reverse proxy server. You can check Gunicorn's documentation on deployment using NGINX.

If you want to use whitenoise, then please install it using:

pip install whitenoise

Then add whitenoise to MIDDLEWARES like this(inside settings.py ):

MIDDLEWARE = [
  # 'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM