nginx基本http_image_filter_module插件的缩略图服务搭建

下载nginx源码并编译

安装编译环境
yum -y install gcc glibc zlib libpng libjpeg libvpx libtiff libfreetype gd-devel pcre-devel libcurl-devel openssl openssl-devel libwebp pcre

下载nginx源码包
wget -c http://nginx.org/download/nginx-1.25.1.tar.gz

编译&安装
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module --with-pcre --with-http_image_filter_module

make install

配置
mkdir /usr/local/nginx/conf/conf.d
mkdir /var/nginx/{store,cache}

nginx配置

nginx.conf

events {
    worker_connections  102400;
    use epoll;
    multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    proxy_cache_lock on;
    proxy_cache_lock_timeout 60s;
    proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=s3cache:256M inactive=10d  max_size=10g;
    proxy_temp_path /var/nginx/cache/tmp;


    include conf.d/*.conf;
}

image.conf

server {
    listen 8192;
    listen [::]:8192 ipv6only=on;

    server_tokens off;
    server_name  localhost;

    location ~* /(.+)_(\d+)x(\d+).(jpg|gif|png|bmp|jpeg)$ {
	root /var/nginx/store;
        proxy_redirect off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_intercept_errors on;

        proxy_store on;
	    proxy_store_access user:rw group:rw all:r;
        proxy_temp_path /var/nginx/store;
	
	if (!-e $request_filename) {
            proxy_pass  http://127.0.0.1:18192;
        }

    }

    location / {
	proxy_redirect off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_intercept_errors on;

        proxy_pass  http://127.0.0.1:18192;
    }
}

image_thumb.conf

server {
    listen 127.0.0.1:18192;

    server_tokens off;
    server_name  localhost;

    image_filter_buffer 100M;   # 设置读取图像缓冲的最大大小,超过则415错误。

    location ~* /(.+)_(\d+)x(\d+).(jpg|gif|png|bmp|jpeg)$ {
        set $backend 'test.oss.aliyun.com';

	proxy_cache s3cache;
        proxy_cache_key "$uri";

	# cache successful responses for 24 hours proxy_cache_valid  200 302 24h;
    # cache missing responses for 1 minutes proxy_cache_valid  403 404 415 1m;

    proxy_redirect off;

    # need to set the hot to be $backend here so s3 static website hosting service knows what bucket to use
    proxy_set_header        Host $backend;
    proxy_set_header        X-Real-IP $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header              X-Cache-Status $upstream_cache_status;
    proxy_hide_header       Set-Cookie;
    proxy_ignore_headers    "Set-Cookie";
    proxy_intercept_errors on;
    resolver 172.10.20.1;
    resolver_timeout 5s;

    set $width $2;
    set $height $3;

	if ($width = "0") {
            set $width "-";
        }
        if ($height = "0") {
            set $height "-";
        }
        image_filter resize $width $height;

        rewrite /(.+)_(\d+)x(\d+).(jpg|gif|png|bmp|jpeg)$  /$1  break;
        proxy_pass https://$backend;

        allow 127.0.0.1;
        deny all;
    }

    location / {
        set $backend 'test.oss.aliyun.com';

        proxy_cache s3cache;
        proxy_cache_key "$uri";

        # cache successful responses for 24 hours
        proxy_cache_valid  200 302 24h;
        # cache missing responses for 1 minutes
        proxy_cache_valid  403 404 415 1m;

        proxy_redirect off;
        # need to set the hot to be $backend here so s3 static website hosting service knows what bucket to use
        proxy_set_header        Host $backend;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        add_header              X-Cache-Status $upstream_cache_status;
        proxy_hide_header       x-amz-id-2;
        proxy_hide_header       x-amz-request-id;
        proxy_hide_header       Set-Cookie;
        proxy_ignore_headers    "Set-Cookie";
        proxy_intercept_errors on;
        resolver 172.16.20.148;
        resolver_timeout 5s;

	    proxy_pass https://$backend;

        allow 127.0.0.1;
        deny all;
    }
}

示例

缩略图:http://172.10.20.10:8192/static/images/2023/07/14/9d9aag5b546c4714a1f5909182816549.png_190x190.png

原图:http://172.10.20.10:8192/static/images/2023/07/14/9d9aag5b546c4714a1f5909182816549.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值