[转载]Nginx 使用 X-Accel-Redirect 实现静态文件下载的统计、鉴权、防盗链、限速等

需求

  • 统计静态文件的下载次数;
  • 判断用户是否有下载权限;
  • 根据用户指定下载速度;
  • 根据Referer判断是否需要防盗链;
  • 根据用户属性限制下载速度;

X-Accel-Redirect

This allows you to handle authentication, logging or whatever else you please in your backend and then have NGINX handle serving the contents from redirected location to the end user, thus freeing up the backend to handle other requests. This feature is commonly known as X-Sendfile.
这个功能允许你在后端处理权限,日志或任何你想干的,Nginx提供内容服务给终端用户从重定向后的路径,因此可以释放后端去处理其他请求(直接由Nginx提供IO,而不是后端服务)。这个功能类似 X-Sendfile 。

不同Web服务器,相同功能,不同的标识:

nginx: X-Accel-Redirect

squid: X-Accelerator-Vary

apache: X-Sendfile

lighttpd: X-Sendfile/X-LIGHTTPD-send-file

X-Accel-Limit-Rate

限制下载速度,单位字节。默认不限速度。

X-Accel-Buffering

设置此连接的代理缓存,将此设置为no将允许适用于CometHTTP流式应用程序的无缓冲响应。将此设置为yes将允许响应被缓存。默认yes

X-Accel-Expires

如果已传输过的文件被缓存下载,设置Nginx文件缓存过期时间,单位秒。默认不过期。

X-Accel-Charset

设置文件字符集,默认UTF-8

使用条件

  • 必须有Nginx作为后端服务的代理;
  • 必须访问Nginx的代理地址,直接访问后端服务Nginx会报404
  • 可自行配置Content-Type来控制是下载(application/octet-stream)还是展示(image/jpeg等);

代码实现

  1. Nginx监听9876端口。
  2. Nginx代理后端服务的8080端口。
  3. 设置/testAccel路径为internal,指定具体文件存储的磁盘位置。
  4. 后端服务接收到文件下载请求,处理业务逻辑后X-Accel-Redirect/testAccel路径。
  5. Nginx收到后端返回信息中的X-Accel-Redirect请求头,接管文件下载或显示任务。
  6. 请求路径:http://localhost:9876/file/download/1234.jpg。

Nginx配置:

        location / {
            #root   html;
            root   F:/web/;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        location /testAccel {
            internal;
            alias F:/web/testAccel/file;
        }
        
        
        location /file {
            proxy_redirect off;
            proxy_set_header Host  $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8080;            
        }

Java代码

注意fileName添加:.+,或者获取不到文件后缀名。

    @GetMapping("/file/download/{fileName:.+}")
    public void download(HttpServletRequest request,
                         HttpServletResponse response,
                         @PathVariable("fileName") String fileName) {
        //统计
        //鉴权
        //判断Referer
        String referer = request.getHeader("Referer");
        System.out.println(referer);

        String prefix = "/testAccel";
        // 在这之前进行一些必要的处理,比如鉴权,或者其它的处理逻辑。
        // 通过X-Accel-Redirect返回在nginx中的实际下载地址
        response.setHeader("X-Accel-Redirect", prefix + "/" + fileName);
        response.setHeader("X-Accel-Limit-Rate", "1024");//限速,单位字节,默认不限
        response.setHeader("X-Accel-Buffering", "yes");//是否使用Nginx缓存,默认yes
    }

如果直接访问路径:http://localhost:9876/testAccel/1234.jpg,就会报404错误

参考:

https://www.zhangbj.com/p/507.html

分类: springmvc , nginx

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值