原理很簡單,就是基於nginx 的rewrite 自動處理模版bucket 的index page 處理
參考配置
- 環境准備
version: "3"
services:
nginx:
image: openresty/openresty:alpine-fat
ports:
- "80:80"
volumes:
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
s3:
image: minio/minio
command: server /export
ports:
- "9002:9000"
volumes:
- ./data:/data
- ./config:/root/.minio
environment:
- "MINIO_ACCESS_KEY=dalongapp"
- "MINIO_SECRET_KEY=dalongapp"
- nginx 配置
worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
resolver 127.0.0.11 8.8.8.8 ipv6=off;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
upstream s3app {
server s3:9000;
}
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
index index.html index.htm;
default_type text/html;
location /demoapp/ {
default_type text/html;
index index.html index.htm;
rewrite ^/(demoapp)(\d*)/$ /$1$2/index.html break;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_next_upstream error timeout http_404;
proxy_pass http://s3app;
}
location /demoapp2/ {
default_type text/html;
index index.html index.htm;
rewrite ^/(demoapp)(\d*)/$ /$1$2/index.html break;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_next_upstream error timeout http_404;
proxy_pass http://s3app;
}
location /demoapp3/ {
default_type text/html;
index index.html index.htm;
rewrite ^/(demoapp)(\d*)/$ /$1$2/index.html break;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffers 1024 4k;
proxy_read_timeout 300;
proxy_next_upstream error timeout http_404;
proxy_pass http://s3app;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- 簡單說明
借用了對於前綴匹配,如果location 包含了/ 那么對於請求不帶/ 會自動觸發一個301 的重定向,同時按照s3桶的格式進行
rewrite,比如上邊的就是對於請求為 /demoapp/ /demoapp1/ .... 格式的頁面自動重定向到/demoapp數字/index.html 的頁面
這樣就解決了默認頁面的問題,如果使用了openresty周邊的產品那么我們可以方便的進行規則重寫(基於openresty強大的
生命周期能力)
訪問參考效果
- minio
注意都開啟了* read only 的策略
- nginx 訪問效果
說明
以上是一個參考實踐,從github 的issues上 理論上是可以配置禁止列表顯示的(但是我們需要的是文件夾的列表),實際上也有直接通過精准匹配
進行重定向默認頁面的,但是不是很靈活
參考資料
https://nginx.org/en/docs/http/ngx_http_core_module.html#location
https://docs.min.io/docs/setup-nginx-proxy-with-minio.html
https://github.com/minio/minio/issues/5279
https://stackoverflow.com/questions/46114783/how-to-proxy-pass-from-to-index-html