docker nginx使用image_filter模块

1、docker下载镜像后查看是否包含image_filter模块(我当前使用的是ubuntu/nginx:latest)

2、使用运行容器后执行,如果包含:--with-http_image_filter_module这段说明镜像没问题,如果没有需要自己找镜像, 镜像地址:https://hub.docker.com/

docker exec -it nginx nginx -V

3、进入nginx容器,查看镜像中是否包含image_fiter模块包,动态模块通常位于 /usr/lib/nginx/modules/ 或 /usr/share/nginx/modules/。你可以在这些目录中查找 ngx_http_image_filter_module.so 文件。

如果有该模块跳转至第5步。

4、因为使用的是ubuntu/nginx镜像,所以使用apt-get安装模块

apt-get install libnginx-mod-http-image-filter

5、在nginx.conf中顶部添加此代码

load_module modules/ngx_http_image_filter_module.so;

6、现在就能使用了,这里粘贴一下我的配置,我这个配置访问地址:http://localhost/static/xxx/xxx/图片名称.png_100x100.png

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;
load_module modules/ngx_http_image_filter_module.so;

events {
    worker_connections  1024;
}

http {
    include mime.types; 
    default_type  application/octet-stream; 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout  65;

    proxy_connect_timeout 300;
    proxy_send_timeout 300;
    proxy_read_timeout 300;
    send_timeout 300;

    image_filter_buffer 10M;
    image_filter_jpeg_quality 75;
    image_filter_interlace on;

	server {
		listen 80;
		charset utf-8;
		server_name localhost;
		server_name_in_redirect off;

# 前端项目
		location / {
        		root   /usr/share/nginx/html/web;
			try_files $uri $uri/ /index.html;
        		index  index.html index.htm;
		}
# 图片访问
		location ~ ^/statics/(.+)\.(jpg|gif|png)_(\d+)x(\d+).(jpg|gif|png)$ {
			set $file_name $1;
			set $original_ext $2;
			set $width $3;
			set $height $4;
			set $new_ext $5;
# 调试,调试完删除
            add_header X-Debug-Message "$file_name, $original_ext, $width, $height, $new_ext" always;

			root /usr/share/nginx/html;
			rewrite ^/statics/(.+)\.(jpg|gif|png)_(\d+)x(\d+).(jpg|gif|png)$ /statics/$file_name.$original_ext break;
	
			image_filter resize $width $height;
			image_filter_buffer 5M;
			
			try_files $uri =404;
		}
# 文件访问
		location /statics/ {
			root /usr/share/nginx/html;
			autoindex off;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值