平时应用中,咱们大都用apache搭建下载页面。毕竟Apache搭建起来很是方便,yum安装,建立目录就能够了。html
但有时仍是须要用nginx配置下载页面。这里就是一个简单的配置nginx下载页面的过程。过程简单,有须要优化的地方建议你们百度一下。nginx
首先环境准备:算法
# lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: CentOS Description: CentOS release 6.7 (Final) Release: 6.7 Codename: Final
yum install -y nginx
useradd -s /sbin/nologin -M nginx
cd /etc/nginx/
cat > nginx.conf << 'eof' user nginx; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server_tokens off; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /download { charset utf-8; root /data/; #alias /data/download/; if ($request_filename ~* ^.*?\.(txt)$){ add_header Content-Disposition 'attachment'; add_header Content-Type: 'APPLICATION/OCTET-STREAM';} autoindex on; autoindex_exact_size off; autoindex_localtime on; access_log /var/log/nginx/download.log main; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } eof
mkdir -p /data/download #chown -R nginx.nginx /data/download
#(这部无关紧要)
/etc/init.d/nginx start
浏览器输入http://IP/download/apache
查看下载浏览器
注意:app
补充一点。工具
目录下载中测试
location /download {
charset utf-8;
#root /data/; #root的意思是url 访问IP/download nginx会定向到本地目录/data/download/下。
alias /data/; # alias 意思是 url 访问IP/download nginx会定向到本地目录/data/ 下。优化
}加密
#安装 htpasswd 工具 yum -y install httpd-tools #设置用户名和密码,并把用户名、密码保存到指定文件中: htpasswd -c /etc/nginx/pass/passwd coderschool New password: Re-type new password: Adding password for user coderschool #修改 nginx 配置文件 auth_basic "Please input password"; #这里是验证时的提示信息 auth_basic_user_file /etc/nginx/pass/passwd; location /download { charset utf-8; #root /data/; alias /data/; ...... #而后重启 nginx: nginx -s reload
htpasswd命令选项参数说明 -c 建立一个加密文件 -n 不更新加密文件,只将htpasswd命令加密后的用户名密码显示在屏幕上 -m 默认htpassswd命令采用MD5算法对密码进行加密 -d htpassswd命令采用CRYPT算法对密码进行加密 -p htpassswd命令不对密码进行进行加密,即明文密码 -s htpassswd命令采用SHA算法对密码进行加密 -b htpassswd命令行中一并输入用户名和密码而不是根据提示输入密码 -D 删除指定的用户
htpasswd例子 a、如何利用htpasswd命令添加用户? htpasswd -bc ./.passwd tonyzhang pass
在当前目录下生成一个.passwd文件,用户名tonyzhang ,密码:pass,默认采用MD5加密方式 b、如何在原有密码文件中增长下一个用户? htpasswd -b ./.passwd onlyzq pass
去掉c选项,便可在第一个用户以后添加第二个用户,依此类推 c、如何不更新密码文件,只显示加密后的用户名和密码? htpasswd -nb tonyzhang pass
不更新.passwd文件,只在屏幕上输出用户名和通过加密后的密码 d、如何利用htpasswd命令删除用户名和密码? htpasswd -D .passwd tonyzhang
e、如何利用 htpasswd 命令修改密码? htpasswd -D .passwd tonyzhang htpasswd -b .passwd tonyzhang pass