nginx作为下载文件服务器

来源:互联网 发布:淘宝手机充值软件利润 编辑:程序博客网 时间:2024/05/20 15:38

1.前言

当我们希望分享自己的文件时,有多种方式,局域网可以采用共享,rtx传输,qq传输,发送到邮箱,直接u盘拷贝等等。但最简单的就是开启本地服务器,其他电脑通过网页的方式直接下载,这里介绍使用nginx作为服务器进行下载

2.步骤

1.下载nginx http://nginx.org/en/download.html 目前稳定版本为1.80 解压到一个目录

2.修改配置文件

nginx.conf

点击(此处)折叠或打开

  1. #user nobody;
  2. worker_processes 1;

  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;

  6. #pid logs/nginx.pid;


  7. events {
  8.     worker_connections 1024;
  9. }


  10. http {
  11.     include mime.types;
  12.     default_type application/octet-stream;

  13.     #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14.     # '$status $body_bytes_sent "$http_referer" '
  15.     # '"$http_user_agent" "$http_x_forwarded_for"';

  16.     #access_log logs/access.log main;

  17.     sendfile on;
  18.     #tcp_nopush on;

  19.     #keepalive_timeout 0;
  20.     keepalive_timeout 65;

  21.     #gzip on;

  22.     server {
  23.         listen 8080;
  24.         server_name localhost;

  25.         #charset koi8-r;

  26.         #access_log logs/host.access.log main;

  27.         location / {
  28.             #root html;
  29.             #index index.html index.htm;

  30.             if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){
  31.             add_header Content-Disposition: 'attachment;';
  32.             }
  33.         }
  34.         #error_page 404 /404.html;

  35.         # redirect server error pages to the static page /50x.html
  36.         #
  37.         error_page 500 502 503 504 /50x.html;
  38.         location = /50x.html {
  39.             root html;
  40.         }
  41.     }
  42. }


​3.在nginx目录下的html中建立目录test和test.rar文件

4.打开命令行切换到nginx目录

4.1测试脚本 nginx -t
4.2开启服务器 start nginx
4.3打开浏览器 http://localhost:8080/test/test.rar应该弹出另存为对话框
4.4关闭服务器nginx -s quit
0 0