思路:

  文件下载请求 到 后台;后台判断权限,不通过则不下载;通过则设置 X-Accel-Redirect;Nginx获取“X-Accel-Redirect”后以sendfile方式从NFS读取文件并进行下载

优点:

  • 不会直接暴露文件地址 抓包工具不会抓到地址;
  • 可以控制权限;

后台示例代码:

1 @RequestMapping(value = "/offline", method = RequestMethod.GET)
2 public void doDownloadOffline(HttpServletResponse response) throws IOException {
3
4 File zipFile = new File("/Users/lixiuming/Desktop/test.json");
5 if (zipFile == null || !zipFile.exists()) {
6 response.sendError(404);
7 }
8 response.setHeader("Content-Type", "application/octet-stream");
9 // 设置转发属性
10 // /appoffline/为Nginx location 名
11 response.setHeader("X-Accel-Redirect", "/appoffline/" + zipFile.getName());
12 response.setHeader("X-Accel-Charset", "utf-8");
13 response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
14

传参示例:

springMVC文件从Nginx下载文件权限控制_文件路径

springMVC文件从Nginx下载文件权限控制_文件路径_02

1 @RequestMapping(value = "/offline", method = RequestMethod.GET)
2 public void doDownloadOffline(Integer file_id, HttpServletResponse response) throws IOException {
3 if (file_id != null && file_id != 0) {
4 File zipFile = new File("/Users/lixiuming/Desktop/test.json");
5 if (zipFile == null || !zipFile.exists()) {
6 response.sendError(404);
7 }
8 response.setHeader("Content-Type", "application/octet-stream");
9 // 设置转发属性
10 // /appoffline/为Nginx location 名
11 response.setHeader("X-Accel-Redirect", "/appoffline/" + zipFile.getName());
12 response.setHeader("X-Accel-Charset", "utf-8");
13 response.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName());
14 } else {
15 System.out.println("error");
16 }
17
18

View Code

说明:

  • /appoffline/为Nginx location 名;
  • 这里的  @RequestMapping(value = "/offline", method = RequestMethod.GET),/offline;当监听到 Nginx监听到 /download_file时,访问了后台(地址是/offline)

nginx.conf配置代码:

1   location  / {
2 root html;
3 proxy_pass https://www.baidu.com;
4 index index.html index.htm;
5 }
6
7 location = /download_file {
8 proxy_pass http://127.0.0.1:8080/offline;
9 }
10 location /appoffline/ {
11 #设置非浏览器访问
12 internal;
13 charset utf-8;
14 alias /Users/lixiuming/Desktop/;
15

 说明:当访问 http://localhost:8081/download_file?file_id=1 (不传参地址http://localhost:8081/download_file) 时,可以执行下载文件,F12 NETWORK 没有文件地址;

其他配置:root/alias

root/alias 是指定文件路径的两种方式,主要区别就是怎么解析location后面的uri。 eg: 访问:​​http://localhost/appImg/abc.jpg​

  • root:
location ^~ /appImg/{
root /home/nginx;
}

这个location相当于访问服务器上的文件路径: /home/nginx/appImg/abc.jpg 。

  • alias:
location ^~ /appImg/{
alias /home/nginx/;
}

这个location相当于访问服务器上的文件目录:/home/nginx/abc.jpg(即alias不会使用location后面配置的路径)。而且如果alias 指定的是目录,后面一定要加上 "/"。。。

补充nginx的常用命令:

Linux:

帮助命令:nginx -h
启动Nginx服务器 :sudo nginx 
查看进程: ps aux | grep nginx
配置文件路径:/usr/local/nginx/conf/nginx.conf
检查配置文件:sudo nginx -t
指定启动配置文件:sudo nginx -c /usr/local/nginx/conf/nginx.conf
暴力停止服务:sudo nginx -s stop
优雅停止服务:sudo nginx -s quit
重新加载配置文件:sudo nginx -s reload

windows:

帮助命令:nginx -h

启动Nginx服务器 start nginx或nginx.exe

停止:nginx.exe -s stop或nginx.exe -s quit

重新载入Nginx:nginx.exe -s reload

重新打开日志文件:nginx.exe -s reopen

查看版本:nginx -v

查看nginx 是否启动   tasklist /fi "imagename eq nginx.exe"(结束进程taskkill /F -pid 25428)

我从来不相信什么懒洋洋的自由。我向往的自由是通过勤奋和努力实现的更广阔的人生。 我要做一个自由又自律的人,靠势必实现的决心认真地活着。