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

Nginx showing 404 Not Found instead of protected media #10

Closed
pitagora04 opened this issue Mar 21, 2022 · 1 comment
Closed

Nginx showing 404 Not Found instead of protected media #10

pitagora04 opened this issue Mar 21, 2022 · 1 comment

Comments

@pitagora04
Copy link

I'm getting Nginx 404 Not Found instead of protected media which is image in my case.
Kindly asking for a help because I don't understand what I am doing wrong.

In settings.py I have this:

PROTECTED_MEDIA_ROOT = os.path.join(BASE_DIR, 'protected')
PROTECTED_MEDIA_URL = "/protected"
PROTECTED_MEDIA_SERVER = "nginx"  # Defaults to "django"
PROTECTED_MEDIA_LOCATION_PREFIX = "/internal" # Prefix used in nginx config
PROTECTED_MEDIA_AS_DOWNLOADS = False  # Controls inclusion of a Content-Disposition header

In django I have this:

@login_required
def my_media_access(request, media_object, file_relpath):
    # some code to check if access is granted
    if access_granted:
        return protected_view(request, file_relpath, server = PROTECTED_MEDIA_SERVER, as_download = PROTECTED_MEDIA_AS_DOWNLOADS)
    return HttpResponseForbidden('Not authorized to access this media.')

In Nginx configuration I have:

    location /internal {
        internal;
        root /home/myuser/myproject/protected;
    }

So the files are in folder /home/myuser/myproject/protected.
Idea is that when you access URL https://example.com/protected/abc/image/4813-image1.png then Django recognizes this path and calls protected_view in django-protected-media.

This are the test results for logged in user which is allowed to see asset:

  • opening https://example.com/protected/abc/image/4813-image1.png - 404 Not Found (nginx) which is NOT OK, image should be shown
  • opening https://example.com/internal/abc/image/4813-image1.png - 404 Not Found (nginx) which is OK
  • opening https://example.com/protected/abc/image/4813-image1_WRONG_PATH.png - Django message that there is no route which is OK
@pitagora04
Copy link
Author

My fault.
I have to use alias instead of root - see explanation HERE.

Solution in my case was to change NGINX configuration to:

    location /internal/ {
        internal;
        alias /home/myuser/myproject/protected/;
    }

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

1 participant