Nginx - 如何在 Django 的呈现页面上提供受保护的视频,而不是强制下载?
Nginx - how to serve protected video on a rendered page in Django, instead of a forced download?
我在 Django 呈现的页面上需要一个受保护的视频文件。该文件受到保护,但它没有像我期望的那样使用 <video src="...">
提供 html 渲染页面, 就像 netflix。 相反,我得到的只是一团乱麻,如 this image.
我知道内部重定向正在为文件提供服务,因此它会像那样显示,但我需要它与其他 html 类似 netflix 的呈现页面..我做错了什么??
Nginx 配置文件:
location /secret_videos/ {
internal;
alias /home/username/path/to/secret/videos/;
}
Url:
path('protected_video/', views.protected_video, name='protected_video'),
查看:
def protected_video(request):
....
if request.method =='POST':
if some_var == 'the_correct_value':
protected_uri = '/secret_videos/secret-vid-1.mp4'
response = render(request, 'template.html', {'some_var ': True, 'protected_uri': protected_uri})
response['X-Accel-Redirect'] = protected_uri
return response
return render(request, 'template.html', {})
模板,但没有渲染html,只有上图:
<video width="75%" height="auto" controls>
<source src="{{ protected_uri }}" type="video/mp4" />
Your browser doesn't support the mp4 video format.
</video>
您合并了两个 request/responses:呈现页面和发送视频。
您需要呈现模板,在其中为视频提供一个调用 Django 视图的 URL。然后,第二个视图 returns 以秘密 URL 作为加速重定向的响应。所以:
path('protected_video/', views.protected_video, name='protected_video'),
path('video_url/<slug: video_slug>/', views.redirect_to_video, name='redirect_to_video'),
...
def protected_video(request):
....
if request.method =='POST':
if some_var == 'the_correct_value':
protected_uri = reverse('redirect_to_video' , kwargs={'video_slug': 'some_slug'})
return render(request, 'template.html', {'some_var ': True, 'protected_uri': protected_uri})
def redirect_to_video(request, slug):
... some logic to get the secret URL from the slug ...
response = HttpResponse()
response['X-Accel-Redirect'] = secret_url
return response
我在 Django 呈现的页面上需要一个受保护的视频文件。该文件受到保护,但它没有像我期望的那样使用 <video src="...">
提供 html 渲染页面, 就像 netflix。 相反,我得到的只是一团乱麻,如 this image.
我知道内部重定向正在为文件提供服务,因此它会像那样显示,但我需要它与其他 html 类似 netflix 的呈现页面..我做错了什么??
Nginx 配置文件:
location /secret_videos/ {
internal;
alias /home/username/path/to/secret/videos/;
}
Url:
path('protected_video/', views.protected_video, name='protected_video'),
查看:
def protected_video(request):
....
if request.method =='POST':
if some_var == 'the_correct_value':
protected_uri = '/secret_videos/secret-vid-1.mp4'
response = render(request, 'template.html', {'some_var ': True, 'protected_uri': protected_uri})
response['X-Accel-Redirect'] = protected_uri
return response
return render(request, 'template.html', {})
模板,但没有渲染html,只有上图:
<video width="75%" height="auto" controls>
<source src="{{ protected_uri }}" type="video/mp4" />
Your browser doesn't support the mp4 video format.
</video>
您合并了两个 request/responses:呈现页面和发送视频。
您需要呈现模板,在其中为视频提供一个调用 Django 视图的 URL。然后,第二个视图 returns 以秘密 URL 作为加速重定向的响应。所以:
path('protected_video/', views.protected_video, name='protected_video'),
path('video_url/<slug: video_slug>/', views.redirect_to_video, name='redirect_to_video'),
...
def protected_video(request):
....
if request.method =='POST':
if some_var == 'the_correct_value':
protected_uri = reverse('redirect_to_video' , kwargs={'video_slug': 'some_slug'})
return render(request, 'template.html', {'some_var ': True, 'protected_uri': protected_uri})
def redirect_to_video(request, slug):
... some logic to get the secret URL from the slug ...
response = HttpResponse()
response['X-Accel-Redirect'] = secret_url
return response