nginx安全下载模块ngx_http_secure_link_module

本文介绍如何使用Nginx的ngx_http_secure_link_module模块配置安全下载,通过添加时间戳和校验码来保护服务器文件,防止未授权访问。文中提供了PHP、Shell及Java生成安全链接的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

nginx安全下载模块可以给服务器文件链接添加时间戳和校验码,从而保护服务器文件不被任意下载盗用。nginx的ngx_http_secure_link_module模块和lighttpd的sec_download模块功能类似,配置更简单。

首先检查nginx是否已安装模块
#nginx -V
输出nginx所有已安装模块,检查是否有ngx_http_secure_link_module

配置nginx
#vi /etc/nginx/conf.d/cms.conf
    location /sec/ {
        root /soft/xlongwei;
        secure_link $arg_st,$arg_e;
        secure_link_md5 segredo$uri$arg_e; #segredo为密码样例
        if ( $secure_link = "" ) {
                return 402;
        }
        if ( $secure_link = "0" ) {
                return 405;
        }
    }

用php生成测试安全下载链接,由于配置有discuz版的bbs.xlongwei.com,所以直接在discuz目录编辑sec.php即可测试
#vi /soft/discuz/sec.php
<?php
$secret = 'segredo'; // secrets
$path   = "/".$_REQUEST["f"]; // ?f=path
$expire = time()+10; // add ? seconds to be available,这里是10妙内访问有效

$md5 = base64_encode(md5($secret . $path . $expire, true)); // Using binary hashing.
$md5 = strtr($md5, '+/', '-_'); // + and / are considered special characters in URLs, see the wikipedia page linked in references.
$md5 = str_replace('=', '', $md5); // When used in query parameters the base64 padding character is considered special.

$url = "http://cms.xlongwei.com$path?st=$md5&e=$expire";  //安全下载链接可以直接echo输出

$arr = array("url"=>$url, "expire"=>date("Y-m-d H:i:s", $expire), "md5"=>$md5);

echo json_encode($arr); //转成json格式输出也不错
?>

测试访问: http://bbs.xlongwei.com/sec.php?f=sec/test.txt
响应内容中的url: http://cms.xlongwei.com/sec/test.txt?st=FzMATYtf1urcUE5hKf01Bg&e=1437467381
如果超时后再访问会返回405

shell方式生成, http://tool.xlongwei.com/shells/sec.sh
secret=`echo segredo`
path=$1
e=`date -d "+15 seconds" +%s`
str=$secret$path$e
#echo $str
st=`echo -n $str | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =`

url="http://cms.xlongwei.com$path?st=$st&e=$e"
echo $url

java方式生成
public class Sec {
	public static String url(String path) {
		String secret="segredo"; //secret
		String e=String.valueOf((System.currentTimeMillis()/1000)+10); //10 seconds
		String md5 = Base64.encodeBase64URLSafeString(DigestUtils.md5(secret+path+e));
		return "http://cms.xlongwei.com"+path+"?st="+md5+"&e="+e;
	}
}

原文链接:https://www.xlongwei.com/detail/15072116

### 构建带有 `nginx_upstream_check_module` 模块Nginx Docker 镜像 为了创建一个包含 `nginx_upstream_check_module` 的自定义 Nginx Docker 镜像,可以按照以下方法操作: #### 准备工作环境 首先,在本地环境中准备必要的文件和脚本。这包括编写用于构建镜像的Dockerfile以及任何所需的shell脚本来简化模块的安装过程。 #### 编写 Dockerfile 下面是一个简单的 Dockerfile 示例,它展示了如何从源码编译并集成 `nginx_upstream_check_module` 到官方 Nginx 容器中: ```dockerfile FROM nginx:latest as builder # 设置环境变量以避免交互提示 ENV DEBIAN_FRONTEND noninteractive RUN apt-get update && \ apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev git wget unzip WORKDIR /usr/src # 下载指定版本的Nginx源码 (这里假设使用最新的稳定版) ARG NGINX_VERSION=1.21.6 RUN wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz && \ tar zxvf nginx-$NGINX_VERSION.tar.gz # 获取第三方模块源码 RUN wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master.zip && \ unzip master.zip WORKDIR /usr/src/nginx-$NGINX_VERSION # 使用原始配置加上新添加的模块选项重新编译Nginx RUN ./configure --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/cache/nginx/client_temp \ --http-proxy-temp-path=/var/cache/nginx/proxy_temp \ --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ --http-scgi-temp-path=/var/cache/nginx/scgi_temp \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --with-http_addition_module \ --with-http_auth_request_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_mp4_module \ --with-http_random_index_module \ --with-http_realip_module \ --with-http_secure_link_module \ --with-http_slice_module \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_sub_module \ --with-mail \ --with-stream \ --add-dynamic-module=../nginx_upstream_check_module-master && \ make modules # 创建最终运行时容器的基础层 FROM nginx:alpine COPY --from=builder /usr/src/nginx-$NGINX_VERSION/objs/ngx_http_upstream_check_module.so /etc/nginx/modules/ # 修改默认配置文件来加载动态模块和支持健康检查功能 RUN echo 'load_module modules/ngx_http_upstream_check_module.so;' >> /etc/nginx/conf.d/default.conf # 将应用特定的配置复制到容器内 COPY conf.d/* /etc/nginx/conf.d/ ``` 此 Dockerfile 中的关键部分在于通过 `--add-dynamic-module` 参数指定了要加入的新模块路径,并且最后一步是确保新的 `.so` 文件被正确放置以便于后续由 Nginx 加载[^1]。 请注意上述命令中的具体路径可能依据实际环境有所变化;此外还需要根据实际情况调整所使用的依赖包列表及其版本号。 完成以上步骤之后就可以利用这个 Dockerfile 来构建自己的 Docker 镜像了。记得在执行 docker build 前先确认所有外部资源都可以正常访问并且能够成功下载下来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值