openresty+lua+nginx 搭建文件服务器过程
刘志波
1. 下载 openresty
wget https://openresty.org/download/openresty-1.13.6.1.tar.gz
2. 解压 openresty
tar zxvf openresty-1.13.6.1.tar.gz
3. 安装 openresty 依赖
yum -y install readline-devel pcre-devel openssl-devel gcc perl curl
4. 配置 openresty
可以通过 –prefix=/usr/指定特定路径,不指定默认路径是/usr/local/openresty/
./configure --with-luajit
5. 配置完毕编译
gmake
gmake install
6. web 接收文件处理
cd /usr/local/openresty/nginx/conf
mkdir lua
touch savefile.lua
输入下面内容
package.path='/usr/local/share/lua/5.1/?.lua;/usr/local/openresty/lualib/resty/?
.lua;'
package.cpath = '/usr/local/lib/lua/5.1/?.so;'
local upload = require "upload"
local chunk_size = 4096
local form = upload:new(chunk_size)
local file
local filelen=0
form:set_timeout(0) -- 1 sec
local filename
function get_filename(res)
local filename = ngx.re.match(res,'(.+)filename="(.+)"(.*)')
if filename then
return filename[2]
end
end
local osfilepath = "/usr/local/openresty/nginx/html/"
local i=0
while true do
local typ, res, err = form:read()
if not typ then
ngx.say("failed to read: ", err)
return
end
if typ == "header" then
if res[1] ~= "Content-Type" then
filename = get_filename(res[2])
if filename then
i=i+1
filepath = osfilepath
file = io.open(filepath,"w+")
if not file then
.. filename
ngx.say("failed to open file ")
return
end
else
end
end
elseif typ == "body" then
if file then
filelen= filelen + tonumber(string.len(res))
file:write(res)
else
end
elseif typ == "part_end" then
if file then
file:close()
file = nil
ngx.say("file upload success")
end
elseif typ == "eof" then
break
else
end
end
if i==0 then
ngx.say("please upload at least one file!")
return
end
7. 配置 nginx.conf
cd /usr/local/openresty/nginx/conf/
vim nginx.conf
修改为以下
user nobody;
worker_processes 1;
#error_log
#error_log
#error_log
logs/error.log;
logs/error.log
logs/error.log
notice;
info;
#pid
logs/nginx.pid;
events {
worker_connections
1024;
}
http {
include
default_type application/octet-stream;
mime.types;
#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
#tcp_nopush
on;
on;
#keepalive_timeout
keepalive_timeout
0;
120;
#gzip
on;
server {
listen
server_name
80;
localhost;
#charset koi8-r;
#access_log
logs/host.access.log
main;
location /files {
alias /usr/local/share/files/;
}
location /uploadfile{
content_by_lua_file 'conf/lua/savefile.lua';
}
#error_page
404
/404.html;
# redirect server error pages to the static page /50x.html
#
error_page
location = /50x.html {
500 502 503 504 /50x.html;
root
html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#
#}
http://127.0.0.1;
proxy_pass
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#
#
#
html;
127.0.0.1:9000;
index.php;
root
fastcgi_pass
fastcgi_index
fastcgi_param
include
SCRIPT_FILENAME
fastcgi_params;
/scripts$fastcgi_script_name;
#
#
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#
#}
all;
deny
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#
#
#
listen
listen
server_name somename
8000;
somename:8080;
another.alias;
alias
location / {
root
index
html;
index.html index.htm;
}
#
#
#
#
#}
# HTTPS server
#
#server {
#
#
listen
server_name localhost;
443 ssl;
#
#
#
#
#
#
#
#
ssl_certificate
cert.pem;
ssl_certificate_key cert.key;
ssl_session_cache
ssl_session_timeout 5m;
shared:SSL:1m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers
on;
location / {
root
html;
index
index.html index.htm;
#
#
#}
}
8. 启动 openresty
cd /usr/local/openresty/ngins/sbin
./nginx
Nginx 常用命令
./nginx -s quit 停止
./nginx
./nginx -s reload 重新加载配置文件
启动
9. 常见问题
如果 openresty 启动无异常,但不能访问,则关闭防火墙
CentOS 7 默认使用的是 firewall 作为防火墙,这里改为 iptables 防火墙。
firewall:
systemctl start firewalld.service
systemctl stop firewalld.service
systemctl disable firewalld.service #禁止 firewall 开机启动
#启动 firewall
#停止 firewall
参考
[1].https://blog.csdn.net/langeldep/article/details/9628819
[2]. https://blog.csdn.net/dieyong/article/details/46550563
[3]. https://openresty.org/cn/