天天看点

MinIO使用Nginx代理访问以及实时缩略图的使用

作者:那年o2016

前面几篇文章以上了解的MinIO的基础使用方法了,但是我们实际使用的时候一般都会通过nginx代理转发。本文将介绍如何使用Nginx代理访问MinIO,并且实现通过Nginx的image-filter模块实现实时缩略图。

配置Nginx代理

首先,我们需要在Nginx上配置反向代理来访问MinIO。我们可以通过以下配置将Nginx与MinIO集成:

location / {
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Forwarded-Proto $scheme;
	proxy_set_header Host $http_host;

	proxy_connect_timeout 300;
	# Default is HTTP/1, keepalive is only enabled in HTTP/1.1
	proxy_http_version 1.1;
	proxy_set_header Connection "";
	chunked_transfer_encoding off;
	proxy_pass http://localhost:9000;
}           

在这个配置中,我们将Nginx监听地址的“/”路径映射到MinIO服务器的IP地址和端口号上“http://localhost:9000;”。我们还需要设置一些header信息,以确保MinIO服务器正确解析请求。这里也给出一段官方的配置:

MinIO使用Nginx代理访问以及实时缩略图的使用

这里注意以下配置要加上:

# To allow special characters in headers
 ignore_invalid_headers off;

 # To disable buffering
 proxy_buffering off;           

使用Nginx实时缩略图

这里要先了解下ngx_http_image_filter_module模块,它提供了一种在Nginx服务器上动态生成缩略图的方法,要使用ngx_http_image_filter_module模块,你需要自己编译在Nginx时启用该模块,也可以通过下载别人编译好带有该模块的nginx压缩包。

Nginx配置文件中启用ngx_http_image_filter_module模块。你可以通过以下配置来启用该模块:

http {
    # 启用 ngx_http_image_filter_module 模块
    image_filter on;
    # 设置缩略图生成参数
    image_filter_jpeg_quality 90;
    image_filter_buffer 5M;
}
           

image_filter:设置为“on”以启用ngx_http_image_filter_module模块。

image_filter_jpeg_quality:控制缩略图的质量。

image_filter_buffer:缓冲区大小。

配置缩略图路由,代码如下:

location ^~ /thumbnail/ {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;
  proxy_connect_timeout 300;
  proxy_http_version 1.1;
  proxy_set_header Connection "";
  chunked_transfer_encoding off;
  proxy_pass http://localhost:9000/;
  set $width -;
  set $height -;
  if ($arg_w != '') {
    set $width $arg_w;
  }
  if ($arg_h != '') {
    set $height $arg_h;
  }
  image_filter resize $width $height;
}           

image_filter resize:指定缩略图的大小。

至此完成配置,下面看看效果:

MinIO使用Nginx代理访问以及实时缩略图的使用

原图

MinIO使用Nginx代理访问以及实时缩略图的使用

压缩宽度800

MinIO使用Nginx代理访问以及实时缩略图的使用

原图

MinIO使用Nginx代理访问以及实时缩略图的使用

压缩宽度800

继续阅读