Skip to content

Instantly share code, notes, and snippets.

@bftanase
Last active July 19, 2024 16:14
Show Gist options
  • Save bftanase/cbae1f9fc69bb4f9cb86 to your computer and use it in GitHub Desktop.
Save bftanase/cbae1f9fc69bb4f9cb86 to your computer and use it in GitHub Desktop.
generate URL for nginx secure_link
@george-of-314
Copy link

george-of-314 commented Feb 7, 2024

UPD. did it in this way

location section {
    if ($secure_link = "0") {
          return 419;
    }
}
...
error_page 419 = @unsecure;
....
location @unsecure {
    # Handle unsecure requests here
    rewrite "^/([0-9]+)/th_([a-z0-9-_]+).(jpg|png|gif)$" /local_folder/picture.php?id=$1&size=th&idx=01&tkey=$tkey;
}

is there any possibility to rewrite or redirect to php script using wildcards in case of expired image?
e.g. something like

if($secure_link = "0") {
    rewrite "^/([0-9]{7})/th_([a-z0-9-_]+).(jpg|png|gif)$" /local_folder/picture.php?id=$1&size=th&idx=01&tkey=$tkey break;
}

but then the php file is saved and not loaded in the browser.
When I enter in browser URL https://this.server/local_folder/picture.php?id=XXXXX&size=th&idx=01&tkey=XXXXXXXXXXX - all is loading fine (image with filters is shown)
There's a block in server setting, which handles php scripts

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

when I use

if($secure_link = "0") {
    return 302 /local_folder/picture.php?id=$1&size=th&idx=01&tkey=$tkey;
}

all working - URL is redirected, image is shown. But how to use rewrite here? Better solution is to keep original URL

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment