Nginx+PHP based on the implementation of a static file server with permission verification, such as some scenarios need to verify parameters before returning resources, or thumbnail generation.

1. Nginx configuration

Static files are stored in Download and are set to “internal”, i.e. internal access is not allowed


2. All static resource requests are typed to the PHP program and can only be accessed after permission verification

The image real storage path /download does not allow direct external access

location ^~ /download { internal; # alias optional for additional configuration the actual path, namely the download directory # alias/home/qii/imgtest/download; } # image capture can add their corresponding static resource suffix location ~ * \. (PNG | JPG jpeg | | GIF) ${# if the file does not exist, it is to rewrite the PHP script file for processing the if (! -f $request_filename) { rewrite ^/.*$ /autoimg.php; If (-f $request_filename) {expires 30d; if (-f $request_filename) {expires 30d; }}

2. PHP processing scripts

<? PHP // autoimg.php // autoimg.php // autoimg.php // autoimg.php // autoimg.php // autoimg.php // autoimg.php isset($_GET['sign'])) { exit('get img failed! '); $imagePath = $_SERVER['DOCUMENT_ROOT']. '/download/'; $image = trim(parse_url($_SERVER['REQUEST_URI'])['path'], '/'); $image = trim($_SERVER['REQUEST_URI'])['path'], '/'); / / stitching images true path Such as/home/qii/imgtest/download / 111 JPG $fullPath = $imagePath. $image; $int = getTimageSize ($fullPath)['mime']; $int = getTimageSize ($fullPath)['mime']; header("Content-Type: $mime"); // Set the Sendfile header to Nginx to "download" and send it to Nginx to process the header(" X-accel-redirect: /download/$image");

The final result

The website root directory is /home/qii/imgtest

/home/qii/ imgtest. sigma ─ autoimg.php sigma ─ download sigma ─ 111.jpg

Access path for http://localhost/111.jpg?sign access path to success http://localhost/111.jpg failed Such as direct access to the http://localhost/download/111.jpg failure, Because the Download folder can only be accessed internally

Thumbnail implementation

<? PHP // if (sign); // if (sign); isset($_GET['sign'])) { exit('get img failed! '); $imagePath = $_SERVER['DOCUMENT_ROOT']. '/download/'; $image = trim(parse_url($_SERVER['REQUEST_URI'])['path'], '/'); $image = trim($_SERVER['REQUEST_URI'])['path'], '/'); / / stitching images true path Such as/home/qii/imgtest/download / 111 JPG $fullPath = $imagePath. $image; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / the first visit, the generated thumbnail if (! File_exists ($fullPath) {file_contents ($fullPath, 'img data... '); '); } / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / get photo mime information set the content-type header $mime = getimagesize ($fullPath) [' mime]; header("Content-Type: $mime"); // Set the Sendfile header to Nginx to "download" and send it to Nginx to process the header(" X-accel-redirect: /download/$image");