Serving media files with Django/Apache/Nginx

478 views
Skip to first unread message

Corey

unread,
Jun 1, 2010, 5:40:10 PM6/1/10
to Django users
I am a Django newbie and Im using Django to build an application. Im
using Apache/mod_wsgi and Nginx as a proxy server to serve static
files.I am running Nginx on port 80 and have all the Django pages
forwarded to Apache. I have my media folder in the application root
folder named media1. I cannot get Nginx to find and serve up my media
files. Here is the different pieces i have.

Nginx config:

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;

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_max_temp_file_size 0;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location ~* ^.+.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|
exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root /media1/;
}


Django SETTINGS config:

MEDIA_ROOT = 'C:/Python26/Lib/site-packages/django/bin/blazinsports/
media1/'
MEDIA_URL = 'http://localhost/media1/'
ADMIN_MEDIA_PREFIX = '/media/'

What do I need to change to get Nginx to serve up my media files??
Thanks for your help!!

Daniel Roseman

unread,
Jun 1, 2010, 5:55:42 PM6/1/10
to Django users
This isn't a Django question at all, but purely an nginx one. I don't
know nginx at all, but it's clear from the config file above that you
have never told it where to look for the files to serve from /media1/
- ie where is the actual physical location on the server.

(Not related, but it is an extremely bad idea to put your own Django
project - and its media files - within the site-packages directory.)
--
DR.

Vasil Vangelovski

unread,
Jun 1, 2010, 5:57:13 PM6/1/10
to django...@googlegroups.com
Using the alias directive is much more easier and straighforward, for
me at least. Here's an example configuration

upstream django-backend
{
server testsite.local:4080;
}


server {
listen 80;
server_name testsite.com;

location /{
proxy_redirect http://testsite.local:4080/ http://$host/;
proxy_set_header X-REAL-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 5;
proxy_send_timeout 15;
proxy_read_timeout 60;
proxy_set_header Host "testsite.local:4080";
proxy_pass http://django-backend;

}

location /static {
alias /home/vasil/Projects/django-test/djangoproject/static;
}
}

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

Reply all
Reply to author
Forward
0 new messages