下载编译软件
# nginx download
$ wget http://nginx.org/download/nginx-1.18.0.tar.gz
# nginx upload
$ wget https://codeload.github.com/vkholodkov/nginx-upload-module/tar.gz/refs/tags/2.3.0
# nginx cache
$ git clone https://github.com/FRiCKLE/ngx_cache_purge.git
# 编译需要用到的库 安装
$ apt-get install openssl libssl-dev gcc zlib1g libpcre3 libpcre3-dev
# 解压命令
$ tar zxvf nginx-1.18.0.tar.gz
nginx编译
# 切换到 nginx-1.18.0 目录
# 安装编译 nginx
$ ./configure --prefix=/usr/local/nginx1.18 --with-pcre --with-http_ssl_module --add-module=/nginx_upload/ngx_cache_purge --add-module=/nginx_upload/nginx-upload-module-2.3.0 --with-cc-opt='-Wno-error -Wno-deprecated-declarations'
$ make && make install
# 安装成功后 会在以下位置生成文件夹
$ /use/local/nginx-1.18.0
nginx.conf 配置
# user root;
worker_processes 1;
#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;
#gzip on;
server {
listen 10086;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
client_max_body_size 100m;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
#
# ------------------------------------------ upload start
# Upload form should be submitted to this location
# Upload form should be submitted to this location
location /upload {
if ($request_method = 'GET') {
root html;
}
if ($request_method = 'POST') {
# Pass altered request body to this location
upload_pass @test;
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 /usr/local/nginx1.18/html/upload 1;
upload_limit_rate 0;
#upload_state_store /usr/local/software/nginx/upload_state;
# Allow uploaded files to be read only by user
upload_store_access user:rw group:rw all:rw;
set $upload_field_name "file";
# 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_args on;
upload_pass_form_field "^.*$";
upload_cleanup 400 404 499 500-505;
}
}
# Pass altered request body to a backend
location @test {
proxy_pass http://localhost:8288;
# return 200;
}
# ------------------------------------------ upload end
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
nginx 启动
# 切换到以下文件夹 首次启动
$ cd /usr/local/nginx1.18/sbin
$ ./nginx -c /usr/local/nginx1.18/conf/nginx.conf
$ nginx -t
$ nginx -s reload
$ netstat -lntp
nodejs 修改文件名称
const http = require('http');
const fs = require('fs');
/**
* 服务入口
*/
http.createServer((req, res) => {
var emp = new Object;
try {
let body = '';
req.on('data', chunk => {
body += chunk; // 获取form-data
});
// 返回结果;ip需要配置, emp.data 地址与nginx配置匹配
emp.resultCode = "200";
emp.resultMsg = "接口请求成功";
emp.data = "http://xx.xx.xx.xx:10086/upload/";
req.on('end', () => {
const params = parseForm(body); // json化参数
if (params.file_name != null) {
var index = params.file_name.lastIndexOf(".");
var suffix = params.file_name.substring(index);
//没有扩展名就不需要加到名称中
if (index == -1) {
suffix = "";
}
let filenameFilder = params.file_path.slice(33,34)
//文件名 = 时间戳 + 原文件md5 + 文件大小
let filename = Date.now() + "_" + params.file_md5 + "_" + params.file_size + suffix;
rename(params.file_path, filename);
//JSON.stringify用于将对象转成JSON文本,JSON.parse用于将JSON文本转成对象
console.log('-----》',filename)
emp.data = emp.data + filenameFilder + '/' + filename;
} else {
emp.resultCode = "99999";
emp.resultMsg = "上传文件异常";
emp.data = "";
}
var retval = JSON.stringify(emp);
res.end(retval);
console.log(params.file_name + " result:");
console.log(retval);
console.log("==================================================================================\n");
})
} catch (err) {
emp.resultCode = "99999";
emp.resultMsg = "异常";
emp.data = "";
var retval = JSON.stringify(emp);
res.end(retval);
}
}).listen(8288)
// 格式化参数
function parseForm(data) {
const reg = /name="([\w_]+)"\s+(.+)\s/g;
const params = {};
let matched;
while ((matched = reg.exec(data))) {
params[matched[1]] = matched[2];
}
console.log("params:");
console.log(params);
return params;
}
// 重命名方法
function rename(source, name) {
const path = require('path');
const dir = path.dirname(source);
fs.renameSync(source, path.join(dir, name));
}
前端调用

