502 Bad Gateway serving Django app through gunicorn

I’m trying to serve a Django application with Caddy, but I get an 502 Bad Gateway when I hit the domain.

I’m using Gunicorn in between as recommended on the FAQ.
My Caddyfile looks like this:

plorenzo.es {
    root /home/user/project/
    proxy / {

I copied it from examples/django at master · caddyserver/examples · GitHub.

I run gunicorn like: (I followed this)

gunicorn --bind myproject.wsgi:application

Also if I run gunicorn directly and I try domain.tld:8000 works fine.

Any idea what can be happening?

What’s the error message(s) in the logs? (You’ll need the errors directive to see them, probably. And run caddy with -log to write a process log; it catches certain transport-level errors that happen internally.)

The log shows this:

WARNING: File descriptor limit 1024 is too low for production servers. At least 8192 is recommended. Fix with     "ulimit -n 8192".
17/Jul/2017:21:04:18 +0200 [ERROR 502 /] dial tcp getsockopt: connection refused

Could have something to do with the file descriptor limit?

OK! I think I found the problem, with this Caddy file is working:

plorenzo.es {
    root /home/user/project

    log /var/log/caddy/access.log
    errors /var/log/caddy/errors.log

    proxy / unix:/home/user/project/project.sock {

Gunicorn is using a unix socket, that’s why I didn’t work with the local address.

This troubleshooting guide gave my the hint.

Thank you for the help!

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.