php - Nginx secure link with expire not working

one text

I am trying to set up NGINX secure link with expiration on a ubuntu server. Here is the NGINX configuration and PHP code I am currently using.

location /files {
    
    secure_link $arg_md5,$arg_expires;
    secure_link_md5 "$secure_link_expires$uri$remote_addr enigma";

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

}

Here is the PHP code


$domain = 'mydomain.com';

$uri = urldecode('/files/test.pdf');

$ip = $_SERVER['REMOTE_ADDR'];

$secure_text = ' enigma';

function getSecureHash($ip, $uri, $secure_text, $expires){
 
 $str = $expires.$uri.$ip.$secure_text;
 
 $tmp = md5( $str, true );
 
 $tmp = base64_encode( $tmp );

 return str_replace( array('+', '/', '='), array('-', '_', ''), $tmp );

}

$url = "$domain$uri?md5=".getSecureHash($ip, $uri, $secure_text, $expires)."&expires=$expires";

echo $ip; 
echo "</br>";
echo "</br>";
echo "</br>";
echo $url;

With this code, I always get 403 forbidden. However when I remove IP from both NGINX and PHP it works, but I want to create a unique link based on IP.

Any guidance will be highly appreciated.

I am following this link

Source