为了扩展先前的答案,你应该能够修改以下代码,并让nginx直接提供你的下载文件,同时仍然保护文件。
首先添加一个位置,例如:
location /files/ { alias /true/path/to/mp3/files/; internal;}到你的nginx.conf文件(内部无法直接访问)。然后,你需要一个类似Django的视图:
def song_download(request, song_id): try: song = Song.objects.get(id=song_id) response = HttpResponse() response['Content-Type'] = 'application/mp3' response['X-Accel-Redirect'] = '/files/' + song.filename response['Content-Disposition'] = 'attachment;filename=' + song.filename except Exception: raise Http404 return response
这会将文件下载交给nginx。



