Nginx http_secure_link 是一种防盗链的机制,它可以用来保护网站资源的安全性。在使用 Nginx http_secure_link 时,我们需要在 Nginx 配置文件中设置一些参数,同时在 PHP 代码中也需要添加一些防盗链的代码。

以下是 Nginx 配置文件中设置 http_secure_link 的示例:

location / {
    # 配置防盗链
    secure_link_secret "your_secret_key";
    secure_link $arg_md5,$arg_expires;
    if ($secure_link = "") {
        return 403;
    }
    if ($secure_link = "0") {
        return 410;
    }
    # 配置静态文件访问
    root /var/www/;
    index index.html index.htm;
}

在 PHP 代码中,我们需要生成一个 md5 值和一个过期时间,并将它们添加到请求的 URL 中。以下是一个防盗链的示例代码:

<?php
// 设置密钥
$secret_key = "your_secret_key";
// 设置过期时间为 1 小时
$expires = time() + 3600;
// 获取文件名
$file = "example.jpg";
// 生成 md5 值
$md5 = md5("$expires$file$secret_key", true);
// 将 md5 值转换为 base64 编码
$md5_base64 = base64_encode($md5);
// 生成 URL
$url = "http://example.com/$file?md5=$md5_base64&expires=$expires";
// 输出 URL
echo $url;
?>

在生成 URL 时,我们将 md5 值和过期时间添加到 URL 的参数中,并使用 base64 编码将 md5 值转换为字符串。当请求到达 Nginx 服务器时,Nginx 会将 URL 中的参数与配置文件中的参数进行比较,如果验证通过就会允许访问资源,否则会返回 403 或 410 状态码

nginx http_secure_link 防盗php 代码与nginx设置

原文地址: https://www.cveoy.top/t/topic/fJwr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录