设为首页 收藏本站
查看: 579|回复: 0

[经验分享] nginx配置,配合lua编程实现

[复制链接]

尚未签到

发表于 2017-12-22 20:42:11 | 显示全部楼层 |阅读模式
-- has been sorted in alphabetical order  
local signed_subresources = {
  'acl',
  'append',
  'bucketInfo',
  'cname',
  'commitTransition',
  'comp',
  'cors',
  'delete',
  'lifecycle',
  'location',
  'logging',
  'mime',
  'notification',
  'objectInfo',
  'objectMeta',
  'partData',
  'partInfo',
  'partNumber',
  'policy',
  'position',
  'referer',
  'replication',
  'replicationLocation',
  'replicationProgress',
  'requestPayment',
  'response-cache-control',
  'response-content-disposition',
  'response-content-encoding',
  'response-content-language',
  'response-content-type',
  'response-expires',
  'restore',
  'security-token',
  'tagging',
  'torrent',
  'uploadId',
  'uploads',
  'versionId',
  'versioning',
  'versions',
  'website'
  
}
  

  
function string.startswith(s, start)
  return string.sub(s, 1, string.len(start)) == start
  
end
  

  
local function get_canon_sub_resource()
  local args = ngx.req.get_uri_args()
  -- lower keys
  local keys = {}
  for k, v in pairs(args) do
  keys[k:lower()] = v
  end
  -- make resource string
  local s = ''
  local sep = '?'
  for i, k in ipairs(signed_subresources) do
  v = keys[k]
  if v then
  -- sub table
  v = type(v) == 'table' and v[1] or v
  s = s .. string.format("%s%s=%s", sep, k, v)
  sep = '&'
  end
  end
  return s
  
end
  

  
local function get_canon_resource()
  resource = ''
  object = ngx.unescape_uri(ngx.var.uri)
  sub = get_canon_sub_resource()   
  return string.format("/%s%s%s", ngx.var.oss_bucket, object, sub)
  
end   
  

  
local function get_canon_headers()
  -- default: <lowerkey, value>
  local headers = ngx.req.get_headers()
  local keys = {}
  for k, v in pairs(headers) do
  if string.startswith(k, 'x-oss-') then
  -- client must assemble the same header keys
  if type(v) ~= 'string' then return nil end
  table.insert(keys, k)
  end
  end
  -- sorted in alphabetical order
  table.sort(keys)
  for i, key in ipairs(keys) do
  keys = key .. ':' .. headers[key] .. '\n'
  end
  return table.concat(keys)
  
end
  

  
local function calc_sign(key, method, md5, type_, date, oss_headers, resource)
  local sign_str = string.format('%s\n%s\n%s\n%s\n%s%s',
  method, md5, type_,
  date, oss_headers, resource)
  ngx.log(ngx.ERR, "SignStr:", sign_str, "\n")
  ngx.log(ngx.ERR, "key:", key, "\n")
  ngx.log(ngx.ERR, "sign_str:", sign_str, "\n")
  local sign_result = ngx.encode_base64(ngx.hmac_sha1(key, sign_str))
  return sign_result, sign_str
  
end   
  

  
local function oss_auth()
  local method = ngx.req.get_method()
  local content_md5 = ngx.var.http_content_md5 or ''
  local content_type = ngx.var.http_content_type or ''
  local date = ngx.var.http_x_oss_date or ngx.var.http_date or ''
  if date == '' then
  date = ngx.http_time(ngx.time())
  ngx.req.set_header('Date', date)
  end
  local resource = get_canon_resource()
  local canon_headers = get_canon_headers()
  local sign_result, sign_str = calc_sign(ngx.var.oss_auth_key, method, content_md5,content_type, date, canon_headers, resource)
  local auth = string.format("OSS %s:%s", ngx.var.oss_auth_id, sign_result)
  local isfile = ngx.var.isfile;
  if isfile=="1" then
  --刘小草添加开始
  --验证authon,防止乱下载开始--
  --通过时间戳,保证失效时间开始
  local times=tonumber(ngx.var['arg_times']);
  local cur_timestamp = os.time()
  local one_minute_timestamp = 60
  local cur_valid_timestamp =times+one_minute_timestamp*5
  local flag="2"
  local tochenkey="liuxiaocao"
  local tocken=ngx.var['arg_tocken']
  local fileuserid=ngx.var['fileuserid']
  local filetime=ngx.var['filetime']
  local newTocken= ngx.md5(filetime..fileuserid..times..tochenkey);
  if (cur_timestamp>cur_valid_timestamp) then --已经过期
  flag="0"
  end
  --通过时间戳,保证失效时间结束
  --验证tochen有效性开始
  if flag=="2" then     
  if (tocken~=newTocken) then
  flag="1"      
  end
  end  
  --验证tochen有效性结束
  --验证authon,防止乱下载结束--
  if flag=="2" then        
  local usragent=ngx.var.http_user_agent
  local filename1 =nil;        
  filename1 = ngx.req.get_uri_args()["n"];
  if (string.match(usragent, "Trident")=="Trident") then
  filename1 = ngx.escape_uri(filename1);--IE浏览器
  --filename1 = "222";
  --else           
  -- filename1="333";
  --filename1=ngx.escape_uri(filename1)
  end
  filename1=string.gsub(filename1," ","_");--将空格替换为下划线,防止firefox传递下载附件出错
  attachment = "attachment;filename="..filename1
  ngx.header["Content-Disposition"] = attachment
  --刘小草添加结束
  ngx.req.set_header('Authorization', auth)         
  lanmu=ngx.var['lanmu']
  if (lanmu=="3") then--论坛
  ngx.exec("@oss1")
  else
  ngx.exec("@oss")
  end
  elseif  flag=="1" then
  ngx.say("".."filetime:"..filetime..";fileuserid:"..fileuserid..";times:"..times.."zuhe:"..(filetime..fileuserid..times..tochenkey).."newTocken:"..newTocken);
  else
  ngx.say("您的访问地址有误!"..flag..';cur_timestamp:'..cur_timestamp..";times:"..times);
  end   
  elseif isfile=="2" then
  --ngx.say(".imagesshow.")
  local filename =nil;        
  filename = ngx.var.uri;        
  filename=string.gsub(filename,"//","/");      
  ngx.req.set_uri(filename,false);
  return ngx.exec("@oss2")
  

  else
  --ngx.say(".images.")
  ngx.req.set_header('Authorization', auth)  
  ngx.exec("@oss1")
  end
  
end  
  

  
-- main
  
res = oss_auth()
  

  
if res then
  ngx.exit(res)
  
end
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-426960-1-1.html 上篇帖子: Nginx和PHP-FPM的启动、重启、停止脚本分享 下篇帖子: nginx+php产生大量TIME
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表