nginx服务拓展

需求:Nginx 文件上传模块nginx-upload-module安装,使nginx支持http请求通过post上传文件至nginx。

准备

nginx包和nginx-upload-module包

wget https://gitee.com/DBVV/note/raw/master/nginx/nginx-1.27.tar.gz

wget https://gitee.com/DBVV/note/raw/master/nginx/nginx-upload-module.zip

环境准备

yum -y install make wget git zlib zlib-devel gcc-c++ libtool 

解压

tar- zxvf nginx-1.27.tar.gz

tar- zxvf nginx-upload-module.zip

编译nginx

cd nginx-1.27.0

./configure --prefix=/usr/local/nginx --add-module=../nginx-upload-module-master

make

make install

前往nginx配置文件目录修改配置(文件路径在之前编译的--prefix=指定处)

在nnginx.conf中添加以下配置:

user root;
worker_processes  1;

pid        logs/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
load_module /usr/local/nginx/modules/ngx_http_upload_module.so;
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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  /usr/local/nginx/logs/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /usr/local/nginx/conf/mime.types;
    default_type        application/octet-stream;

    server {
        client_max_body_size 500m;
        listen       8089 default_server;
        listen       [::]:8089 default_server;
        server_name  _;
        autoindex   on;


        location / {
            root /root/nginxShare;
            #auth_basic "security download area";
            #auth_basic_user_file "/usr/local/nginx/conf/htpasswd";
            autoindex off;
        }

        location /upload {

              upload_store /root/nginxShare/upload;
              upload_store_access user:rw;
              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";
              upload_pass_form_field "^submit$|^description$";
              upload_pass @mock;
        }

                        location @mock {

                              return 200 "ok";

        }

                        location @filerewriter {

                              proxy_pass http://localhost:9997;

        }

}

}

配置完成后保存退出

启动服务

/usr/local/nginx/sbin/nginx

新建测试文件准备上传测试

echo '123' > /mnt/1.txt

测试

curl -X POST -F file=@/mnt/1.txt "http://192.168.254.131:8089/upload"

可以看到上传成功,但是文件名和上传时的文件名不一致。

可以用python 的bottle框架写一个简单的服务接口的示例来rename。

python安装bottle

pip install bottle

编写py文件

#!/usr/bin/python

from bottle import *
@post("/upload")
def postExample():
    oldsimplename = request.forms.get("file.name")
    newfullname = request.forms.get("file.path")
    dir = os.path.abspath(os.path.join(newfullname,os.path.pardir))
    os.rename(newfullname,  os.path.join(dir, oldsimplename)  )

    return "ok"

run(host='localhost', port=9997)

修改nginx配置中的upload_pass @mock; 为 upload_pass @filerewriter; 然后重启服务:

/usr/local/nginx/sbin/nginx -s reload

再次上传测试

运行server.py文件

再次上传测试

curl -X POST -F file=@/mnt/123.txt "http://192.168.254.131:8089/upload"

可以发现文件名正常了

文章原文

PS:新手小白为了记录操作写的。若有不同见解,欢迎大佬点评批评。谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值