使用SecureLink模块

 

 

 

 

    这个模块用于为所需的安全性“令牌”计算和检查请求URL。在0.7.18版本以上的Nginx中提供了该模块,这个模块在Nginx的默认安装中没有包含在内,因此,如果想使用该模块那么需要在configure时指定--with-http_secure_link_module选项。对于0.8.50之后的版,有添加了secure_link_md5指令和secure_link_expires变量,因此,指令secure_link_secret已经不赞成使用了。

 

 

 

配置示例

 

示例1

 

location /prefix/ {

  secure_link_secret secret_word;

 

# If the hash is incorrect then $secure_link has the value of the null string.

  if ($secure_link = "") {

     return 403;

  }

 

  # This needs to be here otherwise you'll get a 404.

   rewrite ^ /prefix/$secure_link break;

}

   

示例2

 

location /p/ {

    ## This must match the URI part related to the MD5 hash and expiration time.

    secure_link $arg_st,$arg_e; # this must match the URI part related

 

    ## This is how the MD5 hash is built from a secret token, an URI and an

    ## expiration time.

    secure_link_md5 segredo$uri$arg_e; # 'segredo' is the secret token

 

    ## If the hash is incorrect then $secure_link is a null string.

    if ($secure_link = "") {

        return 403;

    }

 

    ## The current local time is greater than the specified expiration time.

    if ($secure_link = "0") {

        return 403;

    }

 

    ## If everything is ok $secure_link is 1.

    ## This needs to be here otherwise you'll get a 404.

    rewrite ^/p/(.*)$ /p/$1 break;

}

   

    在这个配置中,我们最终会通过以下URL访问:

 

http://example.com/p/files/top_secret.pdf?st=PIrEk4JX5gJPTGmvqJG41g&e=1324527723

   

    在这个URL中有两处看的比较不顺眼:

 

    一处是st=PIrEk4JX5gJPTGmvqJG41g,另一处是e=1324527723,这两个参数会被传递到服务器端,然后会通过相应的参数获取这些值,至于这些值的使用后面会讲到,我们现在主要说这些值是怎么来的,下面的值都是通过在命令行中计算出的,但是在具体的应用中都是通过具体的语言自动计算得出。

 

    要构建上面的哈希值,可以使用PHP语言(当然其它语言也是可以的),例如我在命令行中使用PHP生成哈希值:

 

[root@mail gz]# php -r 'print  str_replace("=", "",strtr(base64_encode(md5("segredo/p/files/top_secret.pdf1324527723", TRUE)), "+/", "-_")) . "\n";'

PIrEk4JX5gJPTGmvqJG41g

   

    如上所示,用黑体字打出的这一行便是MD5哈希值。当然如果你运行着web 应用,那么这个值必须是自动生成的,而不能像这样手动命令行操作,需要注意的是MD5哈希格式为二进制格式,因此要进行base64编码。

 

    对于生存期,我们可以是PHPtime()函数来实现,当然也可使用其它语言来实现,为了获取Unix epoch时间格式。在这里我们可以通过linux命令计算出:

 

[root@mail gz]# date +%s -d "December 22, 2011  12:22:03"

1324527723

   

    也许你会问这个 "December 22, 2011  12:22:03" 时间是怎么推出来的,同样是使用date

   

[root@mail gz]# date -d @1324527723

Thu Dec 22 12:22:03 CST 2011

 

   

 

    该模块提供了以下3条指令。

 

指令名称:secure_link_secret

    : secure_link_secret secret_word

默 认 值: none

使用环境: location

    能:该指令用于指定一个密码,该密码被用于MD5哈希生成校验请求。一个完整的被保护的连接格式如下:

/prefix/MD5 hash/reference

 

这里的MD5哈希值就是由该指令指定的secret_word密码生成,然后利用它来保护安全连接URI

 

          例如,我们想保护位于目录p下的文件top_secret_file.pdf,那么我们需要在Nginx的配置文件中添加以下配置:

 

location /p/ {

    secure_link_secret segredo;

 

    # If the hash is incorrect then $secure_link has the value of the null string.

    if ($secure_link = "") {

        return 403;

    }

 

    # This needs to be here otherwise you'll get a 404.

    rewrite ^ /p/$secure_link break;

}

   

    我们可以通过使用openssl目录行工具来计算MD5哈希值,具体的做法是这样的:

 

[root@mail gz]# echo -n 'top_secret_file.pdfsegredo' | openssl dgst -md5

0849e9c72988f118896724a0502b92a8

    

    我们看到,被MD5计算的字符不仅仅是指令secure_link_secret指定的segredo密码,还有被访问文件的文件名称。

 

    在计算出这个值后,我们现在才可以使用以下的URL进行访问(这已经是一个被保护的URL)

http://example.com/p/0849e9c72988f118896724a0502b92a8/top_secret_file.pdf

 

而采用通常的方法:

 

http://example.com/p/top_secret_file.pdf

 

是无法访问到文件。

 

需要注意的问题有一点,那就是不要出现以下的使用方法:

 

location / {

   # This is wrong, wrong, wrong. It's a root path!

   secure_link_secret segredo;

   [...]

}

 

这配置之所以错,就是因为它对根路径实施了保护,这是不可以的,因此,仅能对非根的的路径进行安全连接保护。

 

指令名称:secure_link

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27043155/viewspace-732983/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27043155/viewspace-732983/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值