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;
}
}
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!
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:
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.
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
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.