0

I've been looking and trying around all night, but without success.

I configured nginx to serve my static files and proxy all the other traffic:

server {

    listen 80;
    server_name example.com;

    access_log /home/boudewijn/www/bbt/brouwers/logs/access.log;
    error_log /home/boudewijn/www/bbt/brouwers/logs/error.log;

    location / {
            proxy_pass http://127.0.0.1:8080;
            include /etc/nginx/proxy.conf;
    }

    location /media/ {
            root    /home/boudewijn/www/bbt/brouwers/;
    }
}

The proxy passing is no problem, but when I go to example.com/media/ or try to access any testfile over there, it's without success. I paid attention to the difference between root and alias, my media folder exists, I paid attention to the trailing slashes, but still I get a 404 when trying to access my static media files.

Any help?

womble
  • 96,255
  • 29
  • 175
  • 230

2 Answers2

1

I'm assuming your media files are in /home/boudewijn/www/bbt/brouwers/media. In which case the following should work.

If you still get a 404 error then check your error log as it should show you the exact file path it's trying to access.

server {

    listen 80;
    server_name mydomain.com;

    access_log /home/boudewijn/www/bbt/brouwers/logs/access.log;
    error_log /home/boudewijn/www/bbt/brouwers/logs/error.log warn;

    root    /home/boudewijn/www/bbt/brouwers/;

    try_files $uri @proxy;

    location @proxy {
            proxy_pass http://127.0.0.1:8080;
            include /etc/nginx/proxy.conf;
    }
}
Martin Fjordvald
  • 7,749
  • 1
  • 30
  • 35
  • Couldn't someone using this method theoretically request say settings.py? Unless of course your django files are somewhere other than brouwers. – wilbbe01 May 09 '11 at 05:55
  • Sure. If you want a certain type of file to be parsed then you should specify that. – Martin Fjordvald May 10 '11 at 18:43
  • Thanks for your answer Martin, but still no success. There is nothing in mij error logs and the 404 page I'm getting is the one I made in Django, so it looks like the request to mydomain.com/media/ is getting handled by Apache... I'm new to this, so I don't really have an idea anymore, it's also a complicated situation. There's another website that is getting proxied by nginx to Apache, with DocumentRoot in /home/boudewijn/www/. Could that be the problem? –  Nov 07 '10 at 13:10
  • I'll react here so I don't have to add another answer, I know it's not the ideal place to put this, but I've posted my question as non-registered user. @shintoist: that didn't work either I've taken the easy way out and I'm serving my static files through Apache too now. The site is rather small, so it's still fast enough. Thanks for the help anyway, much apreciated. –  Nov 07 '10 at 16:07
  • Yeah with my config Nginx would not give a 404 error at all. Only thing I can say is that if Nginx does not serve the static file then your media files aren't stored in `/home/boudewijn/www/bbt/brouwers/media` – Martin Fjordvald Nov 07 '10 at 17:14
  • They are there, I have no problem serving that directory with Apache. Really strange, and definetely for a noob like me :p –  Nov 07 '10 at 19:14
0

Here's an example of a working nginx frontend, to Apache2 backend, nginx site conf for Django:

server {
    listen  80  default;
    server_name www.example.com example.com;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8001;
    }

    location /media {
        root /var/www/example-static;
    }

}

Johanna Larsson
  • 190
  • 1
  • 9