From 20d3037d3e5d903005649afeba5d390b342b2b16 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Tue, 22 Mar 2022 20:21:11 +0800 Subject: [PATCH 1/9] feat(plugin-redirect): set redirect server port when enable http_to_https Signed-off-by: kwanhur --- apisix/plugins/redirect.lua | 9 ++- docs/en/latest/plugins/redirect.md | 4 +- docs/zh/latest/plugins/redirect.md | 4 +- t/plugin/redirect.t | 91 ++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/redirect.lua b/apisix/plugins/redirect.lua index fefe12549788..ed9eea477b21 100644 --- a/apisix/plugins/redirect.lua +++ b/apisix/plugins/redirect.lua @@ -38,6 +38,7 @@ local schema = { type = "object", properties = { ret_code = {type = "integer", minimum = 200, default = 302}, + ret_port = {type = "integer", minimum = 1, maxmum = 65535, default = 443}, uri = {type = "string", minLength = 2, pattern = reg}, regex_uri = { description = "params for generating new uri that substitute from client uri, " .. @@ -147,6 +148,7 @@ 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 = conf.ret_port local uri = conf.uri local regex_uri = conf.regex_uri @@ -155,7 +157,12 @@ function _M.rewrite(conf, ctx) if conf.http_to_https and _scheme == "http" then -- TODO: add test case -- PR: https://github.com/apache/apisix/pull/1958 - uri = "https://$host$request_uri" + if ret_port == 80 or ret_port == 443 then + uri = "https://$host$request_uri" + else + uri = "https://$host:" .. ret_port .. "$request_uri" + end + local method_name = ngx.req.get_method() if method_name == "GET" or method_name == "HEAD" then ret_code = 301 diff --git a/docs/en/latest/plugins/redirect.md b/docs/en/latest/plugins/redirect.md index 8ce03cc9f419..a6f2b995e0d5 100644 --- a/docs/en/latest/plugins/redirect.md +++ b/docs/en/latest/plugins/redirect.md @@ -33,6 +33,7 @@ URI redirect. | uri | string | optional | | | New URL which can contain Nginx variable, eg: `/test/index.html`, `$uri/index.html`. You can refer to variables in a way similar to `${xxx}` to avoid ambiguity, eg: `${uri}foo/index.html`. If you just need the original `$` character, add `\` in front of it, like this one: `/\$foo/index.html`. If you refer to a variable name that does not exist, this will not produce an error, and it will be used as an empty string. | | regex_uri | array[string] | optional | | | Use regular expression to match URL from client, when the match is successful, the URL template will be redirected to. If the match is not successful, the URL from the client will be forwarded to the upstream. Only one of `uri` and `regex_uri` can be exist. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL template that is redirected to. | | ret_code | integer | optional | 302 | [200, ...] | Response code | +| ret_port | integer | optional | 443 | [1, 65535] | Redirect server port, only work when enable `http_to_https`. | | encode_uri | boolean | optional | false | | When set to `true` the uri in `Location` header will be encoded as per [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) | | append_query_string | boolean | optional | false | | When set to `true`, add the query string from the original request to the location header. If the configured `uri` / `regex_uri` already contains a query string, the query string from request will be appended to that after an `&`. Caution: don't use this if you've already handled the query string, e.g. with nginx variable $request_uri, to avoid duplicates. | @@ -110,7 +111,8 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 "uri": "/hello", "plugins": { "redirect": { - "http_to_https": true + "http_to_https": true, + "ret_port": 9443 } } }' diff --git a/docs/zh/latest/plugins/redirect.md b/docs/zh/latest/plugins/redirect.md index 2140d1114637..795ac5488ee0 100644 --- a/docs/zh/latest/plugins/redirect.md +++ b/docs/zh/latest/plugins/redirect.md @@ -33,6 +33,7 @@ URI 重定向插件。 | uri | string | 可选 | | | 可以包含 Nginx 变量的 URI,例如:`/test/index.html`, `$uri/index.html`。你可以通过类似于 `$ {xxx}` 的方式引用变量,以避免产生歧义,例如:`${uri}foo/index.html`。若你需要保留 `$` 字符,那么使用如下格式:`/\$foo/index.html` | | regex_uri | array[string] | 可选 | | | 转发到上游的新 `uri` 地址, 使用正则表达式匹配来自客户端的 `uri`,当匹配成功后使用模板替换发送重定向到客户端, 未匹配成功时将客户端请求的 `uri` 转发至上游。`uri` 和 `regex_uri` 不可以同时存在。例如:["^/iresty/(.*)/(.*)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的 `uri` 正则表达式,第二个元素代表匹配成功后发送重定向到客户端的 `uri` 模板。 | | ret_code | integer | 可选 | 302 | [200, ...] | 请求响应码 | +| ret_port | integer | 可选 | 443 | [1, 65535] | 重定向服务器端口,仅在开启 `http_to_https` 有效。| | encode_uri | boolean | 可选 | false | | 当设置为 `true` 时,对返回的 `Location` header进行编码,编码格式参考 [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) | | append_query_string | boolean | optional | false | | 当设置为 `true` 时,将请求url的query部分添加到Location里。如果在 `uri` 或 `regex_uri` 中配置了query, 那么请求的query会被追加在这个query后,以 `&` 分隔。 注意:如果已经处理了query,比如使用了nginx变量 `$request_uri`,那么启用此功能会造成query重复 | @@ -111,7 +112,8 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 "uri": "/hello", "plugins": { "redirect": { - "http_to_https": true + "http_to_https": true, + "ret_port": 9443 } } }' diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index 2f2b9ab869d3..b5a4198ec738 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -1045,3 +1045,94 @@ X-Forwarded-Proto: http --- error_code: 301 --- response_headers Location: https://foo.com/hello + + + +=== TEST 43: enable http_to_https with ret_port +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "host": "foo.com", + "plugins": { + "redirect": { + "http_to_https": true, + "ret_port": 9443 + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 44: redirect +--- request +GET /hello +--- more_headers +Host: foo.com +--- error_code: 301 +--- response_headers +Location: https://foo.com:9443/hello + + + +=== TEST 45: enable http_to_https with ret_port(not take effect) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "host": "foo.com", + "plugins": { + "redirect": { + "http_to_https": true, + "ret_port": 443 + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 46: redirect +--- request +GET /hello +--- more_headers +Host: foo.com +--- error_code: 301 +--- response_headers +Location: https://foo.com/hello + From b18a5a6db706d0e6114a3c9a6afb0021fd15d25e Mon Sep 17 00:00:00 2001 From: kwanhur Date: Tue, 22 Mar 2022 20:21:11 +0800 Subject: [PATCH 2/9] feat(plugin-redirect): set redirect server port when enable http_to_https Signed-off-by: kwanhur --- apisix/plugins/redirect.lua | 9 ++- docs/en/latest/plugins/redirect.md | 4 +- docs/zh/latest/plugins/redirect.md | 4 +- t/plugin/redirect.t | 91 ++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) diff --git a/apisix/plugins/redirect.lua b/apisix/plugins/redirect.lua index fefe12549788..47af7a88ec99 100644 --- a/apisix/plugins/redirect.lua +++ b/apisix/plugins/redirect.lua @@ -38,6 +38,7 @@ local schema = { type = "object", properties = { ret_code = {type = "integer", minimum = 200, default = 302}, + ret_port = {type = "integer", minimum = 1, maxmum = 65535, default = 443}, uri = {type = "string", minLength = 2, pattern = reg}, regex_uri = { description = "params for generating new uri that substitute from client uri, " .. @@ -147,6 +148,7 @@ 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 = conf.ret_port local uri = conf.uri local regex_uri = conf.regex_uri @@ -155,7 +157,12 @@ function _M.rewrite(conf, ctx) if conf.http_to_https and _scheme == "http" then -- TODO: add test case -- PR: https://github.com/apache/apisix/pull/1958 - uri = "https://$host$request_uri" + if ret_port == 443 then + uri = "https://$host$request_uri" + else + uri = "https://$host:" .. ret_port .. "$request_uri" + end + local method_name = ngx.req.get_method() if method_name == "GET" or method_name == "HEAD" then ret_code = 301 diff --git a/docs/en/latest/plugins/redirect.md b/docs/en/latest/plugins/redirect.md index 8ce03cc9f419..a6f2b995e0d5 100644 --- a/docs/en/latest/plugins/redirect.md +++ b/docs/en/latest/plugins/redirect.md @@ -33,6 +33,7 @@ URI redirect. | uri | string | optional | | | New URL which can contain Nginx variable, eg: `/test/index.html`, `$uri/index.html`. You can refer to variables in a way similar to `${xxx}` to avoid ambiguity, eg: `${uri}foo/index.html`. If you just need the original `$` character, add `\` in front of it, like this one: `/\$foo/index.html`. If you refer to a variable name that does not exist, this will not produce an error, and it will be used as an empty string. | | regex_uri | array[string] | optional | | | Use regular expression to match URL from client, when the match is successful, the URL template will be redirected to. If the match is not successful, the URL from the client will be forwarded to the upstream. Only one of `uri` and `regex_uri` can be exist. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL template that is redirected to. | | ret_code | integer | optional | 302 | [200, ...] | Response code | +| ret_port | integer | optional | 443 | [1, 65535] | Redirect server port, only work when enable `http_to_https`. | | encode_uri | boolean | optional | false | | When set to `true` the uri in `Location` header will be encoded as per [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) | | append_query_string | boolean | optional | false | | When set to `true`, add the query string from the original request to the location header. If the configured `uri` / `regex_uri` already contains a query string, the query string from request will be appended to that after an `&`. Caution: don't use this if you've already handled the query string, e.g. with nginx variable $request_uri, to avoid duplicates. | @@ -110,7 +111,8 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 "uri": "/hello", "plugins": { "redirect": { - "http_to_https": true + "http_to_https": true, + "ret_port": 9443 } } }' diff --git a/docs/zh/latest/plugins/redirect.md b/docs/zh/latest/plugins/redirect.md index 2140d1114637..795ac5488ee0 100644 --- a/docs/zh/latest/plugins/redirect.md +++ b/docs/zh/latest/plugins/redirect.md @@ -33,6 +33,7 @@ URI 重定向插件。 | uri | string | 可选 | | | 可以包含 Nginx 变量的 URI,例如:`/test/index.html`, `$uri/index.html`。你可以通过类似于 `$ {xxx}` 的方式引用变量,以避免产生歧义,例如:`${uri}foo/index.html`。若你需要保留 `$` 字符,那么使用如下格式:`/\$foo/index.html` | | regex_uri | array[string] | 可选 | | | 转发到上游的新 `uri` 地址, 使用正则表达式匹配来自客户端的 `uri`,当匹配成功后使用模板替换发送重定向到客户端, 未匹配成功时将客户端请求的 `uri` 转发至上游。`uri` 和 `regex_uri` 不可以同时存在。例如:["^/iresty/(.*)/(.*)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的 `uri` 正则表达式,第二个元素代表匹配成功后发送重定向到客户端的 `uri` 模板。 | | ret_code | integer | 可选 | 302 | [200, ...] | 请求响应码 | +| ret_port | integer | 可选 | 443 | [1, 65535] | 重定向服务器端口,仅在开启 `http_to_https` 有效。| | encode_uri | boolean | 可选 | false | | 当设置为 `true` 时,对返回的 `Location` header进行编码,编码格式参考 [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) | | append_query_string | boolean | optional | false | | 当设置为 `true` 时,将请求url的query部分添加到Location里。如果在 `uri` 或 `regex_uri` 中配置了query, 那么请求的query会被追加在这个query后,以 `&` 分隔。 注意:如果已经处理了query,比如使用了nginx变量 `$request_uri`,那么启用此功能会造成query重复 | @@ -111,7 +112,8 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 "uri": "/hello", "plugins": { "redirect": { - "http_to_https": true + "http_to_https": true, + "ret_port": 9443 } } }' diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index 2f2b9ab869d3..b5a4198ec738 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -1045,3 +1045,94 @@ X-Forwarded-Proto: http --- error_code: 301 --- response_headers Location: https://foo.com/hello + + + +=== TEST 43: enable http_to_https with ret_port +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "host": "foo.com", + "plugins": { + "redirect": { + "http_to_https": true, + "ret_port": 9443 + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 44: redirect +--- request +GET /hello +--- more_headers +Host: foo.com +--- error_code: 301 +--- response_headers +Location: https://foo.com:9443/hello + + + +=== TEST 45: enable http_to_https with ret_port(not take effect) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "uri": "/hello", + "host": "foo.com", + "plugins": { + "redirect": { + "http_to_https": true, + "ret_port": 443 + } + } + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 46: redirect +--- request +GET /hello +--- more_headers +Host: foo.com +--- error_code: 301 +--- response_headers +Location: https://foo.com/hello + From a886510e5d6edd294fce3e90858e8e5ec7eff604 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Wed, 23 Mar 2022 09:35:33 +0800 Subject: [PATCH 3/9] style: reindex t/plugin/redirect.t Signed-off-by: kwanhur --- t/plugin/redirect.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index b5a4198ec738..fbd4b603b865 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -1135,4 +1135,3 @@ Host: foo.com --- error_code: 301 --- response_headers Location: https://foo.com/hello - From fc97e859aa0be43a70c76db434d35941d79030a7 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Wed, 23 Mar 2022 09:42:29 +0800 Subject: [PATCH 4/9] style: reindex t/plugin/redirect.t Signed-off-by: kwanhur --- t/plugin/redirect.t | 1 - 1 file changed, 1 deletion(-) diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index b5a4198ec738..fbd4b603b865 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -1135,4 +1135,3 @@ Host: foo.com --- error_code: 301 --- response_headers Location: https://foo.com/hello - From 4726d7912d19ab46f72fadaeca709a71e735e286 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sat, 30 Apr 2022 14:32:35 +0800 Subject: [PATCH 5/9] feat(redirect): redirect server port with x-forwarded-port when enable http_to_https Signed-off-by: kwanhur --- apisix/core/ctx.lua | 1 + apisix/plugins/redirect.lua | 6 +- docs/en/latest/plugins/redirect.md | 24 ++--- docs/zh/latest/plugins/redirect.md | 22 ++-- t/plugin/redirect.t | 162 ++++++++--------------------- 5 files changed, 68 insertions(+), 147 deletions(-) diff --git a/apisix/core/ctx.lua b/apisix/core/ctx.lua index 83b9f9a30170..df6e430ce396 100644 --- a/apisix/core/ctx.lua +++ b/apisix/core/ctx.lua @@ -199,6 +199,7 @@ do upstream_cache_bypass = true, var_x_forwarded_proto = true, + var_x_forwarded_port = true, } -- sort in alphabetical diff --git a/apisix/plugins/redirect.lua b/apisix/plugins/redirect.lua index 47af7a88ec99..e93ec4e5b1f6 100644 --- a/apisix/plugins/redirect.lua +++ b/apisix/plugins/redirect.lua @@ -24,6 +24,7 @@ local ipairs = ipairs local ngx = ngx local str_find = core.string.find local str_sub = string.sub +local tonumber = tonumber local lrucache = core.lrucache.new({ ttl = 300, count = 100 @@ -38,7 +39,6 @@ local schema = { type = "object", properties = { ret_code = {type = "integer", minimum = 200, default = 302}, - ret_port = {type = "integer", minimum = 1, maxmum = 65535, default = 443}, uri = {type = "string", minLength = 2, pattern = reg}, regex_uri = { description = "params for generating new uri that substitute from client uri, " .. @@ -148,7 +148,7 @@ 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 = conf.ret_port + local ret_port = tonumber(ctx.var["var_x_forwarded_port"]) local uri = conf.uri local regex_uri = conf.regex_uri @@ -157,7 +157,7 @@ function _M.rewrite(conf, ctx) if conf.http_to_https and _scheme == "http" then -- TODO: add test case -- PR: https://github.com/apache/apisix/pull/1958 - if ret_port == 443 then + if ret_port == 443 or ret_port == 0 then uri = "https://$host$request_uri" else uri = "https://$host:" .. ret_port .. "$request_uri" diff --git a/docs/en/latest/plugins/redirect.md b/docs/en/latest/plugins/redirect.md index eaff1991252b..2c95d5a32600 100644 --- a/docs/en/latest/plugins/redirect.md +++ b/docs/en/latest/plugins/redirect.md @@ -32,20 +32,21 @@ The `redirect` Plugin can be used to configure redirects. ## Attributes -| Name | Type | Requirement | Default | Valid | Description | -| ------------- | ------- | ----------- | ------- | ----- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| http_to_https | boolean | optional | false | | When it is set to `true` and the request is HTTP, will be automatically redirected to HTTPS with 301 response code, and the URI will keep the same as client request. | -| uri | string | optional | | | New URL which can contain Nginx variable, eg: `/test/index.html`, `$uri/index.html`. You can refer to variables in a way similar to `${xxx}` to avoid ambiguity, eg: `${uri}foo/index.html`. If you just need the original `$` character, add `\` in front of it, like this one: `/\$foo/index.html`. If you refer to a variable name that does not exist, this will not produce an error, and it will be used as an empty string. | -| regex_uri | array[string] | optional | | | Use regular expression to match URL from client, when the match is successful, the URL template will be redirected to. If the match is not successful, the URL from the client will be forwarded to the upstream. Only one of `uri` and `regex_uri` can be exist. For example: [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], the first element represents the matching regular expression and the second element represents the URL template that is redirected to. | -| ret_code | integer | optional | 302 | [200, ...] | Response code | -| ret_port | integer | optional | 443 | [1, 65535] | Redirect server port, only work when enable `http_to_https`. | -| encode_uri | boolean | optional | false | | When set to `true` the uri in `Location` header will be encoded as per [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) | -| append_query_string | boolean | optional | false | | When set to `true`, add the query string from the original request to the location header. If the configured `uri` / `regex_uri` already contains a query string, the query string from request will be appended to that after an `&`. Caution: don't use this if you've already handled the query string, e.g. with nginx variable $request_uri, to avoid duplicates. | +| Name | Type | Required | Default | Valid values | Description | +|---------------------|---------------|----------|---------|--------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| http_to_https | boolean | False | false | | When set to `true` and the request is HTTP, it will be redirected to HTTPS with the same URI with a 301 status code. | +| uri | string | False | | | URI to redirect to. Can contain Nginx variables. For example, `/test/index.html`, `$uri/index.html`, `${uri}/index.html`. If you refer to a variable name that doesn't exist, instead of throwing an error, it will treat it as an empty variable. | +| regex_uri | array[string] | False | | | Match the URL from client with a regular expression and redirect. If it doesn't match, the request will be forwarded to the Upstream. Only either of `uri` or `regex_uri` can be used at a time. For example, [" ^/iresty/(.*)/(.*)/(.*)", "/$1-$2-$3"], where the first element is the regular expression to match and the second element is the URI to redirect to. | +| ret_code | integer | False | 302 | [200, ...] | HTTP response code. | +| encode_uri | boolean | False | false | | When set to `true` the URI in the `Location` header will be encoded as per [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986). | +| append_query_string | boolean | False | false | | When set to `true`, adds the query string from the original request to the `Location` header. If the configured `uri` or `regex_uri` already contains a query string, the query string from the request will be appended to it with an `&`. Do not use this if you have already handled the query string (for example, with an Nginx variable `$request_uri`) to avoid duplicates. | :::note Only one of `http_to_https`, `uri` and `regex_uri` can be configured. +* When enable `http_to_https`, you can specify redirect server port with the header `X-Forwarded-Port`. + ::: ## Enabling the Plugin @@ -107,7 +108,6 @@ Content-Type: text/html Content-Length: 166 Connection: keep-alive Location: /test/default.html - ... ``` @@ -121,8 +121,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f1 "uri": "/hello", "plugins": { "redirect": { - "http_to_https": true, - "ret_port": 9443 + "http_to_https": true } } }' @@ -138,7 +137,6 @@ curl http://127.0.0.1:9080/hello -i HTTP/1.1 301 Moved Permanently ... Location: https://127.0.0.1:9443/hello - ... ``` diff --git a/docs/zh/latest/plugins/redirect.md b/docs/zh/latest/plugins/redirect.md index 238f1c4840b1..fcae7b06d97f 100644 --- a/docs/zh/latest/plugins/redirect.md +++ b/docs/zh/latest/plugins/redirect.md @@ -32,20 +32,21 @@ description: 本文介绍了关于 Apache APISIX `redirect` 插件的基本信 ## 属性 -| Name | Type | Requirement | Default | Valid | Description | -| ------------- | ------- | ----------- | ------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| http_to_https | boolean | 可选 | false | | 当设置为 `true` 并且请求是 http 时,会自动 301 重定向为 https,uri 保持不变 | -| uri | string | 可选 | | | 可以包含 Nginx 变量的 URI,例如:`/test/index.html`, `$uri/index.html`。你可以通过类似于 `$ {xxx}` 的方式引用变量,以避免产生歧义,例如:`${uri}foo/index.html`。若你需要保留 `$` 字符,那么使用如下格式:`/\$foo/index.html` | -| regex_uri | array[string] | 可选 | | | 转发到上游的新 `uri` 地址, 使用正则表达式匹配来自客户端的 `uri`,当匹配成功后使用模板替换发送重定向到客户端, 未匹配成功时将客户端请求的 `uri` 转发至上游。`uri` 和 `regex_uri` 不可以同时存在。例如:["^/iresty/(.*)/(.*)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的 `uri` 正则表达式,第二个元素代表匹配成功后发送重定向到客户端的 `uri` 模板。 | -| ret_code | integer | 可选 | 302 | [200, ...] | 请求响应码 | -| ret_port | integer | 可选 | 443 | [1, 65535] | 重定向服务器端口,仅在开启 `http_to_https` 有效。| -| encode_uri | boolean | 可选 | false | | 当设置为 `true` 时,对返回的 `Location` header进行编码,编码格式参考 [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986) | -| append_query_string | boolean | optional | false | | 当设置为 `true` 时,将请求url的query部分添加到Location里。如果在 `uri` 或 `regex_uri` 中配置了query, 那么请求的query会被追加在这个query后,以 `&` 分隔。 注意:如果已经处理了query,比如使用了nginx变量 `$request_uri`,那么启用此功能会造成query重复 | +| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 | +|---------------------|---------------|-----|-------|--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| http_to_https | boolean | 是 | false | [true,false] | 当设置为 `true` 并且请求是 HTTP 时,它将被重定向具有相同 URI 和 301 状态码的 HTTPS。 | +| uri | string | 是 | | | 要重定向到的 URI,可以包含 NGINX 变量。例如:`/test/index.htm`, `$uri/index.html`,`${uri}/index.html`。如果你引入了一个不存在的变量,它不会报错,而是将其视为一个空变量。 | +| regex_uri | array[string] | 是 | | | 将来自客户端的 URL 与正则表达式匹配并重定向。当匹配成功后使用模板替换发送重定向到客户端,如果未匹配成功会将客户端请求的 URI 转发至上游。 和 `regex_uri` 不可以同时存在。例如:["^/iresty/(.)/(.)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的 URI 正则表达式,第二个元素代表匹配成功后发送重定向到客户端的 URI 模板。 | +| ret_code | integer | 是 | 302 | [200, ...] | HTTP 响应码 | +| encode_uri | boolean | 是 | false | [true,false] | 当设置为 `true` 时,对返回的 `Location` Header 按照 [RFC3986](https://datatracker.ietf.org/doc/html/rfc3986)的编码格式进行编码。 | +| append_query_string | boolean | 是 | false | [true,false] | 当设置为 `true` 时,将原始请求中的查询字符串添加到 `Location` Header。如果已配置 `uri` 或 `regex_uri` 已经包含查询字符串,则请求中的查询字符串将附加一个`&`。如果你已经处理过查询字符串(例如,使用 NGINX 变量 `$request_uri`),请不要再使用该参数以避免重复。 | :::note `http_to_https`、`uri` 和 `regex_uri` 只能配置其中一个属性。 +* 当开启 `http_to_https`,你可以设置请求头 `X-Forwarded-Port` 指定重定向的服务器端口。 + ::: ## 启用插件 @@ -123,8 +124,7 @@ curl http://127.0.0.1:9080/apisix/admin/routes/1 \ "uri": "/hello", "plugins": { "redirect": { - "http_to_https": true, - "ret_port": 9443 + "http_to_https": true } } }' diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index fbd4b603b865..68be6a7c7b01 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -435,11 +435,23 @@ GET /hello Host: foo.com --- error_code: 301 --- response_headers +Location: https://foo.com:1984/hello + + + +=== TEST 19: redirect(pass x-forwarded-port) +--- request +GET /hello +--- more_headers +Host: foo.com +x-forwarded-port: 443 +--- error_code: 301 +--- response_headers Location: https://foo.com/hello -=== TEST 19: enable http_to_https with ret_code(not take effect) +=== TEST 20: enable http_to_https with ret_code(not take effect) --- config location /t { content_by_lua_block { @@ -473,18 +485,18 @@ passed -=== TEST 20: redirect +=== TEST 21: redirect --- request GET /hello --- more_headers Host: foo.com --- error_code: 301 --- response_headers -Location: https://foo.com/hello +Location: https://foo.com:1984/hello -=== TEST 21: wrong configure, enable http_to_https with uri +=== TEST 22: wrong configure, enable http_to_https with uri --- config location /t { content_by_lua_block { @@ -519,7 +531,7 @@ qr/error_msg":"failed to check the configuration of plugin redirect err: value s -=== TEST 22: enable http_to_https with upstream +=== TEST 23: enable http_to_https with upstream --- config location /t { content_by_lua_block { @@ -558,18 +570,18 @@ passed -=== TEST 23: redirect +=== TEST 24: redirect --- request GET /hello --- more_headers Host: test.com --- error_code: 301 --- response_headers -Location: https://test.com/hello +Location: https://test.com:1984/hello -=== TEST 24: set ssl(sni: test.com) +=== TEST 25: set ssl(sni: test.com) --- config location /t { content_by_lua_block { @@ -600,7 +612,7 @@ passed -=== TEST 25: client https request +=== TEST 26: client https request --- config listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl; @@ -674,7 +686,7 @@ close: 1 nil} -=== TEST 26: add plugin with new uri: /test/add +=== TEST 27: add plugin with new uri: /test/add --- config location /t { content_by_lua_block { @@ -709,46 +721,46 @@ passed -=== TEST 27: http to https post redirect +=== TEST 28: http to https post redirect --- request POST /hello-https --- more_headers Host: test.com --- response_headers -Location: https://test.com/hello-https +Location: https://test.com:1984/hello-https --- error_code: 308 --- no_error_log [error] -=== TEST 28: http to https get redirect +=== TEST 29: http to https get redirect --- request GET /hello-https --- more_headers Host: test.com --- response_headers -Location: https://test.com/hello-https +Location: https://test.com:1984/hello-https --- error_code: 301 --- no_error_log [error] -=== TEST 29: http to https head redirect +=== TEST 30: http to https head redirect --- request HEAD /hello-https --- more_headers Host: test.com --- response_headers -Location: https://test.com/hello-https +Location: https://test.com:1984/hello-https --- error_code: 301 --- no_error_log [error] -=== TEST 30: add plugin with new regex_uri: /test/1 redirect to http://test.com/1 +=== TEST 31: add plugin with new regex_uri: /test/1 redirect to http://test.com/1 --- config location /t { content_by_lua_block { @@ -787,7 +799,7 @@ passed -=== TEST 31: regex_uri redirect +=== TEST 32: regex_uri redirect --- request GET /test/1 --- response_headers @@ -798,7 +810,7 @@ Location: http://test.com/1 -=== TEST 32: regex_uri not match, get response from upstream +=== TEST 33: regex_uri not match, get response from upstream --- request GET /hello --- error_code: 200 @@ -809,7 +821,7 @@ hello world -=== TEST 33: add plugin with new regex_uri: encode_uri = true +=== TEST 34: add plugin with new regex_uri: encode_uri = true --- config location /t { content_by_lua_block { @@ -849,7 +861,7 @@ passed -=== TEST 34: regex_uri redirect with special characters +=== TEST 35: regex_uri redirect with special characters --- request GET /test/with%20space --- error_code: 200 @@ -861,7 +873,7 @@ Location: http://test.com/with%20space -=== TEST 35: add plugin with new uri: encode_uri = true +=== TEST 36: add plugin with new uri: encode_uri = true --- config location /t { content_by_lua_block { @@ -895,7 +907,7 @@ passed -=== TEST 36: redirect with special characters +=== TEST 37: redirect with special characters --- request GET /hello/with%20space --- response_headers @@ -906,7 +918,7 @@ Location: /hello/with%20space -=== TEST 37: add plugin with new uri: $uri (append_query_string = true) +=== TEST 38: add plugin with new uri: $uri (append_query_string = true) --- config location /t { content_by_lua_block { @@ -940,7 +952,7 @@ passed -=== TEST 38: redirect +=== TEST 39: redirect --- request GET /hello?name=json --- response_headers @@ -951,7 +963,7 @@ Location: /hello?name=json -=== TEST 39: add plugin with new uri: $uri?type=string (append_query_string = true) +=== TEST 40: add plugin with new uri: $uri?type=string (append_query_string = true) --- config location /t { content_by_lua_block { @@ -985,7 +997,7 @@ passed -=== TEST 40: redirect +=== TEST 41: redirect --- request GET /hello?name=json --- response_headers @@ -996,7 +1008,7 @@ Location: /hello?type=string&name=json -=== TEST 41: enable http_to_https (pass X-Forwarded-Proto) +=== TEST 42: enable http_to_https (pass X-Forwarded-Proto) --- config location /t { content_by_lua_block { @@ -1036,7 +1048,7 @@ passed -=== TEST 42: enable http_to_https (pass X-Forwarded-Proto) +=== TEST 43: enable http_to_https (pass X-Forwarded-Proto) --- request GET /hello --- more_headers @@ -1044,94 +1056,4 @@ Host: foo.com X-Forwarded-Proto: http --- error_code: 301 --- response_headers -Location: https://foo.com/hello - - - -=== TEST 43: enable http_to_https with ret_port ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "uri": "/hello", - "host": "foo.com", - "plugins": { - "redirect": { - "http_to_https": true, - "ret_port": 9443 - } - } - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 44: redirect ---- request -GET /hello ---- more_headers -Host: foo.com ---- error_code: 301 ---- response_headers -Location: https://foo.com:9443/hello - - - -=== TEST 45: enable http_to_https with ret_port(not take effect) ---- config - location /t { - content_by_lua_block { - local t = require("lib.test_admin").test - local code, body = t('/apisix/admin/routes/1', - ngx.HTTP_PUT, - [[{ - "uri": "/hello", - "host": "foo.com", - "plugins": { - "redirect": { - "http_to_https": true, - "ret_port": 443 - } - } - }]] - ) - - if code >= 300 then - ngx.status = code - end - ngx.say(body) - } - } ---- request -GET /t ---- response_body -passed ---- no_error_log -[error] - - - -=== TEST 46: redirect ---- request -GET /hello ---- more_headers -Host: foo.com ---- error_code: 301 ---- response_headers -Location: https://foo.com/hello +Location: https://foo.com:1984/hello From 721aa8f0ea4a6303a237423eb838ee125b2366fc Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sat, 30 Apr 2022 16:19:41 +0800 Subject: [PATCH 6/9] feat(redirect): redirect to well-known port when ret_port nil Signed-off-by: kwanhur --- apisix/plugins/redirect.lua | 2 +- t/plugin/redirect.t | 62 ++++++++++++++++++++++--------------- 2 files changed, 38 insertions(+), 26 deletions(-) diff --git a/apisix/plugins/redirect.lua b/apisix/plugins/redirect.lua index e93ec4e5b1f6..b50140a1d46d 100644 --- a/apisix/plugins/redirect.lua +++ b/apisix/plugins/redirect.lua @@ -157,7 +157,7 @@ function _M.rewrite(conf, ctx) if conf.http_to_https and _scheme == "http" then -- TODO: add test case -- PR: https://github.com/apache/apisix/pull/1958 - if ret_port == 443 or ret_port == 0 then + if ret_port == 443 or ret_port == 0 or ret_port == nil then uri = "https://$host$request_uri" else uri = "https://$host:" .. ret_port .. "$request_uri" diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index 68be6a7c7b01..3d5fb9e8e97f 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -439,7 +439,7 @@ Location: https://foo.com:1984/hello -=== TEST 19: redirect(pass x-forwarded-port) +=== TEST 19: redirect(pass well-known port 443 to x-forwarded-port) --- request GET /hello --- more_headers @@ -451,7 +451,19 @@ Location: https://foo.com/hello -=== TEST 20: enable http_to_https with ret_code(not take effect) +=== TEST 20: redirect(pass invalid non-number to x-forwarded-port) +--- request +GET /hello +--- more_headers +Host: foo.com +x-forwarded-port: ok +--- error_code: 301 +--- response_headers +Location: https://foo.com/hello + + + +=== TEST 21: enable http_to_https with ret_code(not take effect) --- config location /t { content_by_lua_block { @@ -485,7 +497,7 @@ passed -=== TEST 21: redirect +=== TEST 22: redirect --- request GET /hello --- more_headers @@ -496,7 +508,7 @@ Location: https://foo.com:1984/hello -=== TEST 22: wrong configure, enable http_to_https with uri +=== TEST 23: wrong configure, enable http_to_https with uri --- config location /t { content_by_lua_block { @@ -531,7 +543,7 @@ qr/error_msg":"failed to check the configuration of plugin redirect err: value s -=== TEST 23: enable http_to_https with upstream +=== TEST 24: enable http_to_https with upstream --- config location /t { content_by_lua_block { @@ -570,7 +582,7 @@ passed -=== TEST 24: redirect +=== TEST 25: redirect --- request GET /hello --- more_headers @@ -581,7 +593,7 @@ Location: https://test.com:1984/hello -=== TEST 25: set ssl(sni: test.com) +=== TEST 26: set ssl(sni: test.com) --- config location /t { content_by_lua_block { @@ -612,7 +624,7 @@ passed -=== TEST 26: client https request +=== TEST 27: client https request --- config listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl; @@ -686,7 +698,7 @@ close: 1 nil} -=== TEST 27: add plugin with new uri: /test/add +=== TEST 28: add plugin with new uri: /test/add --- config location /t { content_by_lua_block { @@ -721,7 +733,7 @@ passed -=== TEST 28: http to https post redirect +=== TEST 29: http to https post redirect --- request POST /hello-https --- more_headers @@ -734,7 +746,7 @@ Location: https://test.com:1984/hello-https -=== TEST 29: http to https get redirect +=== TEST 30: http to https get redirect --- request GET /hello-https --- more_headers @@ -747,7 +759,7 @@ Location: https://test.com:1984/hello-https -=== TEST 30: http to https head redirect +=== TEST 31: http to https head redirect --- request HEAD /hello-https --- more_headers @@ -760,7 +772,7 @@ Location: https://test.com:1984/hello-https -=== TEST 31: add plugin with new regex_uri: /test/1 redirect to http://test.com/1 +=== TEST 32: add plugin with new regex_uri: /test/1 redirect to http://test.com/1 --- config location /t { content_by_lua_block { @@ -799,7 +811,7 @@ passed -=== TEST 32: regex_uri redirect +=== TEST 33: regex_uri redirect --- request GET /test/1 --- response_headers @@ -810,7 +822,7 @@ Location: http://test.com/1 -=== TEST 33: regex_uri not match, get response from upstream +=== TEST 34: regex_uri not match, get response from upstream --- request GET /hello --- error_code: 200 @@ -821,7 +833,7 @@ hello world -=== TEST 34: add plugin with new regex_uri: encode_uri = true +=== TEST 35: add plugin with new regex_uri: encode_uri = true --- config location /t { content_by_lua_block { @@ -861,7 +873,7 @@ passed -=== TEST 35: regex_uri redirect with special characters +=== TEST 36: regex_uri redirect with special characters --- request GET /test/with%20space --- error_code: 200 @@ -873,7 +885,7 @@ Location: http://test.com/with%20space -=== TEST 36: add plugin with new uri: encode_uri = true +=== TEST 37: add plugin with new uri: encode_uri = true --- config location /t { content_by_lua_block { @@ -907,7 +919,7 @@ passed -=== TEST 37: redirect with special characters +=== TEST 38: redirect with special characters --- request GET /hello/with%20space --- response_headers @@ -918,7 +930,7 @@ Location: /hello/with%20space -=== TEST 38: add plugin with new uri: $uri (append_query_string = true) +=== TEST 39: add plugin with new uri: $uri (append_query_string = true) --- config location /t { content_by_lua_block { @@ -952,7 +964,7 @@ passed -=== TEST 39: redirect +=== TEST 40: redirect --- request GET /hello?name=json --- response_headers @@ -963,7 +975,7 @@ Location: /hello?name=json -=== TEST 40: add plugin with new uri: $uri?type=string (append_query_string = true) +=== TEST 41: add plugin with new uri: $uri?type=string (append_query_string = true) --- config location /t { content_by_lua_block { @@ -997,7 +1009,7 @@ passed -=== TEST 41: redirect +=== TEST 42: redirect --- request GET /hello?name=json --- response_headers @@ -1008,7 +1020,7 @@ Location: /hello?type=string&name=json -=== TEST 42: enable http_to_https (pass X-Forwarded-Proto) +=== TEST 43: enable http_to_https (pass X-Forwarded-Proto) --- config location /t { content_by_lua_block { @@ -1048,7 +1060,7 @@ passed -=== TEST 43: enable http_to_https (pass X-Forwarded-Proto) +=== TEST 44: enable http_to_https (pass X-Forwarded-Proto) --- request GET /hello --- more_headers From 76007ac3d3476eb1b47f627198121a350874fabd Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sat, 30 Apr 2022 16:27:57 +0800 Subject: [PATCH 7/9] feat(redirect): redirect to well-known port when ret_port less than zero or greater than 65535 Signed-off-by: kwanhur --- apisix/plugins/redirect.lua | 2 +- t/plugin/redirect.t | 74 ++++++++++++++++++++++++------------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/apisix/plugins/redirect.lua b/apisix/plugins/redirect.lua index b50140a1d46d..104cf9d45d48 100644 --- a/apisix/plugins/redirect.lua +++ b/apisix/plugins/redirect.lua @@ -157,7 +157,7 @@ function _M.rewrite(conf, ctx) if conf.http_to_https and _scheme == "http" then -- TODO: add test case -- PR: https://github.com/apache/apisix/pull/1958 - if ret_port == 443 or ret_port == 0 or ret_port == nil then + if ret_port == nil or ret_port == 443 or ret_port <= 0 or ret_port > 65535 then uri = "https://$host$request_uri" else uri = "https://$host:" .. ret_port .. "$request_uri" diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t index 3d5fb9e8e97f..47479a2b468e 100644 --- a/t/plugin/redirect.t +++ b/t/plugin/redirect.t @@ -451,7 +451,31 @@ Location: https://foo.com/hello -=== TEST 20: redirect(pass invalid non-number to x-forwarded-port) +=== TEST 20: redirect(pass negative number to x-forwarded-port) +--- request +GET /hello +--- more_headers +Host: foo.com +x-forwarded-port: -443 +--- error_code: 301 +--- response_headers +Location: https://foo.com/hello + + + +=== TEST 21: redirect(pass number more than 65535 to x-forwarded-port) +--- request +GET /hello +--- more_headers +Host: foo.com +x-forwarded-port: 65536 +--- error_code: 301 +--- response_headers +Location: https://foo.com/hello + + + +=== TEST 22: redirect(pass invalid non-number to x-forwarded-port) --- request GET /hello --- more_headers @@ -463,7 +487,7 @@ Location: https://foo.com/hello -=== TEST 21: enable http_to_https with ret_code(not take effect) +=== TEST 23: enable http_to_https with ret_code(not take effect) --- config location /t { content_by_lua_block { @@ -497,7 +521,7 @@ passed -=== TEST 22: redirect +=== TEST 24: redirect --- request GET /hello --- more_headers @@ -508,7 +532,7 @@ Location: https://foo.com:1984/hello -=== TEST 23: wrong configure, enable http_to_https with uri +=== TEST 25: wrong configure, enable http_to_https with uri --- config location /t { content_by_lua_block { @@ -543,7 +567,7 @@ qr/error_msg":"failed to check the configuration of plugin redirect err: value s -=== TEST 24: enable http_to_https with upstream +=== TEST 26: enable http_to_https with upstream --- config location /t { content_by_lua_block { @@ -582,7 +606,7 @@ passed -=== TEST 25: redirect +=== TEST 27: redirect --- request GET /hello --- more_headers @@ -593,7 +617,7 @@ Location: https://test.com:1984/hello -=== TEST 26: set ssl(sni: test.com) +=== TEST 28: set ssl(sni: test.com) --- config location /t { content_by_lua_block { @@ -624,7 +648,7 @@ passed -=== TEST 27: client https request +=== TEST 29: client https request --- config listen unix:$TEST_NGINX_HTML_DIR/nginx.sock ssl; @@ -698,7 +722,7 @@ close: 1 nil} -=== TEST 28: add plugin with new uri: /test/add +=== TEST 30: add plugin with new uri: /test/add --- config location /t { content_by_lua_block { @@ -733,7 +757,7 @@ passed -=== TEST 29: http to https post redirect +=== TEST 31: http to https post redirect --- request POST /hello-https --- more_headers @@ -746,7 +770,7 @@ Location: https://test.com:1984/hello-https -=== TEST 30: http to https get redirect +=== TEST 32: http to https get redirect --- request GET /hello-https --- more_headers @@ -759,7 +783,7 @@ Location: https://test.com:1984/hello-https -=== TEST 31: http to https head redirect +=== TEST 33: http to https head redirect --- request HEAD /hello-https --- more_headers @@ -772,7 +796,7 @@ Location: https://test.com:1984/hello-https -=== TEST 32: add plugin with new regex_uri: /test/1 redirect to http://test.com/1 +=== TEST 34: add plugin with new regex_uri: /test/1 redirect to http://test.com/1 --- config location /t { content_by_lua_block { @@ -811,7 +835,7 @@ passed -=== TEST 33: regex_uri redirect +=== TEST 35: regex_uri redirect --- request GET /test/1 --- response_headers @@ -822,7 +846,7 @@ Location: http://test.com/1 -=== TEST 34: regex_uri not match, get response from upstream +=== TEST 36: regex_uri not match, get response from upstream --- request GET /hello --- error_code: 200 @@ -833,7 +857,7 @@ hello world -=== TEST 35: add plugin with new regex_uri: encode_uri = true +=== TEST 37: add plugin with new regex_uri: encode_uri = true --- config location /t { content_by_lua_block { @@ -873,7 +897,7 @@ passed -=== TEST 36: regex_uri redirect with special characters +=== TEST 38: regex_uri redirect with special characters --- request GET /test/with%20space --- error_code: 200 @@ -885,7 +909,7 @@ Location: http://test.com/with%20space -=== TEST 37: add plugin with new uri: encode_uri = true +=== TEST 39: add plugin with new uri: encode_uri = true --- config location /t { content_by_lua_block { @@ -919,7 +943,7 @@ passed -=== TEST 38: redirect with special characters +=== TEST 40: redirect with special characters --- request GET /hello/with%20space --- response_headers @@ -930,7 +954,7 @@ Location: /hello/with%20space -=== TEST 39: add plugin with new uri: $uri (append_query_string = true) +=== TEST 41: add plugin with new uri: $uri (append_query_string = true) --- config location /t { content_by_lua_block { @@ -964,7 +988,7 @@ passed -=== TEST 40: redirect +=== TEST 42: redirect --- request GET /hello?name=json --- response_headers @@ -975,7 +999,7 @@ Location: /hello?name=json -=== TEST 41: add plugin with new uri: $uri?type=string (append_query_string = true) +=== TEST 43: add plugin with new uri: $uri?type=string (append_query_string = true) --- config location /t { content_by_lua_block { @@ -1009,7 +1033,7 @@ passed -=== TEST 42: redirect +=== TEST 44: redirect --- request GET /hello?name=json --- response_headers @@ -1020,7 +1044,7 @@ Location: /hello?type=string&name=json -=== TEST 43: enable http_to_https (pass X-Forwarded-Proto) +=== TEST 45: enable http_to_https (pass X-Forwarded-Proto) --- config location /t { content_by_lua_block { @@ -1060,7 +1084,7 @@ passed -=== TEST 44: enable http_to_https (pass X-Forwarded-Proto) +=== TEST 46: enable http_to_https (pass X-Forwarded-Proto) --- request GET /hello --- more_headers From 876723337a2779411f5c38aa55e358d1ef3b1d92 Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sun, 1 May 2022 21:42:07 +0800 Subject: [PATCH 8/9] docs: update plugin redirect port when http_to_https at en usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 罗泽轩 --- docs/en/latest/plugins/redirect.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/en/latest/plugins/redirect.md b/docs/en/latest/plugins/redirect.md index 2c95d5a32600..865ef602d3e6 100644 --- a/docs/en/latest/plugins/redirect.md +++ b/docs/en/latest/plugins/redirect.md @@ -45,7 +45,7 @@ The `redirect` Plugin can be used to configure redirects. Only one of `http_to_https`, `uri` and `regex_uri` can be configured. -* When enable `http_to_https`, you can specify redirect server port with the header `X-Forwarded-Port`. +* When enabling `http_to_https`, the port in the redirect URL will be the value of header `X-Forwarded-Port` or the port of the server. ::: From 75fa6f1cec501dc936f8e6c6c034aca8dd0e850d Mon Sep 17 00:00:00 2001 From: kwanhur Date: Sun, 1 May 2022 21:42:30 +0800 Subject: [PATCH 9/9] docs: update plugin redirect port when http_to_https at zh usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 罗泽轩 --- docs/zh/latest/plugins/redirect.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/zh/latest/plugins/redirect.md b/docs/zh/latest/plugins/redirect.md index fcae7b06d97f..75ece0243e4e 100644 --- a/docs/zh/latest/plugins/redirect.md +++ b/docs/zh/latest/plugins/redirect.md @@ -45,7 +45,7 @@ description: 本文介绍了关于 Apache APISIX `redirect` 插件的基本信 `http_to_https`、`uri` 和 `regex_uri` 只能配置其中一个属性。 -* 当开启 `http_to_https`,你可以设置请求头 `X-Forwarded-Port` 指定重定向的服务器端口。 +* 当开启 `http_to_https` 时,重定向 URL 中的端口将是 `X-Forwarded-Port` 请求头的值或服务器的端口。 :::