使用nginx下载静态资源的控制

编程入门 行业动态 更新时间:2024-10-27 00:27:22

使用nginx下载<a href=https://www.elefans.com/category/jswz/34/1771395.html style=静态资源的控制"/>

使用nginx下载静态资源的控制

需求

1、静态资源放到指定文件夹下

2、nginx直接访问无法获取

3、后台走JAVA程序控制

4、用户获取仍然走nginx,不走JAVA后台

进行nginx配置

对X-Accel-Redirect理解:

X-Accel-Redirect response header makes an internal redirection to a locationblock determined by header's value returned from an upstream (backend).

/

 server {listen       80;server_name  dl.mydomain;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#这个location的使用主要是为了让用户使用ip地址直接访问,不需要再走Tomcat的端口号访问location / {proxy_pass  http://127.0.0.1:8080/;  #首先pass到应用服务器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;client_max_body_size       10m;client_body_buffer_size    128k;proxy_connect_timeout      90;proxy_send_timeout         90;proxy_read_timeout         90;proxy_buffer_size          4k;proxy_buffers              4 32k;proxy_busy_buffers_size    64k;proxy_temp_file_write_size 64k;}#这个地方是真正的静态文件的访问地址,后台java程序会控制原访问路径跳转到该路径下:http://域名/pic/文件名;此时nginx会使用alias路径代替piclocation /pic/ { charset utf-8;alias       /var/pic/; #文件的根目录(允许使用本地磁盘,NFS,NAS,NBD等)internal;#防止用户直接访问:http://域名/pic/文件名}

JAVA后台控制

/*** @author zongx* 下载文件实现类*/
@RequestMapping("/v3/download")
public class V3DownloadController {protected static final String DEFAULT_FILE_ENCODING = "ISO-8859-1";@RequestMapping(value = "/pic")public void downloadPic(HttpServletRequest request, final HttpServletResponse response) throws IOException {//读取要下载的文件的名称String name = request.getParameter("name");//读取要进行判断的request的条件String ok = request.getParameter("ok") == null ? "" :  request.getParameter("ok");//判断条件if( ok.equals("ok")) {			//获取文件的真实位置。可以通过配置文件设置前置路径,此处文件路径写死String filepath = "/var/pic/"+name;//判断文件是否存在File file = new File(filepath);if (file != null && file.exists()){//重定向到nginx的方法,主要是设置文件头xAccelRedirectFile(file, response);} else { response.sendError(404);}} else {response.sendError(404);}}protected void xAccelRedirectFile(File file, HttpServletResponse response) throws IOException {String encoding = response.getCharacterEncoding();//设置文件头的Content-Type,固定写法response.setHeader("Content-Type", "application/octet-stream");//这里获取到文件的相对路径。其中 /pic/的目的是用于nginx中进行拦截/pic/ 的URL。对应nginx里的location /pic/response.setHeader("X-Accel-Redirect", "/pic/"+ toPathEncoding(encoding, "1.jpg"));response.setHeader("X-Accel-Charset", "utf-8");//返回iso-8859-1对应的文件名称response.setHeader("Content-Disposition", "attachment; filename="+ toPathEncoding(encoding, file.getName()));//返回文件大小response.setContentLengthLong(file.length());}//如果存在中文文件名或中文路径需要对其进行编码成 iSO-8859-1//否则会导致 nginx无法找到文件及弹出的文件下载框也会乱码private String toPathEncoding(String origEncoding, String fileName) throws UnsupportedEncodingException{return new String(fileName.getBytes(origEncoding), DEFAULT_FILE_ENCODING);}}

思想:

主要思想,就是先访问JAVA后台,确定要不要进行重定向回nginx。nginx负责静态文件的返回用户,只要接收到java后台的X-Accel-Redirect信号,就返回用户文件。

更多推荐

使用nginx下载静态资源的控制

本文发布于:2024-02-12 17:05:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1688671.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:静态   资源   nginx

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!