Skip to content

Instantly share code, notes, and snippets.

@bftanase
Last active March 28, 2024 06:54
Show Gist options
  • Star 48 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • 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
@smowtion
Copy link

Can you share nginx config

@mohammadhosin
Copy link

mohammadhosin commented Jun 5, 2017

	location /securetest {
		   alias html/mhmsecure/securetest;
            secure_link $arg_md5,$arg_expires;
            secure_link_md5 "$secure_link_expires$uri$remote_addr live21.ir";

            if ($secure_link = "") { return 403; }
            if ($secure_link = "0") { return 410; }
        
		}

@nicolasvahidzein
Copy link

You Sir are amazing, thank you for sharing this. I NEVER would have been able to figure this secure link thing alone, you saved me big time. Thank you thank you thank you!!!!

@tahir7us
Copy link

tahir7us commented Aug 6, 2019

Thank you so much, you are the time-saver

@fquero47
Copy link

fquero47 commented Jul 2, 2020

THANKKK you a lot bro.

@batoasnawi1986
Copy link

it not working for different server. like server 1 is where stored the zip file and server 2 were the secure_link.php was configured. please help how to fixed?

@satirovaf
Copy link

I know that I'm a little bit later, but I'm having on 403, for each link that I generate.

The PHP is getting the IP and time right, but I still have 403. Any clues?

@bftanase
Copy link
Author

There are some examples on the nginx docs page on how to generate the md5 hash using linux commands.
http://nginx.org/en/docs/http/ngx_http_secure_link_module.html#secure_link

You could try to compare what you get from php with this and see if there's any difference.

@satirovaf
Copy link

Thanks for your reply, even generating the md5 hash in Linux and not at the PHP file, I will getting 403 when trying to serve the video file.

@bftanase
Copy link
Author

Then, maybe there's something wrong with the nginx config.

@satirovaf
Copy link

I'm using the config provided above, and all the PHP is running ok on the subdomain. Also tried the config from (https://rahul-juneja3.medium.com/how-to-create-secure-download-link-urls-via-nginx-5578a0db5913), with and without the 443 SSL from certbot.

@bftanase
Copy link
Author

Well, at this point, I would look for small typos, maybe the secret is not the same in nginx config and in your php script. Maybe a weird character copy pasted somewhere.

I don't have any other ideas... :)

@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