0

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.

Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57

2 Answers2

1

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.

Nihal Sharma
  • 2,397
  • 11
  • 41
  • 57
0

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

Community
  • 1
  • 1
shady
  • 455
  • 1
  • 7
  • 18
  • I had tried changing the nginx conf also many a times. So had no issue with the conf reload. Also I never changed the path of static to 'actual_path/files'. – Nihal Sharma Dec 14 '16 at 04:35
  • @Nihal Sharma If you wan to access ` "/home/ubuntu/src/myapp/staticfiles/files/css/base.css"`, you need to look at the STATIC_ROOT directory, to find whether there is a file (/files/css/base.css) that exits. – shady Dec 14 '16 at 04:50
  • @ Nihal Sharma It has nothing to do with STATIC_URL, I got the same setting in my local environment, with STATIC_URL = '/staticfiles/'. Still Nginx could find the file specified by the STATIC_ROOT. – shady Dec 14 '16 at 08:19