Nginx图片压缩

本文将介绍怎样使用 nginx 去压缩图片,nginx 将利用 ngx_http_image_filter_module 模块去压缩图片。

在网站中,会存在大量的图片(例如:电商网站),一个页面可能就有几十张图片,而且这些图片都是使用单反相机拍摄。每张图片的大小也非常大,一般有几兆。因此,导致网页加载非常慢,此时我们需要压缩这些图片。下面将介绍怎样使用 nginx 来开启压缩图片。

实例:我们通过chrome浏览器访问nginx上面的图片,设置chrome浏览器网络速度为 10kb 的下载速度。此时,打开图片,你会明显看见图片要很久才能加载完成。如下:

Nginx图片压缩

nginx 要开启图片压缩,需要启用 ngx_http_image_filter_module 模块。默认该模块是不会编译到 nginx 的,需要使用 --with-http_image_filter_module=dynamic 参数去开启。该参数表示开启图片模块,且是动态引入。而不是直接将 nginx 和 ngx_http_image_filter_module 源码编译成一个 nginx 二进制可执行文件。

(1)查看当前自己的 nginx 是否编译过图片模块,如下:

[root@S0 nginx]# ./sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
configure arguments:
[root@S0 nginx]#

(2)手动将 ngx_http_image_filter_module  模块编译到 nginx,如下:

[root@S0 nginx-1.16.1]# ./configure --with-http_image_filter_module=dynamic
checking for OS
 + Linux 2.6.32-358.el6.i686 i686
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
...
checking for GD library in /opt/local/ ... not found

./configure: error: the HTTP image filter module requires the GD library.
You can either do not enable the module or install the libraries.

错误:缺少 GD library 库文件,执行 yum install gd gd-devel 命令安装 gd 库。如下:

[root@S0 nginx-1.16.1]# yum install gd gd-devel
Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
 ...

然后,再次运行 ./configure --with-http_image_filter_module=dynamic 去编译 nginx。如下:

# 预编译
[root@S0 nginx-1.16.1]# ./configure --with-http_image_filter_module=dynamic

# 编译 nginx
[root@S0 nginx-1.16.1]# make

(3)进入到当前的 objs 目录,你会发现目录下面多了 ngx_http_image_filter_module.so 可执行文件。如下:

[root@S0 objs]# ll
total 3512
-rw-r--r--. 1 root root   20283 Jul  7 08:13 autoconf.err
-rw-r--r--. 1 root root   41513 Jul  7 08:13 Makefile
-rwxr-xr-x. 1 root root 3355137 Jul  7 08:15 nginx
-rw-r--r--. 1 root root    5341 Jul  7 08:15 nginx.8
-rw-r--r--. 1 root root    6582 Jul  7 08:13 ngx_auto_config.h
-rw-r--r--. 1 root root     657 Jul  7 08:13 ngx_auto_headers.h
-rw-r--r--. 1 root root    1294 Jul  7 08:13 ngx_http_image_filter_module_modules.c
-rw-r--r--. 1 root root   18724 Jul  7 08:15 ngx_http_image_filter_module_modules.o
-rwxr-xr-x. 1 root root   83523 Jul  7 08:15 ngx_http_image_filter_module.so
-rw-r--r--. 1 root root    5856 Jul  7 08:13 ngx_modules.c
-rw-r--r--. 1 root root   25200 Jul  7 08:15 ngx_modules.o

(4)先将当前运行的 nginx 停止,然后将 nginx 可执行文件拷贝到 /usr/local/nginx/sbin 目录,将 ngx_http_image_filter_module.so 拷贝到 /usr/local/nginx/modules 目录。如果 /usr/local/nginx/modules 目录不存在,则创建该目录。如下:

# 创建 modules 目录
[root@S0 objs]# mkdir /usr/local/nginx/modules

# 拷贝 ngx_http_image_filter_module.so 到 /usr/local/nginx/modules/ 目录
[root@S0 objs]# cp ngx_http_image_filter_module.so /usr/local/nginx/modules/

# 将 nginx 拷贝到 /usr/local/nginx/sbin/
[root@S0 objs]# cp nginx /usr/local/nginx/sbin/
cp: overwrite `/usr/local/nginx/sbin/nginx'? y

(5)配置 nginx.conf 文件。先简单的配置,监听80端口。如下:

server {
    listen  80;
    server_name s0.example.com;
    location / {
        root html;
        index index.html;
    }
    location ~*.(gif|jpg|png|jpeg)$ {
        root html;
    }
}

引入图片压缩模块 ngx_http_image_filter_module.so,配置如下:

# 动态加载 ngx_http_image_filter_module.so 模块
load_module modules/ngx_http_image_filter_module.so;

配置图片压缩,配置如下:

server {
    listen  80;
    server_name s0.example.com;
    location / {
        root html;
        index index.html;
    }
    location ~*.(gif|jpg|png|jpeg)$ {
        root html;
        # 重新将图片大小设置为 150*100
        image_filter resize 150 100;
    }
}

此时,我们可以 https://s0.example.com/img.png 地址去访问图片,如下图:

Nginx图片压缩

图片被压缩了。完整的配置如下:

# 动态引入模块
load_module modules/ngx_http_image_filter_module.so;

user root root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen  80;
        server_name s0.example.com;
        location / {
            root html;
            index index.html;
        }
        # 拦截所有的图片
        location ~*.(gif|jpg|png|jpeg)$ {
            root html;
            # 重新设置图片的大小
            image_filter resize 150 100;
        }
    }
}
青年最主要的任务是学习。 —— 朱德
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号