Search code examples
djangoapachenginxstatic-files

django serve media files on both apache and nginx


I'm going into production mode for my django project, but running into a peculiar problem. I'm running my django through apahce+mod_wsgi and serving static files through nginx.

However my situation demands that I cannot serve "all" static files from nginx. There is a need to serve only "open-flash-chart.swf" from apache. The project uses openpyc and embeds open-flash-chart.swf which needs to run on same server as django, which in my case is Apache. How can I accomplish that? What changes to I need to make into Apache config files?

server {
listen   80 default;
server_name  localhost;

access_log  /var/log/nginx/localhost.access.log;

location / {
    proxy_pass http://localhost: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;
    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 /media/ {
    root /srv/www/enpass/;
    expires max;
}
}

Solution

  • In Apache, set up an alias in your virtual host to serve this file directly:

    Alias /url/to/open-flash-chart.swf /full/path/to/open-flash-chart.swf
    

    Then, instead of using {{ MEDIA_URL }} to reference the file, code in the absolute path:

    <object data="/url/to/open-flash-chart.swf" />
    

    Nginx will still proxy the request (because it's not your media path), and then Apache will deliver the file back to nginx.

    Alternatively, and not recommended, but if it must go straight from Apache to the browser, you could specify the port:

    <object data="http://servername:8080/url/to/open-flash-chart.swf" />