forked from HappyValleyIO/kong-http-to-https-redirect
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandler.lua
More file actions
25 lines (19 loc) · 946 Bytes
/
handler.lua
File metadata and controls
25 lines (19 loc) · 946 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
local BasePlugin = require "kong.plugins.base_plugin"
local responses = require "kong.tools.responses"
local HttpFilterHandler = BasePlugin:extend()
-- handle redirect after ip-restriction, bot-detection, cors - but before jwt and other authentication plugins
-- see https://docs.konghq.com/0.14.x/plugin-development/custom-logic/
HttpFilterHandler.PRIORITY = 1500
function HttpFilterHandler:new()
HttpFilterHandler.super.new(self, "kong-http-to-https-redirect")
end
function HttpFilterHandler:access(conf)
HttpFilterHandler.super.access(self)
if ngx.var.https ~= "on" and ngx.var.http_x_forwarded_proto ~= "https" then
local matches_exclude_pattern = conf.exclude_uri_pattern and string.find(ngx.var.request_uri, conf.exclude_uri_pattern)
if not matches_exclude_pattern then
return ngx.redirect("https://" .. ngx.var.host .. ngx.var.request_uri, ngx.HTTP_MOVED_PERMANENTLY)
end
end
end
return HttpFilterHandler