词条信息

admin
超级管理员
版本创建者 发短消息   
简易百科旧版 >> nginx配置下载目录 >> 历史版本

最新历史版本 :nginx配置下载目录 返回词条



很多场景下,我们需要对用户提供文件下载功能,比如不同版本的软件列表,或者对外提供的一些公开的报表下载。


例如linux提供的内核源码下载列表界面如下:



配置实战需求描述


已有系统,服务端使用的是nginx服务器,域名为www.***.com,是一个php项目。现在要在该域名上实现一个文件下载的目录,所有要下载的文件都放到/home/havy/download/。


实现方案

在原有nginx配置中增加location模块,对www.***.com/attach访问路径设置为下载目录根目录/home/havy/download/,并且对该location块开启目录文件列表,详细配置如下:


server{

listen80;

server_namewww.** *.com;

#此模块为新加配置

location~ ^/attach{

root/home/havy/webroot/;

autoindexon; # 开启目录文件列表

autoindex_exact_sizeon; # 显示出文件的确切大小,单位是bytes

autoindex_localtimeon; # 显示的文件时间为文件的服务器时间

charsetutf- 8,gbk; # 避免中文乱码

}

location~ ^/(favicon.ico|static){

root/home/www/yuhaiwei/odp/webroot;

}

location~ .php${

root/home/havy/webroot;

fastcgi_pass$php_upstream;

fastcgi_indexindex.php;

includefastcgi.conf;

}

location/ {

root/home/havy/webroot;

indexindex.php;

fastcgi_pass$php_upstream;

includefastcgi.conf;

rewrite^/([^/.]*)(/[^?]*)?((?.*)?)$/ $1/index.php $2$3break;

}

}


这样,当访问www.***.com/attach就展现出/home/havy/download/目录下文件列表了。