Reputation: 43
I'm trying to serve static images from a server. The path where images are being saved to is generated using a function I wrote for mass image storing.
Essentially, the image file name becomes the hash of the image index in the database. For example, the 1st image filename would be 356a192b7913b04c54574d18c28d46e6395428ab.
The path is then extracted from the filename based on the first four characters of the hash. For filename above, the path would be /static_root/35/61/356a192b7913b04c54574d18c28d46e6395428ab.
The issue is that when a client requests for a particular file (ie /images/356a192b7913b04c54574d18c28d46e6395428ab), the path to that image still needs to be deconstructed. I want to use Nginx to take some load off of my Django application, but the function to decode/encode file paths is written in python. How should I be transforming the url and serving the image?
Upvotes: 0
Views: 126
Reputation: 4208
You can use X-Accel-Redirect
header to serve the file with Nginx.
Basically you provide the path to the file and Nginx will serve that file on the same URL.
def your_view(request):
response = HttpResponse()
response['X-Accel-Redirect'] = "path/to/image.png"
return response
Take look here: Django Nginx X-Accel-Redirect for protected files on Webfaction or https://www.djangosnippets.org/snippets/491/ for detailed example.
Upvotes: 2