-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: redirect http to https but port not change #7065
Changes from 3 commits
9617d41
096129b
06aa401
9b159e1
c8d2f55
155b5b3
6b38bfe
b9449f9
1ab81b7
9c151e5
d965ff8
215e66b
afe5124
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,8 @@ local ipairs = ipairs | |
local ngx = ngx | ||
local str_find = core.string.find | ||
local str_sub = string.sub | ||
local tonumber = tonumber | ||
local type = type | ||
local math_random = math.random | ||
|
||
local lrucache = core.lrucache.new({ | ||
ttl = 300, count = 100 | ||
|
@@ -148,7 +149,30 @@ function _M.rewrite(conf, ctx) | |
core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf)) | ||
|
||
local ret_code = conf.ret_code | ||
local ret_port = tonumber(ctx.var["var_x_forwarded_port"]) | ||
local local_conf = core.config.local_conf() | ||
local ret_port = core.table.try_read_attr(local_conf, | ||
"plugin_attr", | ||
"redirect_https_port") | ||
|
||
if not ret_port then | ||
local ssl = core.table.try_read_attr(local_conf, | ||
"apisix", | ||
"ssl") | ||
jwrookie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ssl and ssl["enable"] then | ||
ret_port = ssl["listen_port"] | ||
if not ret_port then | ||
local ret_ports = ssl["listen"] | ||
if ret_ports and #ret_ports > 0 then | ||
local idx = math_random(1, #ret_ports) | ||
ret_port = ret_ports[idx] | ||
if type(ret_port) == "table" then | ||
ret_port = ret_port.port | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I feel that this code is too inelegant. Suggest: local function get_port(attr)
local port
if attr then
port = attr.https.port
end
if port then
return port
end
local local_conf = core.config.local_conf()
local ssl = core.table.try_read_attr(local_conf, "apisix", "ssl")
if not ssl or ssl["enable"] then
return port
end
port = ssl["listen_port"]
if port then
return port
end
local ports = ssl["listen"]
if ports and #ports > 0 then
local idx = math_random(1, #ports)
port = ports[idx]
if type(port) == "table" then
port = port.port
end
end
return port
end
local ret_port = get_port(attr) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
local uri = conf.uri | ||
local regex_uri = conf.regex_uri | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -470,3 +470,4 @@ plugin_attr: | |
connect: 60s | ||
read: 60s | ||
send: 60s | ||
redirect_https_port: 8443 # the default port for use by HTTP redirects to HTTPS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use a structure like this:
and comment it out as nobody wants to be redirected to 8443 by default. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a wrapper to get the attribute of a plugin:
apisix/apisix/plugins/prometheus.lua
Line 58 in 6ebe027
We need to use it instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done