Question

Nginx not serving static files despite various configurations

I am trying to deploy my very first site and using django + nginx + gunicorn configuration as instructed in this guide:

https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn

However despite doing everything according to instructions my static files are not server. I tried various solutions and read all related questions I can found but with no results. Please help me find an error, tell me way to debug it or just offer an alternative to nginx because it drives me crazy.

My current settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = '/opt/myenv/static/'
STATICFILES_DIRS = ( '/opt/myenv/static/',)
MEDIA_URL = '/media/'
```

My `ngnix.config`:

```
        server {
                server_name stvskaiciuokles.eu www.stvskaiciuokles.eu;
                access_log /opt/myenv/STV_skaiciuokle/access.log;
                error_log /opt/myenv/STV_skaiciuokle/error.log warn;

                location /static {
                        autoindex on;
                        alias /opt/myenv/static/;
                        }

                location /media {
                        autoindex off;
                        root /opt/myenv/STV_skaiciuokle/skaiciuokle_web/media;
                        }

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

My static files after collectstatic command are in subfolders named css; img; js in parent folder /opt/myenv/static. For testing purposes I also copied main css file to parent folder. But despite various combinations I tried I got no result.

My related question (with currently no answer in stackowerflow): https://stackoverflow.com/questions/58200226/django-nginx-gunicorn-not-serving-static-files?noredirect=1#comment102783159_58200226

P.S. I am sorry for not using format, but for some reason format buttons are un-clickable to me.


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.

Bobby Iliev
Site Moderator
Site Moderator badge
October 3, 2019
Accepted Answer

Hello,

It looks like that your static files are served just as expected:

stvskaiciuokles.eu/static/css/style.css

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.

Hope that this helps! Regards, Bobby

Try DigitalOcean for free

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

Sign up

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

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.