nginx实现文件上传和下载

发布时间:2020-06-05 16:45:27 来源:亿速云 阅读:156 作者:Leah 栏目:系统运维

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

系统版本:

[root@vhost8 local]# cat /etc/redhat-release

CentOS Linux release 7.5.1804 (Core)

用户准备:

useradd -s /sbin/nologin -M nginx

目录及权限准备

[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;   # 文件大小按 G、M 的格式显示,而不是 Bytes;

}

}

}

[root@vhost8 local]#

编辑上传文件

[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 网页登录验证

yum  -y install httpd-tools

mkdir -p /etc/nginx/passwd/

htpasswd -c /etc/nginx/passwd/testpwd user1

输入2遍user1 登录密码

增加nginx启动文件

[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

修改

user = nginx

group = nginx

启动php-fpm服务:

cd /usr/local/php

./sbin/php-fpm &

启动nginx:

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

nginx实现文件上传和下载的更多相关文章

  1. Nginx集群之WCF大文件上传及下载(支持6G传输)

    目录 1       大概思路... 1 2       Nginx集群之WCF大文件上传及下载... 1 3       BasicHttpBinding相关配置解析... 2 4       编写 ...

  2. 七、springBoot 简单优雅是实现文件上传和下载

    前言 好久没有更新spring Boot 这个项目了.最近看了一下docker 的知识,后期打算将spring boot 和docker 结合起来.刚好最近有一个上传文件的工作呢,刚好就想起这个脚手架 ...

  3. 全网最简单的大文件上传与下载代码实现(React+Go)

    前言 前段时间我需要实现大文件上传的需求,在网上查找了很多资料,并且也发现已经有很多优秀的博客讲了大文件上传下载这个功能. 我的项目是个比较简单的项目,并没有采用特别复杂的实现方式,所以我这篇文章的目 ...

  4. java web学习总结(二十四) -------------------Servlet文件上传和下载的实现

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  5. (转载)JavaWeb学习总结(五十)——文件上传和下载

    源地址:http://www.cnblogs.com/xdp-gacl/p/4200090.html 在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传 ...

  6. JavaWeb学习总结,文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

  7. java文件上传和下载

    简介 文件上传和下载是java web中常见的操作,文件上传主要是将文件通过IO流传放到服务器的某一个特定的文件夹下,而文件下载则是与文件上传相反,将文件从服务器的特定的文件夹下的文件通过IO流下载到 ...

  8. 使用jsp/servlet简单实现文件上传与下载

    使用JSP/Servlet简单实现文件上传与下载    通过学习黑马jsp教学视频,我学会了使用jsp与servlet简单地实现web的文件的上传与下载,首先感谢黑马.好了,下面来简单了解如何通过使用 ...

  9. JavaWeb学习总结(五十)——文件上传和下载

    在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...

随机推荐

  1. Kubernetes CoreDNS 状态是 CrashLoopBackOff 报错

    查看状态的时候,遇见coredns出现crashlookbackoff,首先我们来进行排错,不管是什么原因,查看coredns的详细信息,以及logs [root@k8s-master coredns ...

  2. 使用call、apply、bind继承及三者区别

    js里的继承方法有很多,比如:使用原型链的组合继承.es6的Class.寄生继承以及使用call.apply.bind继承.再说继承之前,我们先简单了解下它们的区别. 一.区别: 同:三者都是改变函数 ...

  3. 前端基础功能,原生js实现轮播图实例教程

    轮播图是前端最基本.最常见的功能,不论web端还是移动端,大平台还是小网站,大多在首页都会放一个轮播图效果.本教程讲解怎么实现一个简单的轮播图效果.学习本教程之前,读者需要具备html和css技能,同 ...

  4. 二十五:XSS跨站值原理分类及攻击手法

    HTML DOM树 XSS跨站产生原理,危害,特点 本质,产生层面,函数类,漏洞操作对应层,危害影响,浏览器内核版本 XSS是什么? XSS全称跨站脚本(Cross Site Scripting),为 ...

  5. 探索微软开源Python自动化神器Playwright

    相信玩过爬虫的朋友都知道selenium,一个自动化测试的神器工具.写个Python自动化脚本解放双手基本上是常规的操作了,爬虫爬不了的,就用自动化测试凑一凑. 虽然selenium有完备的文档,但也 ...

  6. Python干货:了解元组与列表的使用和区别

    元组是 Python 对象的集合,跟列表十分相似.下面进行简单的对比. 列表与元组 1.python中的列表list是变量,而元组tuple是常量. 列表:是使用方括号[],元组:则是使用圆括号() ...

  7. LocalDateTime去掉T

    最近在使用阿里巴巴的fastjson反序列化对象的时候,对象里面时间格式属性总是会多了一个T  2021-1-09T18:29:09.097 这个T是啥国际标准,但是我们的前端又不需要这个T,所以就要 ...

  8. etcd 与 Zookeeper、Consul 等其它 kv 组件的对比

    基于etcd的分布式配置中心 etcd docs | etcd versus other key-value stores https://etcd.io/docs/v3.4.0/learning/w ...

  9. XCTF-基础Android

    前期工作 查壳,无.打开看是普通的输入注册码然后验证. 逆向分析 这题太简单了,就不贴图了.看Manifest得知MainActivity是启动类,也就是点进去输入注册码那个类.密码不重要,验证成功后 ...

  10. requestAnimationFrame小结

    背景 在Web应用中,实现动画效果的方法比较多,Javascript 中可以通过定时器 setTimeout或者setInterval 来实现,css3 可以使用 transition 和 anima ...