Question

Stuck on welcome to nginx (nginx+gunicorn+supervisor+daphne)

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:mysite]
directory=/home/user/mysite/mysite
command=/home/user/envs/mysite/bin/gunicorn mysite.wsgi:application
user=user
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/home/user/envs/mysite/bin/gunicorn-error.log

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

nginx/sites-availible/mysite.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://tutoraustria.com$request_uri;
}

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

    # Let's Encrypt parameters
    ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mysite.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 17, 2023

Hello,

This typically happens when the configuration for Nginx is not properly set up to pass requests to your application. Here’s a checklist that might help you identify the issue:

  1. Check Nginx Configuration Syntax: Run sudo nginx -t to verify that your Nginx configuration file has the correct syntax.

  2. Correct File Location: Make sure that your configuration file is symlinked from sites-available to sites-enabled. You can create a symbolic link with:

    sudo ln -s /etc/nginx/sites-available/mysite.com /etc/nginx/sites-enabled/
    
  3. Remove Default Nginx Configuration: The default Nginx configuration may be conflicting with your site’s configuration. You can remove the default symlink in the sites-enabled directory:

    sudo rm /etc/nginx/sites-enabled/default
    
  4. Gunicorn Socket: In the Nginx configuration, you’ve defined server http://unix:/run/gunicorn.sock; but you haven’t set up proxy_pass to this socket in location @proxy_to_app. Update it to:

    location @proxy_to_app {
        proxy_pass http://app_server;
        # rest of the configuration remains same
    }
    
  5. Fix the Upstream Server Line: Remove http:// from the server line under upstream app_server:

    server unix:/run/gunicorn.sock fail_timeout=0;
    
  6. Check Gunicorn Configuration: Ensure that Gunicorn is correctly configured to use the same socket file that Nginx is expecting (/run/gunicorn.sock).

  7. Check Supervisor Status: Ensure that both Gunicorn and Daphne are running under Supervisor. You can check the status with:

    sudo supervisorctl status
    
  8. Check Log Files: Review the Nginx error log (usually at /var/log/nginx/error.log) and the Gunicorn log file you defined (/home/user/envs/mysite/bin/gunicorn-error.log). This might contain useful information regarding the issue.

  9. Reload Configuration: After making these changes, don’t forget to reload Nginx:

    sudo nginx -s reload
    

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