Debugging “Premature end of script headers” – WSGI/Django


I have recently deployed an app to a shared host (webfaction), and for no apparent reason, my site will not load at all (it worked until today).

It is a django app, but the django.log is not even created; the only clue is that in one of the logs, I get the error message: "Premature end of script headers", identifying my wsgi file as the source.

I've tried to add logging to my wsgi file, but I can't find any log created for it. Is there any recommended way to debug this error? I am on the point of tearing my hair out.

My WSGI file:

import os
import sys

from django.core.handlers.wsgi import WSGIHandler

import logging

logger = logging.getLogger(__name__)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
os.environ['CELERY_LOADER'] = 'django'

virtenv = os.path.expanduser("~/webapps/django/oneclickcosvirt/")
activate_this = virtenv + "bin/"
execfile(activate_this, dict(__file__=activate_this))

# if 'VIRTUAL_ENV' not in os.environ:
#    os.environ['VIRTUAL_ENV'] = virtenv


logger.debug('About to run WSGIHandler')

        application = WSGIHandler()
except (Exception,), e:
       logger.debug('Exception starting wsgihandler: %s' % e)
       raise e

Best Solution

It's hard to define exact reason of error without webserver log:

Probable solutions:

And d

Q: Why am I seeing the error message 'premature end of script headers' in the Apache error logs.

A: If using daemon mode, this is a symptom of the mod_wsgi daemon process crashing when handling a request. You would probably also see the message 'segmentation fault'. See answer for question about 'segmentation fault' above.

This error message can also occur where you haven't configured Apache correctly and your WSGI script file is being executed as a CGI script instead.

Hope it will help

Related Question