热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

nginx+lua实现文件上传下载

当前环境011:26:57root@test-a,172.16.15.21:~#catetcredhat-releaseCentOSLinuxrelease7.9.2009(Cor
  1. 当前环境

      0 11:26:57 root@test-a,172.16.15.21:~ # cat /etc/redhat-release 
    CentOS Linux release 7.9.2009 (Core)
      0 11:27:08 root@test-a,172.16.15.21:~ # uname -r
    3.10.0-1160.36.2.el7.x86_64
    
  2. 下载相关的安装包,并解压。

      0 11:27:14 root@test-a,172.16.15.21:~ # cd /usr/local/src/
      0 11:27:28 root@test-a,172.16.15.21:/usr/local/src # wget http://nginx.org/download/nginx-1.18.0.tar.gz
      0 11:27:39 root@test-a,172.16.15.21:/usr/local/src # wget https://github.com/vkholodkov/nginx-upload-module/archive/refs/tags/2.3.0.tar.gz
      0 11:28:06 root@test-a,172.16.15.21:/usr/local/src # wget https://github.com/openresty/luajit2/archive/refs/tags/v2.1-20210510.tar.gz
      0 11:28:13 root@test-a,172.16.15.21:/usr/local/src # wget https://github.com/openresty/lua-resty-upload/archive/refs/tags/v0.10.tar.gz
      0 11:28:58 root@test-a,172.16.15.21:/usr/local/src # wget https://github.com/openresty/lua-nginx-module/archive/refs/tags/v0.10.14.tar.gz
      0 11:29:42 root@test-a,172.16.15.21:/usr/local/src # tar xf nginx-1.18.0.tar.gz 
      0 11:29:53 root@test-a,172.16.15.21:/usr/local/src # tar xf 2.3.0.tar.gz
      0 11:29:57 root@test-a,172.16.15.21:/usr/local/src # tar xf v0.10.14.tar.gz
      0 11:30:01 root@test-a,172.16.15.21:/usr/local/src # tar xf v0.10.tar.gz
      0 11:30:17 root@test-a,172.16.15.21:/usr/local/src # tar xf v2.1-20210510.tar.gz
    
  3. 安装配置各类依赖。

      0 11:30:25 root@test-a,172.16.15.21:/usr/local/src/luajit2-2.1-20210510 # yum -y install pcre-devel openssl-devel
      0 11:32:17 root@test-a,172.16.15.21:/usr/local/src/luajit2-2.1-20210510 # make PREFIX=/usr/local/luajit2
      0 11:32:48 root@test-a,172.16.15.21:/usr/local/src/luajit2-2.1-20210510 # make install PREFIX=/usr/local/luajit2
    
  4. 编译安装nginx。

      0 11:33:53 root@test-a,172.16.15.21:/usr/local/src/luajit2-2.1-20210510 # cd ../nginx-1.18.0/
      0 11:34:24 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # export LUAJIT_LIB=/usr/local/luajit2/lib
      0 11:34:45 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # export LUAJIT_INC=/usr/local/luajit2/include/luajit-2.1
      0 11:35:11 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # useradd -M -s /sbin/nologin www
      0 11:38:35 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # ./configure --user=www --group=www --prefix=/usr/local/nginx --with-stream --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_v2_module --with-pcre --with-ld-opt=-Wl,-rpath,/usr/local/luajit2/lib --add-module=/usr/local/src/nginx-upload-module-2.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.14
      0 11:39:26 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # make && make install
    
  5. 配置resty。

      0 11:41:20 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # mkdir -p /usr/local/luajit2/lib/lua/5.1/resty
      0 11:41:44 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # cp ../lua-resty-upload-0.10/lib/resty/upload.lua /usr/local/luajit2/lib/lua/5.1/
      0 11:42:17 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # vim /usr/local/luajit2/lib/lua/5.1/my_upload.lua
    local upload = require "resty.upload"
    
    local function my_get_file_name(header)
        local file_name
        for i, ele in ipairs(header) do
            file_name = string.match(ele, 'filename="(.*)"')
            if file_name and file_name ~= '' then
                return file_name
            end
        end
        return nil
    end
    
    local chunk_size = 4096
    local form = upload:new(chunk_size)
    local file
    local file_path
    while true do
        local typ, res, err = form:read()
    
        if not typ then
             ngx.say("failed to read: ", err)
             return
        end
    
        if typ == "header" then
            local file_name = my_get_file_name(res)
            if file_name then
                file_path = ngx.var.store_path..file_name
                file = io.open(file_path, "w+")
                if not file then
                    ngx.say("failed to open file ", file_path)
                    return
                end
            end
    
        elseif typ == "body" then
            if file then
                file:write(res)
            end
    
        elseif typ == "part_end" then
            if file then
                file:close()
                file = nil
                ngx.say("upload to "..file_path.." successfully!")
            end
        elseif typ == "eof" then
            break
    
        else
            -- do nothing
        end
    end
    
  6. 配置并启动nginx。

    # 安全起见,配置访问密码。
      0 11:52:40 root@test-a,172.16.15.21:/usr/local/src/nginx-1.18.0 # cd /usr/local/nginx/
      0 11:52:48 root@test-a,172.16.15.21:/usr/local/nginx # echo -n 'merle:' >>conf/.httppasswd
      0 11:53:15 root@test-a,172.16.15.21:/usr/local/nginx # openssl passwd -apr1 >>conf/.httppasswd
      0 11:53:35 root@test-a,172.16.15.21:/usr/local/nginx # vim conf/nginx.conf
    user  www www;
    worker_processes  auto;
    error_log  logs/error.log;
    pid        logs/nginx.pid;
    
    events {
        use epoll;
        worker_connections  65535;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        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  logs/access.log  main;
        sendfile        on;
        keepalive_timeout  65;
        lua_package_path '/usr/local/luajit2/lib/lua/5.1/?.lua;;';
    
        server {
            listen       8888;
            server_name  localhost;
            client_max_body_size 500m;
    
            # auth
            auth_basic "security download area";
            auth_basic_user_file "/usr/local/nginx/conf/.httppasswd";
    
            # download
            autoindex on;
            autoindex_localtime on;
    
            location / {
                root   /server/web_files;		# 文件存储目录
            }
    
            location ~ ^/upload(/.*)?$ {
                set $store_path /server/web_files$1/;		# 文件存储目录
                content_by_lua_file /usr/local/luajit2/lib/lua/5.1/my_upload.lua;
            }
        }
    }
      0 11:59:03 root@test-a,172.16.15.21:/usr/local/nginx # \rm -r logs
      0 12:00:01 root@test-a,172.16.15.21:/usr/local/nginx # mkdir -p /server/logs/nginx /server/web_files
      0 12:00:06 root@test-a,172.16.15.21:/usr/local/nginx # ln -sf /server/logs/nginx /usr/local/nginx/logs
      0 12:00:12 root@test-a,172.16.15.21:/usr/local/nginx # ln -s /usr/local/nginx/sbin/nginx /usr/bin/
      0 12:00:39 root@test-a,172.16.15.21:/usr/local/nginx # nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
      0 12:01:24 root@test-a,172.16.15.21:/usr/local/nginx # nginx
      0 12:01:30 root@test-a,172.16.15.21:/usr/local/nginx # netstat -lntp | grep 8888
    tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      9529/nginx: master
    # 创建几个测试文件,下面测试使用。
      0 12:05:41 root@test-a,172.16.15.21:/server/web_files # touch info{1..5}.txt
      0 12:06:39 root@test-a,172.16.15.21:/server/web_files # echo 'hello world' >info1.txt
    # 为文件存储目录授权,否则等下上传不上(root启动nginx请忽略)。
      0 12:07:32 root@test-a,172.16.15.21:/server/web_files # chown -R www. /server/web_files
    
  7. 测试访问(为了区分,使用内网其它主机访问测试)。

      0 12:09:54 root@test-b,172.16.15.22:~ # curl -u "merle:123456" http://172.16.15.21:8888
    
    
    
    

    Index of /


    ../
    info1.txt                                          23-Sep-2021 12:05                   0
    info2.txt                                          23-Sep-2021 12:05                   0
    info3.txt                                          23-Sep-2021 12:05                   0
    info4.txt                                          23-Sep-2021 12:05                   0
    info5.txt                                          23-Sep-2021 12:05                   0
    

    # 使用命令下载文件。 0 12:13:24 root@test-b,172.16.15.22:~ # wget --user=merle --password="123456" -c -P /tmp http://172.16.15.21:8888/info1.txt --2021-09-23 12:13:30-- http://172.16.15.21:8888/info1.txt 正在连接 172.16.15.21:8888... 已连接。 已发出 HTTP 请求,正在等待回应... 401 Unauthorized 再次使用存在的到 172.16.15.21:8888 的连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:12 [text/plain] 正在保存至: “/tmp/info1.txt” 100%[==============================================================================================>] 12 --.-K/s 用时 0s 2021-09-23 12:13:30 (1.20 MB/s) - 已保存 “/tmp/info1.txt” [12/12]) 0 12:13:30 root@test-b,172.16.15.22:~ # cat /tmp/info1.txt hello world # 使用命令上传文件。 0 12:40:35 root@test-b,172.16.15.22:~ # curl -u "merle:123456" -F filea=@testaaa http://172.16.15.21:8888/upload upload to /server/web_files/testaaa successfully! # 在服务端查看文件 0 12:42:49 root@test-a,172.16.15.21:/server/web_files # ll testaaa -rw-rw-rw- 1 www www 12 9月 23 12:40 testaaa 0 12:42:56 root@test-a,172.16.15.21:/server/web_files # cat testaaa How are you


写作不易,转载请注明出处,谢谢~~


推荐阅读
  • centos6.8 下nginx1.10 安装 ... [详细]
  • linux下编译安装lnmp
    2019独角兽企业重金招聘Python工程师标准#######################安装依赖#####################安装必要的包:y ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文介绍了在Windows环境下如何配置php+apache环境,包括下载php7和apache2.4、安装vc2015运行时环境、启动php7和apache2.4等步骤。希望对需要搭建php7环境的读者有一定的参考价值。摘要长度为169字。 ... [详细]
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 通过Anaconda安装tensorflow,并安装运行spyder编译器的完整教程
    本文提供了一个完整的教程,介绍了如何通过Anaconda安装tensorflow,并安装运行spyder编译器。文章详细介绍了安装Anaconda、创建tensorflow环境、安装GPU版本tensorflow、安装和运行Spyder编译器以及安装OpenCV等步骤。该教程适用于Windows 8操作系统,并提供了相关的网址供参考。通过本教程,读者可以轻松地安装和配置tensorflow环境,以及运行spyder编译器进行开发。 ... [详细]
  • 本文介绍了在无法联网的情况下,通过下载rpm包离线安装zip和unzip的方法。详细介绍了如何搜索并下载合适的rpm包,以及如何使用rpm命令进行安装。 ... [详细]
  • 负载均衡_Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分
    nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解 ... [详细]
  • centos php部署到nginx 404_NodeJS项目部署到阿里云ECS服务器全程详解
    本文转载自:http:www.kovli.com20170919ecs-deploy作者:Kovli本文详细介绍如何部署NodeJS项目到阿里云ECS上, ... [详细]
  • 文本处理与软件管理
    1、自建yum仓库,分别为网络源和本地源本地源:       网络源:[root@openvpn~]#catetcyum.repos.dCentOS-Base.repo#CentO ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • Windows 7 部署工具DISM学习(二)添加补丁的步骤详解
    本文详细介绍了在Windows 7系统中使用部署工具DISM添加补丁的步骤。首先需要将光驱中的安装文件复制到指定文件夹,并进行挂载。然后将需要的MSU补丁解压并集成到系统中。文章给出了具体的命令和操作步骤,帮助读者完成补丁的添加过程。 ... [详细]
  • 本文介绍了在Mac上安装Xamarin并使用Windows上的VS开发iOS app的方法,包括所需的安装环境和软件,以及使用Xamarin.iOS进行开发的步骤。通过这种方法,即使没有Mac或者安装苹果系统,程序员们也能轻松开发iOS app。 ... [详细]
  • 本文记录了作者对x265开源代码的实现与框架进行学习与探索的过程,包括x265的下载地址与参考资料,以及在Win7 32 bit PC、VS2010平台上的安装与配置步骤。 ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
author-avatar
________葬情_231
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有