首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nginx安全链接模块不是处理php文件,而是处理静态文件。

Nginx安全链接模块不是处理php文件,而是处理静态文件。
EN

Stack Overflow用户
提问于 2014-12-14 11:14:42
回答 1查看 2.5K关注 0票数 2

我使用module.html Nginx安全链接模块来保护静态文件下载。对于静态文件,它工作得很好。

但是,当我试图用php文件实现这一点时,它不起作用。基本上,我是通过ajax请求使用它的,如

代码语言:javascript
复制
http://www.example.com/dev/serve.php?h=hash&t=timestamp

当我在下面检查时,虽然它没有哈希和时间戳,但是它是不受任何限制的。

代码语言:javascript
复制
http://www.example.com/dev/serve.php

对于静态文件,它正常工作,即在访问没有散列的任何图像时,将返回指定的错误请求响应的时间戳:

代码语言:javascript
复制
http://www.example.com/dev/image.png

服务器Config:

代码语言:javascript
复制
#
# The default server
#

server {
    listen       80;
    server_name  www.example.com;

    location / {
        root   /box;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /box;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root   /box;
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    location /dev/ {
        root   /box;
        secure_link $arg_h,$arg_s;

        secure_link_md5 "secret$uri$secure_link_expires$remote_addr";

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

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

我的问题:

  1. 它不适用于php这样的动态文件吗?
  2. 如果适用的话,如何实施呢?
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-12 15:31:43

过了一会儿,我找到了解决办法:

代码语言:javascript
复制
#
# The default server
#

server {
    listen       80;
    server_name  www.example.com;

    location / {
        root   /box;
        index  index.php index.html index.htm;
    }

    error_page  404              /404.html;
    location = /404.html {
        root   /box;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root   /box;

        secure_link $arg_h,$arg_s;

        secure_link_md5 "secret$uri$secure_link_expires$remote_addr";

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

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

        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

请注意,这个解决方案是针对php文件的,很少修改它也适用于其他文件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27468743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档