Question

Nginx is not serving the static files that have been collected inside the project directory.

Posted January 19, 2020 1.7k views
Nginx

I have seen others face the same issue. Some were solved with user permission, some with a problem in configuration. However, I have double-checked the configuration and user profile and can’t figure out the root of this problem.

My project folder is at the root folder and not inside the home/user directory. Its user permission and the dir structure are as follows:

drwxr-xr-x 4 bsal_dev root       4096 Jan 17 11:44 eiapp
drwxr-xr-x 3 bsal_dev root       4096 Jan 17 14:20 enginfinity
-rwxr-xr-x 1 bsal_dev root        631 Jan 16 04:56 manage.py
drwxrwxr-x 9 bsal_dev bsal_dev   4096 Jan 17 12:05 static (STATIC_ROOT) 
drwxr-xr-x 8 bsal_dev root       4096 Jan 16 04:56 statics(statics contents of the app)
drwxr-xr-x 3 bsal_dev root       4096 Jan 16 04:56 templates
drwxr-xr-x 2 bsal_dev root       4096 Jan 16 04:56 templatetags
drwxr-xr-x 4 bsal_dev root       4096 Jan 16 04:57 venv

My settings.py configuration:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')'


STATIC_URL = '/static/'
STATICFILES_DIRS = [ 
    os.path.join(BASE_DIR, 'statics'),
    ]

MY NGINX CONFIG

/etc/nginx/sites-available/enginfinity

server {
    server_name xxx.xxx.xx;

    access_log off;

    location /static/ {
        alias /enginfinity/static
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:8000;
        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.

×
1 answer

Hello,

Currently, your static files are owned by your bsal_dev, so I think that Nginx might not have the privileges to access those files.

Check what user is your Nginx service running as:

ps aux | grep -i nginx

Then get that user and set the group of the static folder to the Nginx user

chown -R bsal_dev:nginx_user /path/to/your/static_folder

If this still does not work, I would recommend checking your Nginx error log:

tail -100 /var/log/nginx/error.log

Let me know how it goes!
Regards,
Bobby

  • Hey, thank you very much. I appreciate the effort. Actually the problem was the missing ’/’ at the end of the location path of the nginx config file. Since static is the dir inside which all the static contents are, the missing / at the end of it made nginx look at the static like a file instead of a directory.

    My code was

    location /static/ {
            alias /enginfinity/static
        }
    

    which should have been

    location /static/ {
            alias /enginfinity/static/
        }
    

    A simple mistake like that. :-)

Submit an Answer