Question

Nginx fails to load static files

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"';
	}
}

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.

Accepted Answer

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.

KFSys
Site Moderator
Site Moderator badge
February 5, 2020

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

Try DigitalOcean for free

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

Sign up

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