This module is not built by default, it should be enabled with the –with-http_secure_link_module configuration parameter.
参考 http://nginx.org/en/docs/http/ngx_http_secure_link_module.html

nginx安装模块
查看nginx模块是否存在http_secure_link_module

[root@VM-0-8-centos /]#   /www/server/nginx/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module

/www/server/nginx/src/下执行命令配置命令参数–with-http_secure_link_module

./configure  --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --with-http_secure_link_module

编译

make
        < man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/www/server/nginx/src'

备份原来的nginx

cp /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx.bak

替换原来的nginx

cp /www/server/nginx/src/objs/nginx /www/server/nginx/sbin/nginx

重启后查看nginx是否已存在该模块

/www/server/nginx/sbin/nginx -V
configure arguments: --user=www --group=www --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --add-module=/www/server/nginx/src/nginx-sticky-module --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module <strong>--with-http_secure_link_module</strong>

重启nginx遇到如下提示:/bin/sh: /usr/bin/nginx: Permission denied ,需要放开权限

nginx配置

        location ^~ /mb {  
 
        secure_link $arg_st,$arg_e;
        secure_link_md5 haha$uri$arg_e;
 
        if ($secure_link = "") {
            return 403;
        }
 
        if ($secure_link = "0") {
            return 403;
        }
 
      }

php生成加密链接

    function getMd5Path($domain,$url){
        //$url = "/haha/xxx.html";
        $secret = 'haha'; # 密钥
        # 下载到期时间,time是当前时间,300表示300秒,也就是说从现在到300秒之内文件不过期
        $expire = time()+300;
# 用文件路径、密钥、过期时间生成加密串
        $md5 = base64_encode(md5($secret . $url . $expire, true));
        $md5 = strtr($md5, '+/', '-_');
        $md5 = str_replace('=', '', $md5);
 
        $url = "{$domain}{$url}?st={$md5}&e={$expire}";
        return $url;
    }

文件下载跨域问题

        location ^~ /mb {  
 
        #设置跨域问题
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
 
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注