Skip to content
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

feature: support http_to_https in redirect plugin. #1642

Merged
merged 3 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,21 @@ https://github.com/iresty/lua-resty-radixtree#operator-list
An example, redirect `http://foo.com` to `https://foo.com`

There are several different ways to do this.
1. `redirect` plugin:
1. Directly use the `http_to_https` in `redirect` plugin:
```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"host": "foo.com",
"plugins": {
"redirect": {
"http_to_https": true
}
}
}'
```

2. Use with advanced routing rule `vars` with `redirect` plugin:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
Expand All @@ -139,7 +153,7 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f03433
}'
```

2. `serverless` plugin:
3. `serverless` plugin:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
Expand Down
18 changes: 16 additions & 2 deletions FAQ_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,21 @@ https://github.com/iresty/lua-resty-radixtree#operator-list
比如,将 `http://foo.com` 重定向到 `https://foo.com`

有几种不同的方法来实现:
1. 使用`redirect`插件:
1. 直接使用 `redirect` 插件的 `http_to_https` 功能:
```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"host": "foo.com",
"plugins": {
"redirect": {
"http_to_https": true
}
}
}'
```

2. 结合高级路由规则 `vars` 和 `redirect` 插件一起使用:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
Expand All @@ -142,7 +156,7 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f03433
}'
```

2. 使用`serverless`插件:
3. 使用`serverless`插件:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
Expand Down
37 changes: 25 additions & 12 deletions apisix/plugins/redirect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ local schema = {
properties = {
ret_code = {type = "integer", minimum = 200, default = 302},
uri = {type = "string", minLength = 2},
http_to_https = {type = "boolean"}, -- default is false
},
required = {"uri"},
oneOf = {
{required = {"uri"}},
{required = {"http_to_https"}}
}
}


Expand Down Expand Up @@ -80,11 +84,13 @@ function _M.check_schema(conf)
return false, err
end

local uri_segs, err = parse_uri(conf.uri)
if not uri_segs then
return false, err
if conf.uri then
local uri_segs, err = parse_uri(conf.uri)
if not uri_segs then
return false, err
end
core.log.info(core.json.delay_encode(uri_segs))
end
core.log.info(core.json.delay_encode(uri_segs))

return true
end
Expand Down Expand Up @@ -120,15 +126,22 @@ end
function _M.rewrite(conf, ctx)
core.log.info("plugin rewrite phase, conf: ", core.json.delay_encode(conf))

local new_uri, err = concat_new_uri(conf.uri, ctx)
if not new_uri then
core.log.error("failed to generate new uri by: ", conf.uri, " error: ",
err)
core.response.exit(500)
if conf.http_to_https and ctx.var.scheme == "http" then
conf.uri = "https://$host$request_uri"
conf.ret_code = 301
end

core.response.set_header("Location", new_uri)
core.response.exit(conf.ret_code)
if conf.uri and conf.ret_code then
local new_uri, err = concat_new_uri(conf.uri, ctx)
if not new_uri then
core.log.error("failed to generate new uri by: ", conf.uri, " error: ",
err)
core.response.exit(500)
end

core.response.set_header("Location", new_uri)
core.response.exit(conf.ret_code)
end
end


Expand Down
20 changes: 18 additions & 2 deletions doc/plugins/redirect-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ URI 重定向插件。

|名称 |必须|描述|
|------- |-----|------|
|uri |是| 可以包含 Nginx 变量的 URI,例如:`/test/index.html`, `$uri/index.html`。你可以通过类似于 `$ {xxx}` 的方式引用变量,以避免产生歧义,例如:`${uri}foo/index.html`。若你需要保留 `$` 字符,那么使用如下格式:`/\$foo/index.html`。|
|ret_code|否|请求响应码,默认值为 `302`。|
|uri |是,与 `http_to_https` 二选一| 可以包含 Nginx 变量的 URI,例如:`/test/index.html`, `$uri/index.html`。你可以通过类似于 `$ {xxx}` 的方式引用变量,以避免产生歧义,例如:`${uri}foo/index.html`。若你需要保留 `$` 字符,那么使用如下格式:`/\$foo/index.html`。|
|ret_code|否,只和 `uri` 配置使用。|请求响应码,默认值为 `302`。|
|http_to_https|是,与 `uri` 二选一|布尔值,默认是 `false`。当设置为 `ture` 并且请求是 http 时,会自动 301 重定向为 https,uri 保持不变|

### 示例

Expand Down Expand Up @@ -94,6 +95,21 @@ Location: /test/default.html

我们可以检查响应码和响应头中的 `Location` 参数,它表示该插件已启用。

```

下面是一个实现 http 到 https 跳转的示例:
```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"redirect": {
"http_to_https": true
}
}
}'
```

#### 禁用插件

移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务:
Expand Down
18 changes: 16 additions & 2 deletions doc/plugins/redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ URI redirect.

|Name |Requirement|Description|
|------- |-----|------|
|uri |required| New uri 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.|
|ret_code|optional|Response code, the default value is `302`.|
|uri |required, need pick one from `uri` and `http_to_https`| New uri 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.|
|ret_code|optional, only works with `uri`|Response code, the default value is `302`.|
|http_to_https|required, need pick one from `uri` and `http_to_https`|Boolean value. The default value is `false`. When it is set to `ture` 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.|

## How To Enable

Expand Down Expand Up @@ -101,6 +102,19 @@ We can check the response code and the response header `Location`.

It shows that the `redirect` plugin is in effect.

Here is an example of redirect HTTP to HTTPS:
```shell
curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"redirect": {
"http_to_https": true
}
}
}'
```

## Disable Plugin

When you want to disable the `redirect` plugin, it is very simple,
Expand Down
Loading