Extracting Filename from URI and Proxy Image Storage in Nginx Configuration


2 views

When working with Nginx rewrites and proxy configurations, we often need to extract just the filename portion from a request URI. The standard Nginx variables like $uri, $request_uri, and $request_filename provide the full path, which isn't always what we need.

The most effective approach is to use regular expression captures in your location block:

location ~* ^/(.*\.(gif|jpg))$ {
    set $filename $1;
    try_files $uri @imgstore;
}

Here's a robust solution for proxy image storage:

server {
    set $source_folder /path/to/source/images/;
    set $dest_folder /path/to/destination/;

    # Capture filename with extension
    location ~* ^/([^/]+\.(gif|jpg|png))$ {
        set $filename $1;
        try_files $uri @imgstore;
    }

    location @imgstore {
        # Verify file existence in source
        if (-f $source_folder$filename) {
            proxy_pass http://backend/$filename;
            proxy_store $dest_folder$filename;
            proxy_store_access user:rw group:rw all:r;
            proxy_temp_path /var/tmp/nginx_proxy/;
        }
        
        # Handle missing files
        if (!-f $source_folder$filename) {
            return 404;
        }
    }
}

For more complex scenarios, consider using Nginx's map directive:

map $uri $clean_filename {
    ~*/([^/]+)$ $1;
    default   '';
}

server {
    location ~* \.(gif|jpg)$ {
        try_files $uri @imgstore;
    }

    location @imgstore {
        proxy_pass http://storage/$clean_filename;
        # Additional proxy settings...
    }
}

When implementing filename extraction:

  • Always validate file extensions
  • Implement proper permission controls
  • Consider adding hash verification for sensitive operations

For high-traffic systems:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=img_cache:10m inactive=60m;
proxy_cache_key "$scheme://$host$request_uri$is_args$args";

location @imgstore {
    proxy_cache img_cache;
    proxy_cache_valid 200 304 12h;
    proxy_cache_use_stale error timeout updating;
    # Rest of proxy configuration...
}

When working with NGINX configurations, we often need to extract just the filename portion from a request URI for various processing needs. The standard NGINX variables like $uri, $request_uri, and $request_filename provide the full path, but sometimes we need just the basename.

While NGINX doesn't have a built-in variable for just the filename, we can use regex captures to extract it. The $1 capture you discovered works for extensions, but we can expand this approach for full filenames.

location ~* ^/(.*\.(gif|jpg))$ {
    set $filename $1;
    try_files $uri @imgstore;
}

Here's an improved version of your configuration that properly handles filename extraction and storage:

server {
    set $file_folder /path/to/your/imageAll/;
    
    # Capture full filename with extension
    location ~* ^/([^/]+\.(gif|jpg))$ {
        set $filename $1;
        try_files $uri @imgstore;
    }

    location @imgstore {
        # Use the captured filename
        proxy_pass $file_folder$filename;
        proxy_store on;
        proxy_store_access user:rw group:rw all:r;
        
        # Better temp path handling
        proxy_temp_path /var/cache/nginx/temp;
        
        # Important headers for proper storage
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

For more complex scenarios, consider using the map directive or Lua scripting (if using OpenResty):

map $uri $basename {
    ~/([^/]+)$ $1;
}

server {
    # ... other config ...
    location / {
        # Now you can use $basename anywhere
        add_header X-Filename $basename;
    }
}

When implementing image storage solutions:

  • Always validate file extensions
  • Consider adding hash-based filenames to prevent conflicts
  • Set proper filesystem permissions
  • Implement cleanup mechanisms for temp files

The proxy_store approach works well for many cases, but for high-traffic systems, consider:

  • Using NGINX's X-Accel-Redirect for more efficient file serving
  • Implementing a proper CDN solution
  • Adding cache control headers