使用 X-Accel-Redirect 在 Nginx-Django 中下载文件
Download file in Nginx-Django with X-Accel-Redirect
我在 Nginx-Uwsgi 上有一个 Django 网页 运行。我想让用户下载超过 10GB 的文件。为此,我使用 X-Accel-Redirect
.
这个想法是用户应该去 http://XXX.XX.XX.XX/main/download
然后它可以下载文件。
经过多次配置后,现在我在浏览器上收到 403 HTTP 错误,并在 Nginx
上收到以下跟踪信息:
2021/11/11 16:11:45 [error] 29309#0: *1 open() "/home/myuser/direct/example.txt" failed (13: Permission denied), client: 2.136.173.243, server: _, request: "GET /main/download HTTP/1
我的Nginx配置如下:
events {
worker_connections 10000;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 128M;
proxy_max_temp_file_size 0;
proxy_buffering off;
server_names_hash_bucket_size 256;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /download {
internal;
alias /home/myuser/direct;
proxy_max_temp_file_size 0;
}
location /main {
uwsgi_pass django;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
}
}
我调用 /download
的 Django
函数是:
def download(request):
response = HttpResponse()
path = "/home/myuser/direct/example.txt"
name = "example.txt"
#response['Content_Type']='application/octet-stream'
#response["Content-Disposition"] = "attachment; filename={0}".format(
# name.encode('utf-8'))
response['Content-Length'] = os.path.getsize(path)
response['X-Accel-Redirect'] = "/download/{0}".format(name)
del response['Content-Type']
del response['Content-Disposition']
del response['Accept-Ranges']
del response['Set-Cookie']
del response['Cache-Control']
del response['Expires']
return response
我也曾尝试将出现在 nginx 配置中的用户更改为我的用户,以防访问 /home 时出现任何问题,但我遇到了同样的错误。
因此,我的问题是,我该如何解决这个错误,以允许 Nginx 从 /home 提供可以下载的文件?
问题是 nginx 不希望从提供的目录中读取文件。当此目录更改为 /var/wwww
时,它按预期工作。
我在 Nginx-Uwsgi 上有一个 Django 网页 运行。我想让用户下载超过 10GB 的文件。为此,我使用 X-Accel-Redirect
.
这个想法是用户应该去 http://XXX.XX.XX.XX/main/download
然后它可以下载文件。
经过多次配置后,现在我在浏览器上收到 403 HTTP 错误,并在 Nginx
上收到以下跟踪信息:
2021/11/11 16:11:45 [error] 29309#0: *1 open() "/home/myuser/direct/example.txt" failed (13: Permission denied), client: 2.136.173.243, server: _, request: "GET /main/download HTTP/1
我的Nginx配置如下:
events {
worker_connections 10000;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 128M;
proxy_max_temp_file_size 0;
proxy_buffering off;
server_names_hash_bucket_size 256;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream django {
server 127.0.0.1:8000;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /download {
internal;
alias /home/myuser/direct;
proxy_max_temp_file_size 0;
}
location /main {
uwsgi_pass django;
uwsgi_param Host $host;
uwsgi_param X-Real-IP $remote_addr;
uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
}
}
我调用 /download
的 Django
函数是:
def download(request):
response = HttpResponse()
path = "/home/myuser/direct/example.txt"
name = "example.txt"
#response['Content_Type']='application/octet-stream'
#response["Content-Disposition"] = "attachment; filename={0}".format(
# name.encode('utf-8'))
response['Content-Length'] = os.path.getsize(path)
response['X-Accel-Redirect'] = "/download/{0}".format(name)
del response['Content-Type']
del response['Content-Disposition']
del response['Accept-Ranges']
del response['Set-Cookie']
del response['Cache-Control']
del response['Expires']
return response
我也曾尝试将出现在 nginx 配置中的用户更改为我的用户,以防访问 /home 时出现任何问题,但我遇到了同样的错误。
因此,我的问题是,我该如何解决这个错误,以允许 Nginx 从 /home 提供可以下载的文件?
问题是 nginx 不希望从提供的目录中读取文件。当此目录更改为 /var/wwww
时,它按预期工作。