我正在尝试使用Nginx为用户提供文件,后来当我首先发现此问题时,然后使用受保护的url提供文件。现在,我有一些代码可以指定用户要下载的文件的路径。但是,我的浏览器返回File not found
。
我的观点
def download_item(request, pk):
item = get_object_or_404(Usertasks, pk=pk)
output_path = item.OutputPath
response = HttpResponse()
print(output_path)
response["Content-Disposition"] = "attachment; filename={0}".format(output_path)
response['X-Accel-Redirect'] = output_path
return response
我的终端打印了
未找到: /home/django/copypaste/cleanup/var/media/admin/output/123.txt
此路径正确,文件名为123.txt
(出于测试目的)
启用Nginx网站的配置
# mysite_nginx.conf
# the upstream component nginx needs to connect to
upstream django {
server unix:///home/django/copypaste/cleanup/usegolem.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name 127.0.0.1; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 250M; # adjust to taste
location /static {
alias /home/django/copypaste/cleanup/static/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/django/copypaste/cleanup/uwsgi_params; # the uwsgi_params file you installed
}
location /media {
internal;
root /home/django/copypaste/cleanup/var;
}
}
当用户单击下载时,它还会重定向到如下所示的下载网址。
path('download/<int:pk>/', views.download_item, name='item-download'),
是否会因为Nginx想要转到该URL而不是文件的网络服务器路径而导致问题?