Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I setup a Trusty VM with Django + Nginx (other stuffs too).

The server is working, I get the "Welcome to Django" page. But when I enter to servername/admin it loads the HTML page but fails to load the static content.

And the admin page have this links to static content:

<link rel="stylesheet" type="text/css" href="/static/admin/css/base.css" />
<link rel="stylesheet" type="text/css" href="/static/admin/css/login.css" />

Both of the CSS files give me 404, as the Nginx log shows:

192.168.56.1 - - [05/Jun/2014:12:04:09 -0300] "GET /admin HTTP/1.1" 301 5 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"
192.168.56.1 - - [05/Jun/2014:12:04:09 -0300] "GET /admin/ HTTP/1.1" 200 833 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"
192.168.56.1 - - [05/Jun/2014:12:04:10 -0300] "GET /static/admin/css/base.css HTTP/1.1" 404 142 "http://ubuntu-server/admin/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"
192.168.56.1 - - [05/Jun/2014:12:04:10 -0300] "GET /static/admin/css/login.css HTTP/1.1" 404 142 "http://ubuntu-server/admin/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0"

I think that the error is on my nginx.conf file, but no idea how to solve it. This is the site tree:

screenshoot

Major files: urls.py, settings.py.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted
+100

This is most likely a problem of configuration in django or Nginx

There are two things you want to check:

  1. Are your staticfiles configured correctly in settings.py? For the basic django page, these settings are not necessary, but for your static files, you need to configure static files. That is not necessary for production use, because there the webserver serves the static files. Are they collected? Once configured you might have to run python manage.py collectstatic.

    For instance,

    #settings.py
    INSTALLED_APPS += [django.contrib.staticfiles,]
    STATIC_URL = '/static/'
    STATIC_ROOT = '/your_path/.../static/'
    
    # This collects all files in /static/ directories and puts them in the static ROOT folder.
    python manage.py collectstatic
    
  2. Django does not serve static files by itself, thus you will have to add a configuration in engine x to make it serve the static files. You might have to restart nginx and/or add autoindex on; to your config for /static/ in nginx.conf.

See Deploying static files.

In development, you might also have to add the /static/ url to your urls.py (in development) and not just the settings.

share|improve this answer
    
I tried with the three methods, none worked. I made modification to nginx.conf and then restart the service, but still. Tried also adding the following line into urls.py but no luck.. url(r'^static/', include(admin.site.urls)), –  Lucio Jun 12 at 23:54
    
Can you post your django settings, your main urls.py and the output of python manage.py collectstatic? –  don.joey Jun 13 at 8:38
    
After reading the output of that command, I noted that I wrote a wrong path. Now it worked! Without autoindex the directory. What I do not understand is, should I do this on production? –  Lucio Jun 13 at 14:51
1  
Lucio, you should indeed collect static files on production. Or at least, I do it on production as part of the deployment process (using fabric) so as to make sure I really have all the static files (also those in a new version of an old app, for instance). –  don.joey Jun 13 at 15:06
add comment

I'm guessing you have the wrong file paths. According to what the server is saying the admin and static folders are in the same path.

Also on Linux / is the root path. Your files aren't in the root directory.

Again I am just guessing, hope that this helps!

share|improve this answer
    
What files and why should be in the root directory? I don't see where the server is saying that admin and static folders are in the same path. –  Lucio Jun 11 at 3:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.