nginx配置插件压缩(切)图片

1.安装和配置nginx:

参考文档:https://blog.csdn.net/yx0628/article/details/53148451

如果存在端口占用问题,请修改nginx安装目录的conf下的nginx.conf
本机nginx的安装目录:/usr/local/nginx

nginx.conf里边的配置:

 server {
        listen       80;//如果80被占用,修改80端口
        server_name  localhost;
                location / {
                               root html;
                        }
    }
wq!//保存退出

重新加载nginx如下:

[root@gitlab sbin]# ./nginx -s reload

2.下载 ngx_image_thumb-master模块

安装依赖:yum install gd-devel pcre-devel libcurl-devel

下载安装模块:https://github.com/3078825/nginx-image/archive/master.zip (ngx_image_thumb-master .zip)
解压:unzip ngx_image_thumb-master .zip

nginx的安装包的目录为:/usr/local/nginx-1.9.9,此目录为安装包目录,/usr/local/nginx为安装目录

将 ngx_image_thumb-master .zip包上传至nginx安装的包的目录下:

[root@gitlab nginx-1.9.9]# ll
总用量 700
drwxr-xr-x. 6 1001 1001   4096 7月  18 14:12 auto
-rw-r--r--. 1 1001 1001 256752 12月  9 2015 CHANGES
-rw-r--r--. 1 1001 1001 390572 12月  9 2015 CHANGES.ru
drwxr-xr-x. 2 1001 1001   4096 7月  18 14:12 conf
-rwxr-xr-x. 1 1001 1001   2481 12月  9 2015 configure
drwxr-xr-x. 4 1001 1001   4096 7月  18 14:12 contrib
drwxr-xr-x. 2 1001 1001   4096 7月  18 14:12 html
-rw-r--r--. 1 1001 1001   1397 12月  9 2015 LICENSE
-rw-r--r--. 1 root root    366 7月  18 15:13 Makefile
drwxr-xr-x. 2 1001 1001   4096 7月  18 14:12 man
drwxr-xr-x. 2 root root   4096 9月  18 2017 ngx_image_thumb-master
-rw-r--r--. 1 root root  15526 7月  26 2018 ngx_image_thumb-master.zip
drwxr-xr-x. 4 root root   4096 7月  18 15:13 objs
-rw-r--r--. 1 1001 1001     49 12月  9 2015 README
drwxr-xr-x. 9 1001 1001   4096 7月  18 14:12 src

添加参数,重新编译nginx:

添加参数前需要确认nginx的原有参数:/usr/local/nginx/sbin/nginx -V (在原有的基础上增加–add-module=ngx_image_thumb-master)

[root@gitlab nginx-1.9.9]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=ngx_image_thumb-master

--add-module=ngx_image_thumb-master //图片的
--with-http_stub_status_module --with-http_ssl_module //ssl的

//接下来make ,不能make install 这样会把原来的数据覆盖
[root@gitlab nginx-1.9.9]# make

//备份原来/user/loacl/nginx/sbin 下的nginx
[root@gitlab sbin]# mv nginx nginx_copy

//拷贝 /usr/local/nginx-1.9.9/objs/ 目录下的nginx到 /usr/local/nginx/sbin下
[root@gitlab sbin]# cd /usr/local/nginx-1.9.9/objs/
[root@gitlab objs]# ll
总用量 5460
drwxr-xr-x. 3 root root    4096 7月  18 14:14 addon
-rw-r--r--. 1 root root   14278 7月  18 15:13 autoconf.err
-rw-r--r--. 1 root root   38413 7月  18 15:13 Makefile
-rwxr-xr-x. 1 root root 5410795 7月  18 15:13 nginx
-rw-r--r--. 1 root root    5341 7月  18 15:13 nginx.8
-rw-r--r--. 1 root root    6337 7月  18 15:13 ngx_auto_config.h
-rw-r--r--. 1 root root     657 7月  18 15:13 ngx_auto_headers.h
-rw-r--r--. 1 root root    4034 7月  18 15:13 ngx_modules.c
-rw-r--r--. 1 root root   87800 7月  18 15:13 ngx_modules.o
drwxr-xr-x. 9 root root    4096 7月  18 14:12 src
[root@gitlab objs]# cp nginx /usr/local/nginx/sbin/

//修改nginx.conf(增加如下)

location / {
		 root /var/www/picture;#指定图片存放路径 
		  access_log /usr/local/nginx/logs/images_access.log;#日志存放路径   
		  image on; 
		  image_output on;
		}

//配置参数说明

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




//重新加载nginx :./nginx -s reload

浏览器中输入:ip:端口号/1.jpg
例如:http://192.168.1.24/1.jpg //原图 1.jpg这张图片是在/var/www/picture路径下存在的
     http://192.168.1.24/1.jpg!c300x300.jpg //缩略图


#有关参数:

	c参数按请求宽高比例从图片高度10%处开始截取图片,然后缩放到指定尺寸
	
	m参数按请求宽高比例从图片中截取图片,然后缩放到指定尺寸
	
	t参数按请求宽高比例缩放到指定尺寸
	
	w参数按请求宽高比例缩放到指定尺寸,空白处填充白背景颜色




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值