【问题标题】:nginx rewrite secure linknginx 重写安全链接
【发布时间】:2013-07-14 20:53:48
【问题描述】:

我正在尝试重写一个安全链接,这是我的 nginx 配置:

    location /files/ {
        deny all;
        return 403;
    }

    # MEMBERS ONLY #####
    location /auth/ {
        secure_link $arg_h,$arg_e;
        secure_link_md5 authkey$uri$arg_e;
        if ($secure_link = "") {
            return 403;
        }
        if ($secure_link = "0") {
            return 403;
        }
        rewrite  ^/auth/(.*)$  /files/$1  break;
        add_header Content-Disposition "attachment; filename=$arg_f";
    }

如果我这样放下载链接,它的工作:

http://13.37.13.37/auth/path/to/dir/file.zip?h=sdiouqosid&e=1337&f=the_file.zip

但如果我打印这样的链接,它不会:

http://subdir.mysite.org/auth/path/to/dir/file.zip?h=sdiouqosid&e=1337&f=the_file.zip

请注意:

  • subdir.mysite.org 在 DNS 记录中有“A”重定向到 13.37.13.37
  • 13.37.13.37 与 mysite.org 的服务器不同

还有:
- http://subdir.mysite.org/path/to/something/somefile.zip 效果很好,只有当我使用 secure_link 时它才会失败(返回 403 或加载资源失败)。我想这与我的 url_rewrite 有关。对于这种奇怪的行为,我尝试了很多东西但没有成功。

感谢您的帮助

编辑:
下面是完整的 nginx:

user www-data;
worker_processes  2;

events {
    worker_connections  1024;
}

http {

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    #server_tokens off;

    keepalive_timeout  65;

    gzip on;
    gzip_disable "msie6";

    limit_conn_zone $binary_remote_addr zone=freeuser:10m;

    map $request_uri $request_qs {
        default '';
        ~.*\?(?P<key>.+)$ $key;
    }

    server {

        listen          80;
        server_name     localhost;
        root            /var/www;


        location / {
            proxy_pass http://127.0.0.1:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            access_log off;
            add_header Cache-Control public;
        }


        location ~* \.(jpg|jpeg|gif|css|png|js|ico|swf|mp3)$ {
            expires        365d;
            access_log     off;
        }


        location /file/ {
            deny all;
            return 403;
           }


        location /auth/ {
            secure_link $arg_h,$arg_e;
            secure_link_md5 authkey$uri$arg_e$remote_addr;
            if ($secure_link = "") {
                return 403;
            }
            if ($secure_link = "0") {
                return 403;
            }
            rewrite  ^/auth/(.*)$  /file/$1  break;
            add_header Content-Disposition "attachment; filename=$arg_f";
        }


    }

}

【问题讨论】:

  • 你能粘贴其余的conf,至少是服务器块的其余部分。
  • 我编辑了第一篇完整的帖子。

标签: nginx rewrite


【解决方案1】:

我会说更改 /files 块,因为现在返回 403 是合乎逻辑的,因为你只是拒绝所有。

location /files/ {
    internal;
}

这将返回 404 而非授权而不是 403,不知道这是否适合您。

【讨论】:

  • 如果我像你说的那样改变,任何未经授权的人都可以下载受保护的文件(暗示他们猜测路径)。而且它已经很好地工作了。只有当我放置“subdir.mysite.org”而不是“服务器 ip”时它才会失败。如果我设置域名而不是 ip,问题似乎是 'if ($secure_link = "")' 变为 'true'...
  • internal 表示没有人可以从服务器外部访问files 目录,这意味着只有服务器重写才能访问该文件夹。只留下 rewrite ^/auth/(.*)$ /files/$1 break; 重写来访问它。 wiki.nginx.org/HttpCoreModule#internal
  • 好的。无论如何,这并不能解决问题,因为当我放置服务器 ip 时脚本的行为按预期进行,但当我放置子域时失败。感谢您的尝试...
【解决方案2】:

我解决了编辑“反向”(主机名)的问题。
问候,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 1970-01-01
    • 2016-08-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多