1,安装一些使用过程中会用到的工具
yum install libgd2-devel
yum install libpcre-devel
yum install libcurl-devel
yum install gd-devel
2,安装Nginx,不知道怎么安装的可以点击查看
3,下载模块源代码,将下载来的文件解压至Nginx的根目录
cd /usr/local/nginx-1.12.2/
wget https://github.com/3078825/nginx-image/archive/master.zip
unzip master.zip
4,运行nginx -V命令查看已经安装的nginx模块(configure arguments:后面表示当前已经安装的nginx模块)
[root@izbp11gsqdkmgt6b1r4kajz ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.15.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
5,配置Nginx的参数,添加图片处理模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=ngx_image_thumb-master
6,编译
make
7,备份,安装
备份之前目录下的/usr/local/nginx/sbin/nginx文件,拷贝新编译的/usr/local/nginx-1.15.0/objs/nginx到/usr/local/nginx/sbin/nginx目录下,到此缩略图模块安装完成。
8,配置nginx.conf文件
location ~*\.(jpg|png|gif){
root /home/upload/nginx/;
image on;
#image_backend off;
image_output on;
#image_jpeg_quality 75;
image_water on;
image_water_type 0;
image_water_pos 9;
image_water_file "/home/upload/nginx/logo.png";
#image_water_transparent 80;
image_backend off;
image_backend_server http://baidu.com/docs/aabbc.png; #配置一个不存在的地址,防止访问的图片不存在时响应时间过长造成nginx阻塞
}
9,配置参数说明
image on/off 是否开启缩略图功能,默认关闭
image_backend on/off 是否开启镜像服务,当开启该功能时,请求目录不存在的图片(判断原图),将自动从镜像服务器地址下载原图
image_backend_server 镜像服务器地址
image_output on/off 是否不生成图片而直接处理后输出 默认off
image_jpeg_quality 75 生成JPEG图片的质量 默认值75
image_water on/off 是否开启水印功能
image_water_type 0/1 水印类型 0:图片水印 1:文字水印
image_water_min 300 300 图片宽度 300 高度 300 的情况才添加水印
image_water_pos 0-9 水印位置 默认值9 0为随机位置,1为顶端居左,2为顶端居中,3为顶端居右,4为中部居左,5为中部居中,6为中部居右,7为底端居左,8为底端居中,9为底端居右
image_water_file 水印文件(jpg/png/gif),绝对路径或者相对路径的水印图片
image_water_transparent 水印透明度,默认20
image_water_text 水印文字 "Power By Vampire"
image_water_font_size 水印大小 默认 5
image_water_font 文字水印字体文件路径
image_water_color 水印文字颜色,默认 #000000
加粗备注:配置之后使用缩略图功能,如果图片不存在,本应返回404但是却响应很慢。这样就可以配置:
image_backend off;
image_backend_server http://baidu.com/docs/aabbc.png;
配置一个不存在的地址,是因为他代码里面的off不起作用。
GitHub原文章地址:https://github.com/oupula/ngx_image_thumb