Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document integration of ObjectDownloadView with NGINX acceleration #177

Open
johnthagen opened this issue Jan 26, 2021 · 12 comments
Open

Document integration of ObjectDownloadView with NGINX acceleration #177

johnthagen opened this issue Jan 26, 2021 · 12 comments

Comments

@johnthagen
Copy link
Contributor

Currently, there is not an example/documentation for how to integrate ObjectDownloadView with NGINX XAccelRedirectMiddleware. This would be a helpful example for new users.

@johnthagen
Copy link
Contributor Author

@Natim I'm currently looking into this and will submit a PR if/when I figure it out. Do you have any recommendations for this issue?

@johnthagen
Copy link
Contributor Author

johnthagen commented Feb 2, 2021

Specifically, the type of View I am trying to accelerate is:

#: Serve ``file`` attribute of ``Document`` model.
default_file_view = ObjectDownloadView.as_view(model=Document)

urlpatterns = [
re_path(
r"^default-file/(?P<slug>[a-zA-Z0-9_-]+)/$",
views.default_file_view,
name="default_file",
),

@johnthagen
Copy link
Contributor Author

johnthagen commented Feb 2, 2021

As a note, ObjectDownloadView is listed as supported for acceleration in the docs: https://django-downloadview.readthedocs.io/en/latest/optimizations/index.html#supported-features-grid

@ghost
Copy link

ghost commented May 5, 2021

@johnthagen Did you manage to get it working?

@johnthagen
Copy link
Contributor Author

@amarandon I did not. The challenge I had was that by using object primary keys, I couldn't find a corollary to how to translate those into something to pass NGINX.

I think this would be a very important addition to this library, but after trying for some time I could not figure it out. I do hope that someone is able to and can share their findings with the community.

@johnthagen
Copy link
Contributor Author

@Natim Do you have any insights or directions someone could take who is trying to discover how to do this? Is this something that is intended to be supported?

@Natim
Copy link
Collaborator

Natim commented May 5, 2021

@ghost
Copy link

ghost commented May 5, 2021

@Natim So if I understand correctly ObjectDownloadView should add the correct header out of the box and we only need to configure nginx to make use of it? I just had a look in browser's devtools and that doesn't seem to be the case 🤔

@Natim
Copy link
Collaborator

Natim commented May 5, 2021

If I understand the code correctly it would work like that if you configure the XAccelMiddleware to monitor the path that your ObjectResponse will use.

@ghost
Copy link

ghost commented May 5, 2021

@Natim What confuses me is what to put in DOWNLOADVIEW_RULES and how it relates to Django URLs configuration and Nginx configuration.

I have a Photo model like this:

class Photo(models.Model):
    file = models.ImageField(upload_to='uploads/%Y/%m/%d/')

And an ObjectDownloadView configured in urls.py like this:

path('photos/<int:pk>', ObjectDownloadView.as_view(model=Photo), name='photo_file'),

This works as expected: files are served through the Python view.

Now in settings.py I've added django_downloadview.SmartDownloadMiddleware at the end of my MIDDLEWARE lists. I've enabled the nginx backend with DOWNLOADVIEW_BACKEND = "django_downloadview.nginx.XAccelRedirectMiddleware" but now I'm confused as to how I should configure DOWNLOADVIEW_RULES. I tried this:

DOWNLOADVIEW_RULES = [
    {
        'source_url': '/photos/',
        'destination_url': '/uploads/',
    },
]

Along with this entry in nginx config:

location /uploads/ {
      internal;
      # /home/myuser/myproject contains a directory named 'uploads' which contains uploaded photos
      alias /home/myuser/myproject;
}

But this doesn't seem to change anything. In fact I'm not even sure of how to verify that it works. I was kind of expecting a line in nginx log files saying it did the internal redirection but I'm not sure if this expectation is reasonable.

@Natim
Copy link
Collaborator

Natim commented May 5, 2021

I think the issue is with

DOWNLOADVIEW_RULES = [
    {
        'source_url': '/photos/',
        'destination_url': '/uploads/',
    },
]

can you try:

DOWNLOADVIEW_RULES = ['/uploads']

@benoitbryon
Copy link
Collaborator

Disclaimer: I had a very quick look at this thread and I've not been coding with Django for some time now...

I haven't figured out where is the issue at the moment.

I'm not even sure of how to verify that it works

As far as I remember, in order to debug, you can:

  • disable X-Accel backend in Django settings and check that files are served. You did that already if I understood well.
  • enable X-Accel backend in Django settings but comment /uploads/ section in Nginx configuration, then check HTTP response.
    • If response does have X-Accel-Redirect header, then it looks like Nginx didn't catch this header (or the value of the header), so check Nginx configuration.
    • If response doesn't have X-Accel-Redirect header, then it looks like the middleware didn't catch the URL as expected. So check Django configuration.

There is also some assert_x_accel_redirect() utility to create tests. It may be useful to check that middleware catches the response and transforms it to X-Accel as expected. See https://django-downloadview.readthedocs.io/en/latest/optimizations/nginx.html#test-responses-with-assert-x-accel-redirect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants