带有子文件夹支持的Nginx动态图像调整大小

时间:2019-11-04 17:52:46

标签: nginx nginx-location nginx-reverse-proxy

我已经实现了一个Nginx代理来自动调整大小和缓存保存在特定位置并且可以正常工作的图像。我的问题是,如果我尝试将子文件夹放入结构中,系统将无法识别它们。我尝试了几种选择,但没有任何效果。 我当前的Nginx配置是这样的:

#main server

server {
    listen         xxx.xxx.xxx.xxx:443 ssl http2;
    server_name    cdn.example.com;
    server_name    www.cdn.example.com;

    client_max_body_size 128m;
    disable_symlinks if_not_owner from=$document_root;

    root "/var/www/cdn.example.com/someclient";

    if ($host ~* ^www\.cdn\.example\.com$) {
        rewrite ^(.*)$ https://cdn.example.com$1 permanent;
    }

    location ~ "^/images/(?<width>(180|208|268|308|334|360|400|416|480|536|616|668|800|960|1072|1336|1920))/(?<image>.+)$"{
        proxy_pass http://localhost:9000/images/$width/$image;
        proxy_cache sawitaly;
        proxy_cache_valid 200 720h;

        allow all;

        add_header Allow "GET, HEAD" always;
        if ($request_method !~ ^(GET|HEAD)$){
            return 405;
        }

        expires max;
        add_header Vary "Accept, Accept-Encoding";
        add_header X-Image-Cache $upstream_cache_status;
        add_header Cache-Control "public";
        add_header Strict-Transport-Security "max-age=31536000; preload" always;
    }

    location /images/ {
        proxy_pass http://localhost:9000/;
    }
}

#proxy server

server {
    listen 9000;
    server_name localhost;

    allow 127.0.0.1;
    deny all;

    location ~ "^/images/(?<width>\d+)/(?<image>.+)$" {
        alias /var/www/cdn.example.com/someclient/srcimg/$image;

        image_filter resize $width -;
        image_filter_jpeg_quality 75;
        image_filter_webp_quality 75;
        image_filter_interlace on;
        image_filter_buffer 16m;
    }
}

# Cache settings

proxy_cache_path /tmp/nginx-someclient-cache/ levels=1:2 keys_zone=someclient:64m inactive=240h max_size=1024m;

如果更改文件夹结构并将图像放在/var/www/cdn.example.com/someclient/srcimg/somefolder1/$image/var/www/cdn.example.com/someclient/srcimg/somefolder2/$image之类的子文件夹中,则无法使其正常工作。

为防止对机器资源的任何无意访问,除映像位置外,所有访问均被403阻止。

问题是我不知道子文件夹的数目或名称,它们是动态生成的。任何帮助将不胜感激!

0 个答案:

没有答案