The ngx_http_image_filter_module module of Nginx (nginx version 0.7. 54 +) can be used to dynamically convert picture sizes in JPEG, GIF, PNG, and WebP formats.
This module is not built by default and needs to be enabled with the —with-http_image_filter_module configuration parameter.
You can use this module if the number of pictures accessed is not large.
This module uses libgd library. It is recommended to use the latest version of the library.
Here are the steps to add this module when nginx is already installed.
1 Installation dependencies
yum -y install gd-devel
yum -y install libxslt-devel
yum -y install perl-devel perl-ExtUtils-Embed
2 View the original nginx parameters
nginx -V
The output is as follows:
nginx version: nginx/1.12.2 configure arguments:
Check to see if there is—with-http_image_filter_module, if there is, it means it has been installed, otherwise continue installation.
3 Compile
When recompiling, you need to download the same version of nginx source code and decompress it.
Add —with-http_image_filter_module=dynamic and the original parameters (reinstall without executing the make install command)
./configure --with-http_image_filter_module=dynamic
make
4 Back up the original nginx
cp -r /usr/local/nginx /usr/local/nginxbak
5 Covering nginx
cp objs/nginx /usr/local/nginx/sbin/
mkdir /usr/local/nginx/module
cp objs/ngx_http_image_filter_module.so /usr/local/nginx/module/ngx_http_image_filter_module.so
6 Modify the configuration file
vim /usr/local/nginx/conf/nginx.conf
load_module /usr/local/nginx/module/ngx_http_image_filter_module.so;
location ~* /images/(.+)_(\d+)x(\d+)\.(jpg|jpeg|gif|png)$ {
set $w $2;
set $h $3;
image_filter resize $w $h;
image_filter_buffer 30M; # Set the maximum size of the picture buffer. If the size exceeds the set value, the server will return an error 415
image_filter_interlace on;
image_filter_jpeg_quality 80;
try_files /$1.$4 /notfound.jpg;
expires 1d;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
alias /home/publisherupload/ ; #root Can make access /images/ Path, access the /User/XX/Desktop/images If the root Replace with alias Is accessed /User/XX/Desktop/ ; Among them /User/XX/Desktop/ Is the absolute path in this machine.
}
location /images {
alias /home/publisherupload/ ; #root Can make access /images/ Path, access the /User/XX/Desktop/images If the root Replace with alias Is accessed /User/XX/Desktop/ ; Among them /User/XX/Desktop/ Is the absolute path in this machine.
autoindex on; # Turn on directory browsing
}
7 Overload nginx Configuration
/usr/local/nginx/sbin/nginx -s
8 Test
The final picture access address is to add _ width X height parameter after the original picture address
http://110.87.103.58:12345/images/2021-01/202101250944272\_500x400.png