为休闲游戏撑持存档功用,需要一个撑持上传下载的文件办事器,间接利用日常平凡做开发的办事器上的nginx来搭建。有一个费事的工作是,一般我们本身办事器的nginx是默认自带的,若是文件办事器需求只是撑持下载功用,间接修改nginx设置装备摆设即可,但若是要撑持上传功用,就需要加载额外的module,需要下载nginx源码从头编译。
下载编译nginx源码和上传模块 wget https://nginx.org/download/nginx-1.20.1.tar.gz git clone https://github.com/vkholodkov/nginx-upload-module/ tar -xvzf nginx-1.20.1.tar.gz cd nginx-1.20.1 yum -y install pcre-devel openssl openssl-devel ./configure --with-compat --add-dynamic-module=../nginx-upload-module make make installnginx默认安拆目次/usr/local/nginx/sbin,设置装备摆设文件/usr/local/nginx/conf/nginx.conf
下载设置装备摆设 # download # autoindex on; # enable directory listing output # autoindex_exact_size off; # output file sizes rounded to kilobytes, megabytes, and gigabytes # autoindex_localtime on; # output local times in the directory server { listen 7088; root /data/json/; location / { } ... }默认是88端口,修改为自定义端口,autoindex设置是为了翻开目次阅读功用的,调试阶段能够翻开。root指定根目次。修改完设置装备摆设重启nginx即可。
上传设置装备摆设 location /upload { upload_store /data/json/; 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 @filerewriter; }利用postman停止上传文件测试,会发现文件名并非当地文件名,而是0000001x如许的,那是nginx-upload-module模块本身的设想,旨在躲避文件抵触。
文件重定名为领会决那个问题,那里参考了网上的做法,利用python的bottle框架监听request,把上传接口保留的文件转发给bottle,利用脚本修改上传文件的文件名。
修改nginx设置装备摆设,代办署理转发给7089端口:
location @filerewriter { proxy_pass http://localhost:7089; }安拆bottle:
pip install bottlefile_server.py文件如下:
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=7089)重启nginx,运行python脚本即可实现上传文件的重定名。
0