Report this

What is the reason for this report?

Nginx fails to load static files

Posted on February 5, 2020
R G

By R G

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


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.
0

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.

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 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.