前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >nginx实现文件上传和下载

nginx实现文件上传和下载

作者头像
子润先生
修改于 2021-08-03 02:44:25
修改于 2021-08-03 02:44:25
11.1K00
代码可运行
举报
运行总次数:0
代码可运行

这篇文章给大家分享的是nginx实现文件上传和下载的方法。小编觉得挺实用的,因此分享给大家学习。如下资料是关于实现文件上传和下载的方法的内步骤。

系统版本:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@vhost8 local]# cat /etc/redhat-release 
CentOS Linux release 7.5.1804 (Core) 

用户准备:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
useradd -s /sbin/nologin -M nginx

目录及权限准备

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@vhost8 local]#
mkdir -p /export/share/upload/
mkdir -p /export/tmp/upload/
cd /export/tmp/upload/
mkdir 0  1  2  3  4  5  6  7  8  9  state
chown -R nginx:nginx /export/
[root@vhost8 export]# tree                        
.                                                 
├── share                                         
│   └── upload                                    
│       ├── eclipse-cpp-2019-09-R-win32-x86_64.zip    
└── tmp                                           
    └── upload                                    
        ├── 0                                     
        │   ├── 0001559170                                            
        ├── 1                                     
        │   ├── 0000000001                                            
        ├── 2                                                      
        ├── 3                                                                      
        ├── 4                                                                          
        ├── 5                                     
        ├── 6                                                    
        ├── 7                                                                            
        ├── 8                                                                     
        ├── 9                                                                             
        └── state                                 
15 directories, 26 files
[root@vhost8 local]# 
nginx 安装:
yum remove nginx
yum -y install gcc gcc-c++ autoconf automake gd gd-devel zlib zlib-devel openssl openssl-devel pcre-devel 
mkdir /root/thb
cd /root/thb
wget http://nginx.org/download/nginx-1.17.7.tar.gz
git clone https://github.com/hongzhidao/nginx-upload-module.git
git clone https://github.com/masterzen/nginx-upload-progress-module.git
tar -xzvf nginx-1.17.7.tar.gz 
cd nginx-1.17.7/
./configure  --with-debug --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --add-module=/root/thb/nginx-upload-module --add-module=/root/thb/nginx-upload-progress-module --with-stream --with-http_image_filter_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-file-aio --with-cc-opt='-Wno-format-security -Wno-unused-but-set-variable -Wno-unused-result -D NGX_HAVE_OPENSSL_MD5_H=1 -D NGX_OPENSSL_MD5=1 -D NGX_HAVE_OPENSSL_SHA1_H=1 -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2  -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
make && make install
修改nginx配置:
cd /etc/nginx/
vi nginx.conf
[root@vhost8 nginx]# cat nginx.conf
worker_processes  4;  
#error_log  logs/error.log;  
#error_log  logs/error.log  notice;  
#error_log  logs/error.log  info;  
#pid        logs/nginx.pid;  
events {  
    worker_connections  1024;  
}  
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;  
    #tcp_nopush     on;  
    #keepalive_timeout  0;  
    keepalive_timeout  65;  
    upload_progress proxied 8m;  
    #gzip  on;  
    server {
        listen       80;
        auth_basic "Please input password"; #这里是验证时的提示信息
        auth_basic_user_file /etc/nginx/passwd/testpwd;
        # upload
        client_max_body_size 100g; # 这个配置表示最大上传大小,但是我没有验证过是否能传 100g 的文件
        # Upload form should be submitted to this location
        location /upload {
                # Pass altered request body to this location
                upload_pass /upload.php;
                # 开启resumable
                upload_resumable on;
                # Store files to this directory
                # The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
                # 记得修改目录的读写权限
                upload_store /export/tmp/upload 1;
                upload_state_store /export/tmp/upload/state;
                # Allow uploaded files to be read by all
                upload_store_access all:r;
                # Set specified fields in request body
                upload_set_form_field "${upload_field_name}_name" $upload_file_name;
                upload_set_form_field "${upload_field_name}_content_type" $upload_content_type;
                upload_set_form_field "${upload_field_name}_path" $upload_tmp_path;
                # Inform backend about hash and size of a file
                upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
                upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
                upload_pass_form_field "^submit$|^description$";
        }
        location ~ \.php$ {
           # fastcgi_pass   unix:/run/php-fpm/php-fpm.sock;
           fastcgi_pass   127.0.0.1:9000;
           fastcgi_index  index.php;
           # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }
        location /myfiles {
            alias /export/share/upload/;        # 文件存放目录,注意要以 '/' 结尾;
            index index.html;               # 如果文件存放目录有 index.html,会跳转到 index.html;
            autoindex on;               # 自动列出目录下的文件;
            autoindex_exact_size off;   # 文件大小按 GM 的格式显示,而不是 Bytes;
        }
    }
}

[root@vhost8 local]# 

编辑上传文件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@vhost8 local]# 
cd /etc/nginx/html
vi upload.php 
[root@vhost8 html]# cat upload.php 
<?php
$header_prefix = 'file';
$slots = 6;
?>
<html>
<head>
<title>Test upload</title>
</head>
<body>
<?php
if ($_POST){
    echo "<h3>Uploaded files:</h3>";
    echo "<table border=\"2\" cellpadding=\"2\">";
    echo "<tr><td>Name</td><td>Location</td><td>Content type</td><td>MD5</td><td>Size</td><td>Scp Command</td><td>Wget Command</tr>";
    for ($i=1;$i<=$slots;$i++){
        $key = $header_prefix.$i;
        if (array_key_exists($key."_name", $_POST) && array_key_exists($key."_path",$_POST)) {
            $tmp_name = $_POST[$key."_path"];
            $name = $_POST[$key."_name"];
            $content_type = $_POST[$key."_content_type"];
            $md5 = $_POST[$key."_md5"];
            $size = $_POST[$key."_size"];
            $final_path = "/export/share/upload";
            if (copy($tmp_name, "$final_path/$name")) {
                    echo "SUCCESS!";
            } else {
                    echo "FAIL!";
            }
            $scp_cmd = "scp team@***:/export/share/upload/$name .";
            $wget_cmd = "wget http://***/files/upload/$name";
            echo "<tr><td>$name</td><td>$final_path</td><td>$content_type</td><td>$md5</td><td>$size</td><td>$scp_cmd</td><td>$wget_cmd</td>";
        }
    }
    echo "</table>";
}else{?>
<h3>Select files to upload</h3>
<form name="upload" method="POST" enctype="multipart/form-data" action="/upload">
<input type="file" name="file1"><br>
<input type="file" name="file2"><br>
<input type="file" name="file3"><br>
<input type="file" name="file4"><br>
<input type="file" name="file5"><br>
<input type="file" name="file6"><br>
<input type="submit" name="submit" value="Upload">
<input type="hidden" name="test" value="value">
</form>
<?php
}
?>
</body>
</html>
[root@vhost8 local]# 

增加nginx 网页登录验证

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
yum  -y install httpd-tools
mkdir -p /etc/nginx/passwd/
htpasswd -c /etc/nginx/passwd/testpwd user1

输入2遍user1 登录密码

增加nginx启动文件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@vhost8 local]# 
vi /lib/systemd/system/nginx.service
[root@vhost8 local]# cat /lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@vhost8 local]# 
php安装
[root@vhost8 nginx]# 
yum -y install gcc automake autoconf libtool make
yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel
cd ~
wget https://museum.php.net/php5/php-5.4.7.tar.gz
tar -xzvf php-5.4.7.tar.gz 
cd php-5.4.7
./configure --prefix=/usr/local/php  --enable-fpm --with-mcrypt --enable-mbstring --disable-pdo --with-curl --disable-debug  --disable-rpath --enable-inline-optimization --with-bz2  --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --with-mhash --enable-zip --with-pcre-regex --with-mysql --with-Mysqli --with-gd --with-jpeg-dir
make all install
cd /usr/local/php
cp etc/php-fpm.conf.default etc/php-fpm.conf
vi etc/php-fpm.conf

修改

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
user = nginx  
group = nginx         

启动php-fpm服务:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
cd /usr/local/php
./sbin/php-fpm &

启动nginx:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[root@vhost8 local]# 
systemctl enable nginx.service
systemctl start nginx
systemctl status nginx

文件浏览:

http://192.168.187.137/myfiles/

文件上传:

http://192.168.187.137/upload.php

关于nginx实现文件上传和下载的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
lnmp应用服务器安装手册
-创建配置文件目录/usr/local/webserver/nginx/conf/vhosts
用户7657330
2020/08/14
9350
CentOS下Nginx+PHP7 安装及配置
yum install pcre pcre-devel openssl openssl-devel -y
星哥玩云
2022/07/19
5020
Java实战
原因:设计实现电商网站 说明:DIY项目 环境部署 JDK安装配置 rpm -qa | grep jdk //清理自带jdk sudo chmod 777 jdk-7u80-linux-x64.rpm sudo rpm -ivh jdk-7u80-linux-x64.rpm sudo vim /etc/profile //插入以下的配置 export JAVA_HOME=/usr/java/jdk1.7.0_80 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:
ZHaos
2019/02/27
1K0
CentOS 7.5下搭建高可用的FastDFS分布式文件系统
FastDFS是一个开源的轻量级分布式文件系统,由跟踪服务器(tracker server)、存储服务器(storage server)和客户端(client)三个部分组成,主要解决了海量数据存储问题,特别适合以中小文件(建议范围:4KB < file_size <500MB)为载体的在线服务。
星哥玩云
2022/07/24
5320
CentOS 7.5下搭建高可用的FastDFS分布式文件系统
FastDFS分布式文件上传系统的搭建
https://sourceforge.net/projects/fastdfs/
lyb-geek
2018/10/24
1.8K0
FastDFS分布式文件上传系统的搭建
LNMP_nginx安装_3
cd /usr/local/src/ wget http://nginx.org/download/nginx-1.6.2.tar.gz tar zxvf nginx-1.6.2.tar.gz cd nginx-1.6.2 ./configure   --prefix=/usr/local/nginx   --with-pcre 
py3study
2020/01/13
5560
编译安装Nginx + PHP + MySQL
编译安装 Nginx # 创建用户和组 groupadd nginx useradd -s /sbin/nologin nginx # 编译安装 ./configure --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/ng
陳斯托洛夫斯記
2022/10/27
1.1K0
php7详细安装教程(linux + nginx +PHP + mysql)
php下载网站:http://cn2.php.net/downloads.php 选择你需要的版本.
conanma
2021/12/02
2.7K0
02.docker镜像制作
基于centos7制作docker镜像 docker镜像是企业非常常用的一种应用打包,应用交付的方式. docker天生优势,一处构建处处运行,在任何机器构建的服务均可以在任意一台安装有docker的主机上运行 1. docker构建nginx镜像 1. 首先安装centos7平台环境 docker run -itd --name centos7 cenots:7 docker run -it --name centos7 centos:7 2. 进入cenots7 docker exec -it cen
陈雷雷
2020/03/18
1.5K0
02.docker镜像制作
LNMP环境搭建
Nginx的PHP安装和LAMP环境搭建中的PHP安装是有区别的。因为Nginx中的PHP是以fastcgi的方式结合Nginx的,而httpd是把PHP作为自己的模块来调用的。
刘銮奕
2019/07/22
2.3K0
CentOS 7源码安装最新版LNMP环境
首先做一些准备工作,先把centos7的防火墙更换成iptables,可以参见如下链接
botkenni
2022/01/21
5520
配置nginx + php7 + mongodb的centos服务器环境
重构、重构、重构 大三下学期了,面试某公司,结果连技术面都没过,╮(╯▽╰)╭,还是有一点打击的。不过也有打算重构一下两个小项目的,希望以此叩开实习的大门。记录一下全过程! 重构前 这部分主要是服务器的搭建。 之前Github学生认证,送了DigitalOcean 100刀,买了一台位于新加坡节点的512 MB Memory / 20 GB Disk / SGP1 - CentOS 6.7 x64 编译nginx mongodb … 前的准备 yum updateyum install wget gcc g
许杨淼淼
2018/07/11
9470
单机部署 LNMP
``关闭SELinux # 临时关闭selinux setenforce 0 # 永久关闭selinux sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config 关闭防火墙 # 临时关闭防火墙: systemctl stop firewalld # 永久关闭防火墙: systemctl disable firewalld 1. 源码部署nginx 1.1 下载源码 # -c 是断点续传 wget -c http://
萌海无涯
2021/03/16
9220
CentOS 7.5 + PHP 5.6.36 + Nginx 1.14.0 配置笔记
Nginx 配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。main 部分设置的指令将影响其它所有部分的设置;server 部分的指令主要用于指定虚拟主机域名、IP 和端口;upstream 的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location 部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系式:server 继承 main,location 继承 server;upstream 既不会继承指令也不会被继承,它有自己的特殊指令,不需要在其他地方的应用。
赵达
2018/07/16
1.5K0
LNMP环境搭建Zabbix监控平台自动化安装脚本
此脚本是LNMP环境搭建Zabbix监控平台自动化安装脚本,有需要朋友可以参考,脚本内容如下:
子润先生
2021/07/06
3490
LNMP+FastCGI平台搭建脚本
cmake-2.8.10.2.tar.gz libevent-2.0.21-stable.tar.gz libmcrypt-2.5.8.tar.gz mhash-0.9.9.9.tar.gz nginx-1.2.0.tar.gz php-5.4.9.tar.gz libiconv-1.13.tar.gz MySQL-5.5.13.tar.gz
星哥玩云
2022/07/03
3130
centos7.4安装LNMP
系统最小化安装,只安装了一些常用包(vim、lirzs、gcc*、wget、bash-completion)
萧晚歌
2020/08/19
9620
centos6.5编译安装LNMP架构web环境
作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率。
用户3094376
2018/09/12
1.7K1
CentOS5安装Nginx1.4+PHP5.5 FastCGI
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel ssse2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers gettext-devel libXpm-devel libtool libevent libevent-devel gd-devel gd libmcrypt libmcrypt-devel pcre pcre-devel 
星哥玩云
2022/06/28
3240
CentOS5安装Nginx1.4+PHP5.5 FastCGI
zabbix-web切换为nginx及https
zabbix使用了很久,安装的时候并没有选择复杂的源码编译安装,所以默认采用了apache的httpd提供web服务。由于对httpd并没有深入研究,而且个人对httpd的配置文件格式很不感冒,怎么办?当然是换nginx呀!顺便加上https证书安全安全。 本文中的环境如下: 系统版本:CentOS Linux release 7.4.1708 (Core) 软件版本: zabbix 4.0.0 nginx 1.16.0 php 5.6.40
仙人技术
2020/04/28
1.4K0
相关推荐
lnmp应用服务器安装手册
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验