Nginx + Gunicorn + Django - nginx not allowing static files

7,156

This answer has been edited to summarize the solutions.

In the configuration of nginx

Replacing:

alias /home/ubuntu/virtualenv/mysite/homelaunch/;

with:

root /home/ubuntu/virtualenv/mysite/homelaunch/;

See the documentation for more details.

Replacing:

proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;

with:

proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

In gunicorn's config

Replacing:

bind = 'ec2-x-x-x-x.compute-1.amazonaws.com:8001'

with:

bind = '127.0.0.1:8001'

In django's settings

Setting STATIC_ROOT = '/home/ubuntu/virtualenv/mysite/homelaunch/static/' which is the correct absolute filesystem path.

Setting DEBUG = True to have more detailed error information.

Share:
7,156

Related videos on Youtube

user700070
Author by

user700070

Updated on September 18, 2022

Comments

  • user700070
    user700070 over 1 year

    Updated Code - this is still not working though.

    Settings.py

    STATIC_ROOT = '/home/ubuntu/virtualenv/mysite/homelaunch/static/'
    STATIC_URL = '/static/'
    

    nginx conf :: /etc/nginx/sites-enabled/mysite

    server {
            server_name ec2-x-x-x-x.compute-1.amazonaws.com;
            access_log /home/ubuntu/virtualenv/mysite/error/access.log;
            error_log /home/ubuntu/virtualenv/mysite/error/error.log warn;
            connection_pool_size 2048;
    
            location /static/ {
                #alias /home/ubuntu/virtualenv/mysite/homelaunch/static/;
                #alias /static/;
                root /home/ubuntu/virtualenv/mysite/homelaunch/;
            }
    
            location / {
                proxy_pass http://127.0.0.1:8001;
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                #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"';
            }
        }
    

    gunicorn config -

    command = '/usr/local/bin/gunicorn'
    logfile = "/home/ubuntu/virtualenv/mysite/error/gunicorn.log"
    loglevel = "info"
    pythonpath = '/home/ubuntu/virtualenv/mysite'
    bind = '127.0.0.1:8001'
    

    full path to img dir and css dir:

    /home/ubuntu/virtualenv/mysite/homelaunch/static/css
    /home/ubuntu/virtualenv/mysite/homelaunch/static/img
    

    Receiving an error in the error.log:

    001/favicon.ico", host: "ec2-xx-xx-xx-xx.compute-1.amazonaws.com"
    2013/09/02 16:57:42 [error] 2819#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: ec2-xx-xx-xx-xx.compute-1.amazonaws.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "ec2-xx-xx-xx-xx.compute-1.amazonaws.com"
    

    Anything that is glaringly wrong? I'm still getting a 404 error on both img and css files when I try to view them through the outputted html source.

    • Changaco
      Changaco over 10 years
      The configuration seems fine to me, does the file /home/ubuntu/virtualenv/mysite/homelaunch/img/templated/base‌​/LogoV1.png actually exist ?
    • user700070
      user700070 over 10 years
      No it doesn't. /home/ubuntu/virtualenv/mysite/homelaunch/static/img/templat‌​ed/base/LogoV1.png does . This is missing the static dir after homelaunch dir . So what do I have to modify? If you could please specify in an answer I will try it out
    • user700070
      user700070 over 10 years
      Does STATIC_ROOT = '/mysite/homelaunch/' have to change to: STATIC_ROOT = '/mysite/homelaunch/static/' ?
    • user700070
      user700070 over 10 years
      additionally, i tried this as well ^^ and it didnt fix anything. It ALWAYS loads the template, but will not load the static files: img, css, etc .
    • user700070
      user700070 over 10 years
      Okay, I fixed that. Code updated. it seems to be definitely throwing a http404 error now when i click the on css link within the html source outputted - it didnt before, butStill no images or css working though. I also verified that '/home/ubuntu/virtualenv/mysite/homelaunch/static/' is accessible and it is. Any thought?
    • user700070
      user700070 over 10 years
      I'm getting an error in error.log as specified in nginx config of: 2013/09/01 03:33:37 [alert] 2689#0: accept4() failed (24: Too many open files) - any idea? . I also just turned on DEBUG in settings.py - nothing new at least on output
    • user700070
      user700070 over 10 years
      This error is potentially unrelated to the problem i am having
    • user700070
      user700070 over 10 years
      DEBUG is turned on. I get the basic 404 page served by django. It throws an error with the urls.py . It says it can't find a path to use for ec2-x-x-x-x.compute-1.amazonaws.com:8001/static/img/template‌​d/… , do I have to specify a regex for all of these directories, like: /img/templated/base/, /img/templated/home in urls.py? To serve all of the directories for static files?
    • user700070
      user700070 over 10 years
      am I missing any required settings on nginx that you can think of?
  • user700070
    user700070 over 10 years
    yes and gunicorn
  • user700070
    user700070 over 10 years
    yes it does ! This is so weird ha
  • Skamasle
    Skamasle over 10 years
    Not is the same, because on django he have defindes satatic files on /static/ so if change alias for correct dir stil work, he have alias defines for orther dir not for statics: location /static/ { alias /home/ubuntu/virtualenv/mysite/homelaunch/; } So this solution is if he have their statics on /home/ubuntu/virtualenv/mysite/homelaunch/statics
  • user700070
    user700070 over 10 years
    @Skamasle - please see updated code and give your thoughts. I would really appreciate it
  • Skamasle
    Skamasle over 10 years
    So and now working ? May point is only than you need define the alias on same dir when you have statics.., so I hope that works if not give us some lines of your error logs, and your dir distribution so what are on /home/ubuntu/virtualenv/mysite/homelaunch/ and on /home/ubuntu/virtualenv/mysite/ and on what dir you have your statics etc I have a django setup for some of my clients whit similar alias, and work very well.
  • user700070
    user700070 over 10 years
    @Skamasle - what do you need from me?
  • user700070
    user700070 over 10 years
    Thank you very much for the reply! I tried testing this, but nothing has changed the img and css are not appearing. I updated my question with even more detail - could you see if you see if anything is wrong ? I'm going to try look at the documentation some more myself. Thank you for your help thus far !
  • user700070
    user700070 over 10 years
    gunicorn conf is updated above - now my site isn't loading lol.
  • user700070
    user700070 over 10 years
    Does this tell you anything?
  • Changaco
    Changaco over 10 years
    Once you change gunicorn's bind value you have to use the right URL: http://ec2-x-x-x-x.compute-1.amazonaws.com/ not http://ec2-x-x-x-x.compute-1.amazonaws.com:8001/
  • Skamasle
    Skamasle over 10 years
    So you still have the problem whit images ? give us your error log, last lines on paste bin, and give us also path dir when you have your static content, your images or template.
  • user700070
    user700070 over 10 years
    DUDE!!!!!!! This is working now. You are da-bomb! Can I treat you to a starbucks?