Thursday, September 3, 2015

Re: How to serve any static directory?

Do you use a webserver "in front" of Django, like Nginx or Apache?

What we do:
- In *development* (when DEBUG=True), use django.views.static.serve to serve a file: https://docs.djangoproject.com/en/1.8/ref/views/#django.views.static.serve

- In *production*, let Django do what it needs to do (like authorisation, or in your case the dynamic translation of the URL to a file path) and then send headers to the webserver to let it return the file, freeing up the Django worker.

Under Nginx, we have a bit of nginx.conf in the server {} part like:

    location /protected/ {
          internal;
          alias /the/directory/root/of/the/files/
    }

And we create a path that starts with /protected/. For Apache, you simply need the full absolute path.

Then do

    response = http.HttpResponse()
    response['X-Sendfile'] = full_absolute_path  # Apache
    response['X-Accel-Redirect'] = path_starting_with_protected  # Nginx
    response['Content-Type'] = ''  # So the webserver will decide it
    return respone

Hope this helps.


On Wed, Sep 2, 2015 at 5:41 PM, Yann Salaün <yannsalaun1@gmail.com> wrote:
Hi,

The main reason I try to serve those static files with django is because I would like to use django to configure the route. 

Trying to clarify my question:

1. The url (http://<host>/site/<user-friendly-url>/) doesn't match the directory name (which could be anything, like `XYZ_html/`).
2. Administrators must be able to upload new archives, which should be extracted and served automatically. 

For those two reasons, I don't have any idea how to serve this content using the webserver, because how is it possible to configure the routes then?

Thank you for your answer

On Tue, Sep 1, 2015 at 4:43 PM, Florian Schweikert <kelvan@ist-total.org> wrote:
On 01/09/15 16:34, Yann Salaün wrote:
> In summary : is there a way to put something like `return
> serve_this_static_directory()` in the view? or is there any workaround
> there?

Not sure if I get you right, you want to serve static files with django?
Is there any reason why it's not possible to serve them with nginx/apache?

regards,
Florian

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/55E5B97B.3070607%40ist-total.org.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAHDVCz7udUn-Fo542FUwcLVz-6NSvoHh6qFLf8aGnKUOTp%3Dagw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFAGLK1bK_t5RomS0cLg2UPkXrQqoH0mkbd8tPjB_UY7z9YL-g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment