Created
April 1, 2021 09:02
-
-
Save cloverstd/dd61d245eae9a73bb849317ae6f8284b to your computer and use it in GitHub Desktop.
Nginx cors by lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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