nginx+lua 设置跨域

nginx 配置:

                        header_filter_by_lua_file cros.lua;
                        access_by_lua '
                        if ngx.var.request_method == "OPTIONS" then
                        return 203
                        end
                        ';

 

cros.lua:

if string.find(ngx.var.http_origin,":8008") then          //判断request的origin
   ngx.header["Access-Control-Allow-Credentials"] = "true"
   ngx.header["Access-Control-Allow-Origin"] = ngx.var.http_origin
   ngx.header["Access-Control-Allow-Headers"] = "x-requested-with,content-type"
   if ngx.var.request_method == "OPTIONS" then                                        ngx.header["Access-Control-Max-Age"] = "86400"
      ngx.header["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS, DELETE"
      ngx.header["Content-Length"] = "0"
      ngx.header["Content-Type"] = "text/plain, charset=utf-8"
   end 
end

 

posted @ 2017-03-24 15:57  潘点点  阅读(2294)  评论(0编辑  收藏  举报