Nihal Sharma
Nihal Sharma

Reputation: 2437

Nginx looking for a different path than what is given Django static files url setting

I ran into very weird issue for which I could not find a reason. I have a django app with uWSGI as my app server and Nginx as our reverse proxy. My initial setting for the static url in Django are as below:

PROJECT_DIR = "/home/ubuntu/src/myapp"

MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'staticfiles')
STATIC_URL = '/staticfiles/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'static'),
)

and what's there in the nginx conf is as below:

server {
    listen      80;
    server_name  stg1.myapp.com

    client_max_body_size 5G;   # adjust to taste

    access_log /var/log/nginx/myapp-access.log combined;
    error_log /var/log/nginx/myapp-error.log;

    location /media  {
        alias /home/ubuntu/src/myapp/media/;  # your Django project's media files - amend as required
    }

    location /static {
        alias /home/ubuntu/src/myapp/staticfiles/;
    }
    location / {
        uwsgi_pass  django;
        uwsgi_read_timeout 600s;
        uwsgi_buffering off;
        uwsgi_send_timeout 600s;
        proxy_read_timeout 600s;
        include /home/ubuntu/src/myapp/uwsgi_params;

    }
}

Now, when I was trying to access the server I was getting this error for the static files:

2016/12/13 20:33:03 [error] 30533#0: *194 open() "/home/ubuntu/src/myapp/staticfiles/files/css/base.css" failed (2: No such file or directory), client: 172.31.4.166, server: stg.myapp.com, request: "GET /staticfiles/css/base.css HTTP/1.1", host: "stg.myapp.com", referrer: "http://stg.myapp.com/profile/user1"

Collectstatic copied all the files in the given STATIC_ROOT location. But the path actually searched was - STATIC_ROOT/files.

When I changed the STATIC_ROOT to

STATIC_ROOT = os.path.join(PROJECT_DIR, 'staticfiles/files')

it worked. I am still clueless about why the directory staticfiles/files was being looked for and not just the staticfiles directory as given in nginx conf. Where should I be looking for a possible reason?

EDIT - I had restarted nginx service whenever there was a change done there. So no issues there.

Upvotes: 0

Views: 561

Answers (2)

Nihal Sharma
Nihal Sharma

Reputation: 2437

I found the reason.

STATIC_URL = '/staticfiles/' is creating issue. It has to be

STATIC_URL = '/static/'.

The earlier one appends the files in the path on request which was causing my issue.

Upvotes: 1

shady
shady

Reputation: 455

Cannot find the directory files anywhere in your first set up. [I have not privileges to comment yet].


The set up looks good for me, maybe you just forget to restart Nginx when you modified the configuration file.

Refer link at STATIC_ROOT in Django on Server

Also Static Root and Static Url confusion in Django

Upvotes: 0

Related Questions