Hello,
大家好!
我在nginx下做一个file upload功能,使用的是nginx_dav_module模块通过PUT的方式上传文件。遇到500的返回错误。
通过PUT方法上传至这样的URL:
http://serverip:port/upload/file/name/to/upload?user_id=xxx&passwd=xxx
URL中传入了user_id与passwd,用于依次完成:
1. 通过user_id与passwd完成首先去数据库里验证用记的合法性,不合法则立即返回;
2. 通过ngx.loation.capture("/file_store" .. ngx.var.arg), 将用户文件重定位至另外的location,此location开启了HTTP PUT支持(使用nginx_dav_module);
3. 如果2上传成功,则在数据库里记录下本次上传记录。
大致配置如下:
http {
server {
....
location /upload {
lua_need_request_body on; # force receive body
access_by_lua_file validate_user.lua;
content_by_lua '
file = ngx.var.uri中去除/upload后剩下file/name/to/upload;
res = ngx.location.capture(
"/file_store" .. file .. ngx.var.args,
{ method = ngx.HTTP_PUT, always_forwad_body = true }
)
if res.status == ngx.HTTP_OK then
-- ** insert a new file-uploading record to database
end
';
}
}
}
这样的配置会在第2步返回500出错。
我有debug看到capture的subrequest中得到的header是这样子的:
PUT /upload/a.txt?user_id=xx&passwd=xxx (*******1******)
我的理解,capture之后的subrequest中的request line应该是样才对:
PUT /file_store/a.txt?user_id=xx&passwd=xxx (*******2******)
500的出错是不是因为这个原因导致的?
另外在ngx_lua_module的文档中:
“Note that subrequests issued by ngx.location.capture inherit all the request headers of the current request by default and that this may have unexpected side effects on the subrequest responses. For example, when using the standard ngx_proxy module to serve subrequests, an "Accept-Encoding: gzip" header in the main request may result in gzipped responses that cannot be handled properly in Lua code. Original request headers should be ignored by setting proxy_pass_request_headers to off in subrequest locations.”
说subrequest会原封不动的将父request的header继承过去。这样的话,上文中的(*******1******)是合理的。
猜测是ngx.location.capture没有没将header中的request line变成(*******2******)这样才报的500错误。不知道这样的理解对不对!
有没有办法将request line变成这(*******2******)这样。或者大家帮忙看错误可能出在哪里。
先谢谢大家了!