网站建设技术人员要求,青创云网站建设,重庆网站建站系统哪家好,工业设计网页需求
统计静态文件的下载次数#xff1b;判断用户是否有下载权限#xff1b;根据用户指定下载速度#xff1b;根据Referer判断是否需要防盗链#xff1b;根据用户属性限制下载速度#xff1b;
X-Accel-Redirect
This allows you to handle authentication, logging or …需求
统计静态文件的下载次数判断用户是否有下载权限根据用户指定下载速度根据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将允许适用于Comet和HTTP流式应用程序的无缓冲响应。将此设置为yes将允许响应被缓存。默认yes。
X-Accel-Expires
如果已传输过的文件被缓存下载设置Nginx文件缓存过期时间单位秒。默认不过期。
X-Accel-Charset
设置文件字符集默认UTF-8
使用条件
必须有Nginx作为后端服务的代理必须访问Nginx的代理地址直接访问后端服务Nginx会报404可自行配置Content-Type来控制是下载(application/octet-stream)还是展示(image/jpeg等)
代码实现
Nginx监听9876端口。Nginx代理后端服务的8080端口。设置/testAccel路径为internal指定具体文件存储的磁盘位置。后端服务接收到文件下载请求处理业务逻辑后X-Accel-Redirect到/testAccel路径。Nginx收到后端返回信息中的X-Accel-Redirect请求头接管文件下载或显示任务。请求路径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) {//统计//鉴权//判断RefererString 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