i have a page where the user has static files (pdf) and images (created from the pdf on)
there is a structure like
/files/user1/img/foo.jpg
/files/user1/pdf/foo.pdf
/files/user2/img/bar.jpg
/files/user2/pdf/bar.pdf
...
At the moment i deliver these images and pdf files with a php script which checks if user1 is authenticated (with something like php?img=user1.jpg) but this is slow.
I want do serve it directly with nginx but it needs some sort of authentification to insure that only the logged in user1 can read files from user1 etc.
There is a PHP login done with codeigniter4, is there a way to let nginx at the login with php know that is allowed to server the files to user1?
Thanks for helping.
Best regards
Alex
2
Answers
You can use the
X-Accel-Redirect
feature of Nginx.Yes, you can use X-Accel-Redirect. first check if the user is authenticated and authorized to access the file, then if the user is allowed, send an X-Accel-Redirect header pointing to an internal location. Configure Nginx to map that internal location to the actual file path.
nginx configs could be like this:
and a sample for php code: