Skip to content

Instantly share code, notes, and snippets.

@cloverstd
Created April 1, 2021 09:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloverstd/dd61d245eae9a73bb849317ae6f8284b to your computer and use it in GitHub Desktop.
Save cloverstd/dd61d245eae9a73bb849317ae6f8284b to your computer and use it in GitHub Desktop.
Nginx cors by lua
local origin_pattern = [=[(\.hui\.lu|\.cloverstd\.com)(:\d+)?$]=]
local headers = ngx.req.get_headers()
ngx.header['Access-Control-Max-Age'] = '3600'
ngx.header['Access-Control-Allow-Credentials'] = 'true'
local origin = headers['Origin']
if origin then
local m, err = ngx.re.match(origin, origin_pattern, "jo")
if m then
ngx.header['Access-Control-Allow-Origin'] = origin
end
end
local request_headers = headers['Access-Control-Request-Headers']
if request_headers then
ngx.header['Access-Control-Allow-Headers'] = request_headers
end
local request_method = headers['Access-Control-Request-Method']
if request_method then
ngx.header['Access-Control-Allow-Methods'] = request_method
end
if ngx.req.get_method() == 'OPTIONS' then
ngx.exit(204);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment