I am building a web API using Django and Nginx that needs to support Byte-Range requests.
Currently, when making a request such as:
curl --header "Content-Type: application/json"
--header "Authorization: Token 8f25cd3bb5b43a5277f4237b1d1db0273dbe8f6a"
--request POST http://myurl/download/
--header "Range: bytes=0-50"
-v
my Django view (the MyFileAPI class) is called, the user is authenticated, and the correct file path (/code/web/myapp/user_id/user_info.pbf) is fetched. However, the entire file is returned instead of only the first 50 bytes.
How do I configure Nginx and Django to allow partial file downloads?
default.conf
server {
listen 80;
server_name localhost;
charset utf-8;
client_max_body_size 10M;
add_header Accept-Ranges bytes;
proxy_force_ranges on;
location /static/ {
alias /django_static/;
}
location / {
include uwsgi_params;
uwsgi_pass web:8000;
}
}
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
views.py
class MyFileAPI(APIView):
permission_classes = (IsAuthenticated,)
def post(self, request):
try:
user = request.user
my_file = MyFile.objects.filter(user_id=user).latest("created_date")
# ex) my_file.filepath = /myapp/13/user_info.pbf
# ex) my_files_path = /code/web/myapp/13/user_info.pbf
my_files_path = settings.BASE_DIR + my_file.filepath
response = FileResponse(open(my_files_path, 'rb'))
response['Accept-Ranges'] = 'bytes'
response['X-Accel-Buffering'] = 'no'
# Adding this next line results in a 404 error
# response['X-Accel-Redirect'] = my_files_path
return response
except Exception as e:
return Response({'status': str(e)}, content_type="application/json")
When I add the line response['X-Accel-Redirect'] = my_files_path
I receive the error The current path /myapp/13/user_info.pbf didn't match any of these.
and all of my urls from urls.py are listed.
I know there are other posts on the same topic, but none give a full answer.