Question

Nginx fails to load static files

Posted February 5, 2020 217 views
NginxDjango

I am following this tutorial to deploy my Django website: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps.

I am able to connect to the site using gunicorn, but all of my pages are displayed without any static content (css, javascript.)

Here is my nginx configuration file for the site.

server {
    server_name [DROPLET IP ADDRESS];

    access_log off;

    location /static/ {
        alias /home/myuser/myvirtualenv/myproject/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}
edited by MattIPv4

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.

×
2 answers

I solved this myself.

The issue was a misconfiguration of both Nginx and Gunicorn.

Nginx was not set to listen on a port. Here is the updated site configuration file:

server {
    listen 1234;
    server_name [DROPLET IP ADDRESS];

    access_log off;

    location /static/ {
        alias /home/myuser/myvirtualenv/myproject/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

Gunicorn’s issue was that it was set to listen to my hosts IP address rather than the address Nginx’s “proxy_pass.”

I initially launched Gunicorn using my Droplet’s IP address.

gunicorn --bind [DROPLET IP ADDRESS]:8001 drawbook.wsgi

I changed [DROPLET IP ADDRESS] to 127.0.0.1 and now everything works after restarting both Gunicorn and Nginx.

Hi @richiegreen95,

The problem that I’m seeing is with your proxy_pass and your backend. You need to make sure that your backend service is actually listening on port 8001. You can do that with:

sudo netstat -plant | grep 8001

If you don’t see any output you need to make sure that you’ve followed the instructions in step 9 regarding the Gunicorn configuration.

Regards,
KDSys

  • The output is

    tcp        0      0 [DROPLET IP ADDRESS]:8001     0.0.0.0:*               LISTEN      3145/python3        
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36214     TIME_WAIT   -                   
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36218     TIME_WAIT   -                   
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36212     TIME_WAIT   -                   
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36216     TIME_WAIT   -                   
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36210     TIME_WAIT   -                   
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36220     TIME_WAIT   -                   
    tcp        0      0 [DROPLET IP ADDRESS]:8001     73.76.127.146:36222     TIME_WAIT   -   
    

    I only get output if gunicorn is currently running.

Submit an Answer