Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
1  
Logs. Always look at the logs. –  womble Aug 27 '11 at 6:06

2 Answers 2

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;
    }
}
share|improve this answer
    
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? –  user59450 Nov 7 '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. –  user59450 Nov 7 '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 7 '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 –  user59450 Nov 7 '10 at 19:14
    
Couldn't someone using this method theoretically request say settings.py? Unless of course your django files are somewhere other than brouwers. –  wilbbe01 May 9 '11 at 5:55

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;
    }

}

share|improve this answer

Your Answer

 
discard

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