Question

Running unicorn alongside daphne on nginx

I have been following several different tutorials about how to set up gunicorn and daphne in parallel so that gunicorn can serve http to my django apps and daphne to my django channels app. However, I am now stuck on the welcome to nginx homepage and I cannot figure out what the problem is.

supervisor.conf

[program:example]
directory=/home/user/example/example
command=/home/user/envs/example/bin/gunicorn example.wsgi:application
user=user
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/user/envs/example/bin/gunicorn-error.log

[program:serverinterface]
directory=/home/user/example/example
command=/home/user/envs/example/bin/daphne -b 0.0.0.0 -p 8001 example.asgi:application
autostart=true
autorestart=true
stopasgroup=true
user=user
stdout_logfile = /home/user/example/bin/gunicorn-error.log

nginx/sites-availible/example.com

upstream app_server {
    server http://unix:/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 301 https://example.com$request_uri;
}

server {
    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    server_name example.com www.example.com;

    # Let's Encrypt parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        try_files $uri @proxy_to_app;
    }

    location /ws/ {
        try_files $uri @proxy_to_ws;
    }
    location @proxy_to_app {
        proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header   Host              $http_host;
        proxy_redirect     off;
    }
location @proxy_to_ws {
        proxy_pass http://0.0.0.0:8001;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $server_name;
    }
}

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 6, 2023

Hi there,

It appears that your Nginx configuration may be missing some proxy_pass directives for forwarding the requests to your Gunicorn and Daphne servers. The missing proxy_pass directive in the @proxy_to_app location block could be the reason why you are stuck on the Nginx welcome page.

Here’s how you might modify your Nginx configuration to correct the issue:

  1. Update the upstream block: Since Gunicorn serves your HTTP requests, define it in the upstream block. If you are using a Unix socket for Gunicorn, you can keep it as is; otherwise, you may replace it with the appropriate IP and port.

  2. Fix the proxy_pass directives: You will need to specify the appropriate proxy_pass directives to route the requests to the upstream app server for HTTP requests and to Daphne for WebSocket requests.

Here’s an example of how you can modify the nginx/sites-available/example.com file:

upstream app_server {
    server unix:/run/gunicorn.sock fail_timeout=0; # or server 127.0.0.1:8000;
}

server {
    # ... rest of the configuration ...

    location / {
        try_files $uri @proxy_to_app;
    }

    location /ws/ {
        try_files $uri @proxy_to_ws;
    }

    location @proxy_to_app {
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   Host $http_host;
        proxy_redirect     off;
        proxy_pass         http://app_server; # proxy to Gunicorn
    }

    location @proxy_to_ws {
        proxy_pass http://0.0.0.0:8001; # proxy to Daphne

        # ... rest of the configuration ...
    }
}

Once you have made the necessary changes, restart Nginx to apply the new configuration:

sudo nginx -t # Test the configuration
sudo systemctl restart nginx # Restart Nginx

Check the Nginx error log if you encounter any issues:

sudo tail -f /var/log/nginx/error.log

With these modifications, Nginx should forward HTTP requests to Gunicorn and WebSocket requests to Daphne, based on the paths provided in your configuration. Make sure that Gunicorn and Daphne are running, and the paths and ports in the Nginx configuration correspond to their actual locations and ports.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel