You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by ag...@apache.org on 2020/05/26 15:11:39 UTC
[incubator-apisix] branch master updated: test cases: add doc and
test cases for how to redirect http to https. (#1595)
This is an automated email from the ASF dual-hosted git repository.
agile6v pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-apisix.git
The following commit(s) were added to refs/heads/master by this push:
new 67a2096 test cases: add doc and test cases for how to redirect http to https. (#1595)
67a2096 is described below
commit 67a20965408ee9bb354b9bfb59fcf5bd38584905
Author: Wen Ming <mo...@gmail.com>
AuthorDate: Tue May 26 23:11:29 2020 +0800
test cases: add doc and test cases for how to redirect http to https. (#1595)
* add FAQ about redirect http To https
* add test cases for serverless plugin and redirect plugin
Co-authored-by: rhubard <18...@163.com>
---
FAQ.md | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++-
FAQ_CN.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-
t/plugin/redirect.t | 52 +++++++++++++++++++++++++++++++++++++
t/plugin/serverless.t | 59 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 252 insertions(+), 2 deletions(-)
diff --git a/FAQ.md b/FAQ.md
index c75218f..55c1590 100644
--- a/FAQ.md
+++ b/FAQ.md
@@ -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 '
{
@@ -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...
diff --git a/FAQ_CN.md b/FAQ_CN.md
index 8221cd4..2a576a6 100644
--- a/FAQ_CN.md
+++ b/FAQ_CN.md
@@ -73,7 +73,7 @@ luarocks 服务。 运行 `luarocks config rocks_servers` 命令(这个命令
如果使用代理仍然解决不了这个问题,那可以在安装的过程中添加 `--verbose` 选项来查看具体是慢在什么地方。排除前面的
第一种情况,只可能是第二种,`git` 协议被封。这个时候可以执行 `git config --global url."https://".insteadOf git://` 命令使用 `https` 协议替代。
-## 如何通过APISIX支持A/B测试?
+## 如何通过 APISIX 支持 A/B 测试?
比如,根据入参`arg_id`分组:
@@ -81,6 +81,7 @@ luarocks 服务。 运行 `luarocks config rocks_servers` 命令(这个命令
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 '
{
@@ -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`。
diff --git a/t/plugin/redirect.t b/t/plugin/redirect.t
index 0f413c5..2ff80ec 100644
--- a/t/plugin/redirect.t
+++ b/t/plugin/redirect.t
@@ -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
diff --git a/t/plugin/serverless.t b/t/plugin/serverless.t
index d6c691a..ad78286 100644
--- a/t/plugin/serverless.t
+++ b/t/plugin/serverless.t
@@ -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