Report this

What is the reason for this report?

How to resolve This (django) site can’t be reached with nginx, gunicorn, droplet ?

Posted on May 7, 2021

I was following this tutorial for deploying my django project to digitalocean droplet [https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-20-04]. But I have been facing this issue for last 2 days. Here are my configuration files: /etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
}


http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

    server {
        listen          80;
        server_name     167.71.233.157;
        location / {
                proxy_pass http://unix:/run/gunicorn.sock;
        }
    }

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    client_max_body_size 100M;
}

/etc/nginx/sites-enabled/lavisco_ecommerce

server {
    listen 80;
    server_name 167.71.233.157;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/lavisco/Lavisco_Ecommerce;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}


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!

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.

Heya, Try some of the following suggestions:

Nginx Configuration (/etc/nginx/nginx.conf)

  1. Server Block Inside http Block:

    • You have a server block inside the main http block. It’s generally recommended to separate individual server configurations into their own files under /etc/nginx/sites-available/ and then create symbolic links to those files in the /etc/nginx/sites-enabled/ directory. This helps keep configurations organized and manageable.
  2. Proxy Pass:

    • The proxy_pass directive is using a Unix socket (http://unix:/run/gunicorn.sock;). Ensure that this matches exactly with what Gunicorn is using. Check your Gunicorn service file (typically found at /etc/systemd/system/gunicorn.service) for the --bind option.

Nginx Site Configuration (/etc/nginx/sites-enabled/lavisco_ecommerce)

  1. Server Block:

    • This file contains the specific server block for your Django application. It’s correctly listening on port 80 and specifies the server IP.
  2. Static Files Location:

    • The location block for static files (location /static/) seems correctly configured. Ensure that /home/lavisco/Lavisco_Ecommerce is the correct path where your Django static files are collected (STATIC_ROOT in your Django settings).
  3. Gunicorn Proxy Pass:

    • Similar to the main nginx.conf, this uses the same proxy_pass. Just ensure it’s correctly pointing to the Gunicorn socket.

General Suggestions:

  • Remove Redundant Server Block:

    • If /etc/nginx/sites-enabled/lavisco_ecommerce is the specific configuration for your Django app, the server block inside nginx.conf might be redundant and could potentially cause conflicts. Consider removing it.
  • Check Symbolic Links:

    • Make sure there is a symbolic link from /etc/nginx/sites-available/lavisco_ecommerce to /etc/nginx/sites-enabled/lavisco_ecommerce. This is typically done using:
sudo ln -s /etc/nginx/sites-available/lavisco_ecommerce /etc/nginx/sites-enabled

Nginx and Gunicorn Services:

  • Ensure that both Nginx and Gunicorn services are running. You can check their status with:
sudo systemctl status nginx
sudo systemctl status gunicorn

Firewall Configuration:

  • Make sure your firewall is configured to allow traffic on port 80 (HTTP). If you’re using UFW, you can enable it with:
sudo ufw allow 'Nginx Full'
  • Error Logs:

    • If you’re encountering specific errors, check the Nginx error logs (/var/log/nginx/error.log) and Gunicorn logs for clues.
  • File Permissions:

    • Ensure that the user running Nginx (typically www-data) has the necessary permissions to access the Gunicorn socket and the Django project files, especially the static files.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.