3

Ok so i wanted to give nginx a try and while setuping my Django app through FastCGI i encountered a issue. FastCGI is running ok but static files give me 404. Here is my config:

server {
 listen   80;
 server_name  localhost;
 access_log  /var/log/nginx/default.access.log;


 location /static/public {
  autoindex on;
  index index.html;
  root /home/daniels/djangoapp/public;
 } 

 location / {
  include /etc/nginx/fastcgi_params;
  fastcgi_pass unix:/home/daniels/djangoapp/djangoapp.sock;
 }

 # redirect server error pages to the static page /50x.html
 #
 #error_page   500 502 503 504  /50x.html;
 #location = /50x.html {
 # root   /var/www/nginx-default;
 #}
}

Anything that i try to access as http://127.0.0.1/static/public/ gives a 404
eg: http://127.0.0.1/static/public/css/sytle.css which is at /home/daniels/djangoapp/public/css/style.css gives 404

Any idea what's wrong?

LE:

2010/02/20 22:31:42 [error] 3411#0: *3 open() "/home/daniels/djangoapp/public/img/static/public/img/background.jpg" failed (2: No such file or directory), client: 10.0.2.2, server: localhost, request: "GET /static/public/img/background.jpg HTTP/1.1", host: "127.0.0.1"

So it seems that it appends the /static/public part from location directive to the root

LE2:
Ok, i'm really sorry about the typos, but this was made when i copy & pasted the log and edited the name of the app. What seems to happen is that for /static/public i have root set to /home/daniels/djangoapp/public so for http://127.0.0.1/static/public/test.jpg i was expecting nginx to look for the file at /home/daniels/djangoapp/public/test.jpg but instead it looks for it at /home/daniels/djangoapp/public/static/public/test.jpg
It seems that it appends an extra /static/public to the path.

1
  • "/home/daniels/djnagoapp/public/img/static/public/img/background.jpg" Is djnagoapp intentional typo? Feb 21, 2010 at 12:05

4 Answers 4

12

Just fixed using this:

location /static/public/ {
    alias /home/daniels/neasoferul/public/;
}
4
  • 4
    Thanks, I was using root as well and took me about an hour to figure out what was going on.
    – xamox
    Jan 20, 2012 at 20:14
  • Ran into the same issue as xamox. :) Dec 3, 2014 at 18:28
  • How can I set a list of alias in one location section?
    – Amintabar
    Oct 4, 2018 at 5:32
  • Thank you! It's some kind of magic... Nov 15, 2021 at 13:13
1

I think Nginx associates rules in reverse order: try putting the /static/public part underneath the / rule.

I have this config, which works fine:

# proxy to Apache 2 and mod_python
location / {
    proxy_set_header    Host             $host;
    proxy_set_header    X-Real-IP        $remote_addr;
    proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;

    proxy_pass http://wsgi;
}
# catch any weird erroneous media locations - it's a sub domain
location /media/ {
    root   /var/www/sites/mysite/static;
}
1
  • 2
    It doesn't. It uses the most specific location block. If you have location /media and location /media/images and a request for /media/images/logo.png is received then it will always use /media/images, regardless of the order. Feb 21, 2010 at 17:26
0

Assuming you didn't actually type in sytle.css in the URL but rather style.css then enable the error log: http://wiki.nginx.org/NginxHttpMainModule#error_log and see which path it's trying to access.

0

You should put your static content in its own subdomain. It's easier to manage and expand later.

1
  • 1
    Agree on that, but in this particular case i have to server the whole stuff just by ip, no domain name available.
    – daniels
    Feb 20, 2010 at 19:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .