nginx+lua 設置跨域

nginx 配置:nginx

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

 

cros.lua: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
相關文章
相關標籤/搜索