Skip to content

Commit

Permalink
test cases: add doc and test cases for how to redirect http to https. (
Browse files Browse the repository at this point in the history
…#1595)

* add FAQ about redirect http To https

* add test cases for serverless plugin and redirect plugin

Co-authored-by: rhubard <[email protected]>
  • Loading branch information
moonming and rhubard authored May 26, 2020
1 parent 1bc2af5 commit 67a2096
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 2 deletions.
72 changes: 71 additions & 1 deletion FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ An example, if you want to group by the request param `arg_id`:
1. Group A:arg_id <= 1000
2. Group B:arg_id > 1000

here is the way
here is the way:
```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
Expand Down Expand Up @@ -107,11 +107,81 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335
}'
```


Here is the operator list of current `lua-resty-radixtree`
https://github.com/iresty/lua-resty-radixtree#operator-list

## How to redirect http to https via APISIX?

An example, redirect `http://foo.com` to `https://foo.com`

There are several different ways to do this.
1. `redirect` plugin:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"host": "foo.com",
"vars": [
[
"scheme",
"==",
"http"
]
],
"plugins": {
"redirect": {
"uri": "https://$host$request_uri",
"ret_code": 301
}
}
}'
```

2. `serverless` plugin:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions": ["return function() if ngx.var.scheme == \"http\" and ngx.var.host == \"foo.com\" then ngx.header[\"Location\"] = \"https://foo.com\" .. ngx.var.request_uri; ngx.exit(ngx.HTTP_MOVED_PERMANENTLY); end; end"]
}
}
}'
```

Then test it to see if it works:
```shell
curl -i -H 'Host: foo.com' http://127.0.0.1:9080/hello
```

The response body should be:
```
HTTP/1.1 301 Moved Permanently
Date: Mon, 18 May 2020 02:56:04 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive
Location: https://foo.com/hello
Server: APISIX web server
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>openresty</center>
</body>
</html>
```


## How to fix OpenResty Installation Failure on MacOS 10.15
When you install the OpenResty on MacOs 10.15, you may face this error

```shell
> brew install openresty
Updating Homebrew...
Expand Down
71 changes: 70 additions & 1 deletion FAQ_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ luarocks 服务。 运行 `luarocks config rocks_servers` 命令(这个命令
如果使用代理仍然解决不了这个问题,那可以在安装的过程中添加 `--verbose` 选项来查看具体是慢在什么地方。排除前面的
第一种情况,只可能是第二种,`git` 协议被封。这个时候可以执行 `git config --global url."https://".insteadOf git://` 命令使用 `https` 协议替代。

## 如何通过APISIX支持A/B测试
## 如何通过 APISIX 支持 A/B 测试

比如,根据入参`arg_id`分组:

1. A组:arg_id <= 1000
2. B组:arg_id > 1000

可以这么做:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
Expand Down Expand Up @@ -109,9 +110,77 @@ curl -i http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335
}'
```


更多的 lua-resty-radixtree 匹配操作,可查看操作列表:
https://github.com/iresty/lua-resty-radixtree#operator-list

## 如何支持 http 自动跳转到 https?

比如,将 `http://foo.com` 重定向到 `https://foo.com`

有几种不同的方法来实现:
1. 使用`redirect`插件:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"host": "foo.com",
"vars": [
[
"scheme",
"==",
"http"
]
],
"plugins": {
"redirect": {
"uri": "https://$host$request_uri",
"ret_code": 301
}
}
}'
```

2. 使用`serverless`插件:

```shell
curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"uri": "/hello",
"plugins": {
"serverless-pre-function": {
"phase": "rewrite",
"functions": ["return function() if ngx.var.scheme == \"http\" and ngx.var.host == \"foo.com\" then ngx.header[\"Location\"] = \"https://foo.com\" .. ngx.var.request_uri; ngx.exit(ngx.HTTP_MOVED_PERMANENTLY); end; end"]
}
}
}'
```

然后测试下是否生效:
```shell
curl -i -H 'Host: foo.com' http://127.0.0.1:9080/hello
```

响应体应该是:
```
HTTP/1.1 301 Moved Permanently
Date: Mon, 18 May 2020 02:56:04 GMT
Content-Type: text/html
Content-Length: 166
Connection: keep-alive
Location: https://foo.com/hello
Server: APISIX web server
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>openresty</center>
</body>
</html>
```

## 如何修改日志等级

默认的APISIX日志等级为`warn`,如果需要查看`core.log.info`的打印结果需要将日志等级调整为`info`
Expand Down
52 changes: 52 additions & 0 deletions t/plugin/redirect.t
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,55 @@ Location: /hello//bar
--- error_code: 301
--- no_error_log
[error]
=== TEST 15: http -> https redirect
--- 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",
"vars": [
[
"scheme",
"==",
"http"
]
],
"plugins": {
"redirect": {
"uri": "https://$host$request_uri",
"ret_code": 301
}
}
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]
=== TEST 16: redirect
--- request
GET /hello
--- more_headers
Host: foo.com
--- error_code: 301
--- response_headers
Location: https://foo.com/hello
59 changes: 59 additions & 0 deletions t/plugin/serverless.t
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,62 @@ passed
GET /hello
--- error_log
serverless pre function:2



=== TEST 19: http -> https redirect
--- 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,
[[{
"plugins": {
"serverless-pre-function": {
"functions" : ["return function() if ngx.var.scheme == \"http\" and ngx.var.host == \"foo.com\" then ngx.header[\"Location\"] = \"https://foo.com\" .. ngx.var.request_uri; ngx.exit(ngx.HTTP_MOVED_PERMANENTLY); end; end"]
}
},
"uri": "/hello"
}]],
[[{
"node": {
"value": {
"plugins": {
"serverless-pre-function": {
"functions" : ["return function() if ngx.var.scheme == \"http\" and ngx.var.host == \"foo.com\" then ngx.header[\"Location\"] = \"https://foo.com\" .. ngx.var.request_uri; ngx.exit(ngx.HTTP_MOVED_PERMANENTLY); end; end"]
}
},
"uri": "/hello"
},
"key": "/apisix/routes/1"
},
"action": "set"
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- request
GET /t
--- more_headers
Host: foo.com
--- response_body
passed
--- no_error_log
[error]



=== TEST 20: check plugin
--- request
GET /hello
--- more_headers
Host: foo.com
--- error_code: 301
--- response_headers
Location: https://foo.com/hello

0 comments on commit 67a2096

Please sign in to comment.