Nginx配置实现下载文件的示例代码

ããå¶å°å¬äººè¯´ç¨nginxå®ç°æ件ä¸ä¼ ä¸è½½ï¼ä¹åçnginxå®è·µå¤§è´çå°è¿ï¼æ²¡æç»ç©¶ãæ以ä»å¤©å°±æ³ç ç©¶ä¸nginxå®ç°æ件çä¸ä¼ ä¸è½½ï¼ç´æ¥å¼æï¼æ¬å°æå¡å¯èµ·ãè¿éè®°å½ä¸éç½®å踩åè®°å½ã

ä¸ãéç½®

http {
 ...
 server: {
 # éç½®ä¸è½½
  location /download {
   root D:\\download;
   autoindex on;
   autoindex_exact_size off;
  }
 }
 ...
}

Nginx配置实现下载文件的示例代码

ããè¿æ¯ç®å½éé便æ¾çå ä¸ªæ件ï¼å¯ä»¥çå°å®ç°æåã

ããè¿é踩è¿å ä¸ªåï¼ä¸é¢æ示ä¸ï¼

1ãrootè·¯å¾éç½®é®é¢

ããåå¼å§éç½®ç alias D:\downloadï¼æ¥éï¼2020/08/14 10:36:06 [emerg] 26396#16140: invalid number of arguments in "alias" directive in D:\Program File\nginx\nginx-1.13.12/conf/nginx.conf:74

ããé£ä¹æ¥éç½® 74 è¡ï¼åç°å°äºåå·ï¼å ä¸é®é¢ä¾æ§ãæ¢æ root D:\download;ï¼æ¥éï¼2020/08/14 10:44:20 [emerg] 21376#17156: invalid number of arguments in "root" directive in D:\Program File\nginx\nginx-1.13.12/conf/nginx.conf:74

ããé®é¢ä¾æ§ï¼åæ¥åç°è·¯å¾æé®é¢ï¼åéäºï¼åºè¯¥æ¯

root D:/download;
// æè
root D:\\download;

ããåå é½æï¼å°±æ¯ \ åªæ¯ä¸ªè½¬ä¹å­ç¬¦ï¼è¦ä¹ç¨ \\ï¼è¦ä¹å°±ç¨ /

2ãrootä¸aliaså·®å«

ããokï¼è¿æ ·éç½®å¯ä»¥äºãä½æ¯å½æè¾å¥ï¼http://localhost/download/ï¼æ¥é404ï¼æçDçç®å½ä¸ä¸ºD:/download/*ï¼ç¶åå¶ä»æ件

ããåå¼å§æ¥éï¼2020/08/14 11:02:49 [error] 9928#12876: *11 CreateFile() "D:\download\/download" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: , request: "GET /download HTTP/1.1", host: "localhost"

ããçå°è¿ä¸ªè·¯å¾D:\download\/downloadè¿éæé®é¢ï¼æ以ææéç½®rootD:/download/;ï¼åé¢ç / å»æäºï¼åè¯ï¼è¿æ¯æ¥éï¼2020/08/14 11:04:15 [error] 3128#11636: *1 CreateFile() "D:\download/download" failed (2: The system cannot find the file specified), client: 127.0.0.1, server: , request: "GET /download HTTP/1.1", host: "localhost"

ããæ以èèå°ä¸æ¯è·¯å¾çé®é¢ï¼é£ä¹é®é¢åå å¨åªï¼å¨äº root ä¸ alias éç½®çåºå«ã

Nginx配置实现下载文件的示例代码

ããä¹å°±æ¯è¯´ å½ rootD:/download; æ¶ï¼ä½ è¯·æ±http://localhost/download/ï¼ä»æ¾çæ¯ï¼D:\download\download

ããèå½ alias D:/download; æ¶ï¼ä½ è¯·æ±http://localhost/download/ï¼ä»æ¾çææ¯ï¼D:\download

ããé£ä¹æææçæ¬å°è·¯å¾æ¹ä¸ä¸ï¼D:/download/download/*ï¼ç¶ååæ¯å¶ä»æ件ï¼é£ä¹å°±æåäºã

3ã中ææ件é®é¢

ããè¿æåï¼ç¹å»å¨è±æåæ件æ¯æ­£å¸¸é¢è§ä¸è½½çï¼ä¸­æåæ件æ¥éãå¨ serveræ®µä¸­æ·»å  charset utf-8; ä¹ä¸è¡ï¼æè charset gbk,utf-8; ä¹ä¸è¡ãææ¸æ¥çï¼ç¦è¯·ä¸åèµæï¼è°¢è°¢ã

ããæèè¯ä¹ï¼åºè¯¥ä¸æ¯nginxéç½®çé®é¢äºï¼å ä¸ºå¾å¤å°æ¹é½è¯´è¿æ ·é置就å¯ä»¥æ¾ç¤ºä¸­æäºãç¶åææ³å°ä¼ä¸ä¼æ¯ç³»ç»ç¼ç é®é¢å¢ï¼æç¨æ¬å°åæå¡å¨ã使ç¨chcpå½ä»¤æ¥äºä¸ä¸ç³»ç»ç¼ç ï¼æ¾ç¤º936ãä¸è¬æ­£å¸¸è½æ¾ç¤ºä¸­æçæ¯936ï¼é£è¯´æ没é®é¢ã

ããè½ç¶ä¸­ææ¾ç¤ºæ¯æ­£å¸¸çï¼ä½æ¯**çï¼ä¸ç¹è¿æ¯æ¥éï¼ä¹±ç æè404ã没åæ³ç´æ¥ç¹å»ä¸è½½ä¸­æåæ件ï¼æèæµè§ä¸­æåç®å½ã

ãã中æåæ件ä¸è½½å¯ä»¥éè¿ å¤å¶æ件åï¼ç¶åè´´å¨æµè§å¨urlåé¢ï¼å°±å¯ä»¥ä¸è½½ä¸æ¥äºï¼ä¸­æåç®å½æ¯ççæ æ³æµè§ï¼ï¼ï¼

ããåå æ¯ï¼windowsæå¡å¨ä¸­ææ¯gbkç¼ç ï¼nginxçè¿ç§é¡µé¢çhrefå°±æ¯ç¨gbkè¿è¡urlencodeçï¼ç¶åä¸è¬æµè§å¨é½æ¯ä½¿ç¨utf-8çï¼å½ç¶å°±æ²¡æ³è®¿é®äºã

4ãæ³é²æ­¢æµè§å¨èªå¨æå¼æ件èä¸æ¯ä¸è½½æ件ï¼æ·»å 

add_header Content-Disposition "attachment;";
// æææ件é½ä¸æå¼
location / {
 add_header Content-Disposition "attachment;filename*=utf-8'zh_cn'$arg_n";
}

ããä¸è¬å­å¨å¨æå¡å¨ä¸çæ件é½ä¸ä¼ç¨åæ¥çåå­ï¼æ以ä¸è½½çæ¶åéè¦æ¹åï¼å¯éè¿url中添å åæ°ï¼ç¶åå¨nginx中根æ®åæ°æ¥æ¹åã

ãã举ä¾ï¼åæ°ä¸º n=xxx.jpg

ããurlï¼https"//xxxxx?n=xxx.jpg //表示æä¸è½½ä¸æ¥çæ件åå­æ¹ä¸ºxxx.jpg

ããnginxéè¦éç½®æ

location / {
ããif ($request_uri ~* ^.*\?n=([^&]+)$) {
ããããadd_header Content-Disposition "filename*=$arg_n";
ãã}
}

ãã注éï¼éé¢çutf-8æ¯æäºæµè§å¨ä¼èªå¨ææ件æ¹æurlencodeçæ ¼å¼

ããä¸è¬1,2é½æ¯è¦ä¸èµ·éç½®ç

location / {
ããif ($request_uri ~* ^.*\?n=([^&]+)$) {
ããããadd_header Content-Disposition "attachment;filename*=$arg_n";
ãã}
}

å°æ­¤è¿ç¯å³äºNginxéç½®å®ç°ä¸è½½æ件ç示ä¾ä»£ç çæç« å°±ä»ç»å°è¿äº,æ´å¤ç¸å³Nginx ä¸è½½æ件å容请æç´¢èæ¬ä¹å®¶ä»¥åçæç« æ继续æµè§ä¸é¢çç¸å³æç« å¸æ大家以åå¤å¤æ¯æèæ¬ä¹å®¶ï¼

相关推荐