You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/03/24 07:49:18 UTC

[GitHub] [apisix] Gary-Airwallex opened a new issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Gary-Airwallex opened a new issue #3903:
URL: https://github.com/apache/apisix/issues/3903


   ### Issue description
   Minimum steps to reproduce:
   In `init.lua`, add 
   
   ### Environment
   
   * apisix version (cmd: `apisix version`):
   * OS (cmd: `uname -a`):
   * OpenResty / Nginx version (cmd: `nginx -V` or `openresty -V`):
   * etcd version, if have (cmd: run `curl http://127.0.0.1:9090/v1/server_info` to get the info from server-info API):
   * apisix-dashboard version, if have:
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-807926060


   It is because `ctx.var` caches the result:
   https://github.com/apache/apisix/blob/83931ba0a5c95e04ec867a4ffd0821d426ca0d27/apisix/core/ctx.lua#L186


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] zwjzxh520 commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
zwjzxh520 commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-812368163


   @Firstsawyou 你好,请问这个问题怎么修复呢?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] Firstsawyou edited a comment on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
Firstsawyou edited a comment on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-807035124


   Hi, @Gary-Airwallex  Sorry, my previous test method was incorrect. I checked the latest version again, and there seems to be such a problem.   @spacewander  Please take a look at this question when you are free.
   
   Here are the steps to reproduce:
   
   1. Add the log of `ctx.var.upstream_uri` in the rewrite phase of the `cors` plugin, and print the rewritten uri in the `proxy-rewrite` plugin.
   
   ```shell
   diff --git a/apisix/plugins/cors.lua b/apisix/plugins/cors.lua
   index 4b0b7c4..98beecc 100644
   --- a/apisix/plugins/cors.lua
   +++ b/apisix/plugins/cors.lua
   @@ -225,6 +225,7 @@ end
    
    
    function _M.rewrite(conf, ctx)
   +    core.log.warn("Access upstream uri: ", ctx.var.upstream_uri)
        if ctx.var.request_method == "OPTIONS" then
            return 200
        end
   diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua
   index c678b59..e0c3d58 100644
   --- a/apisix/plugins/proxy-rewrite.lua
   +++ b/apisix/plugins/proxy-rewrite.lua
   @@ -179,6 +179,7 @@ function _M.rewrite(conf, ctx)
        end
    
        if not conf.headers then
   +        core.log.warn("upstream_uri: ", ctx.var.upstream_uri)
            return
        end
   ```
   
   2. Add the global plugin `cors`
   
   ```shell
   curl -X PUT http://127.0.0.1:9080/apisix/admin/global_rules/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -d '{
           "plugins": {
              "cors": {}
           }
   }'
   ```
   
   3. Add a route and bind the `proxy-rewrite` plugin
   
   ```shell
   curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "methods": ["GET"],
       "uri": "/hello",
       "plugins": {
           "proxy-rewrite": {
               "uri": "/route01"
           }
       },
       "upstream": {
           "type": "roundrobin",
           "nodes": {
               "127.0.0.1:1990": 1
           }
       }
   }'
   ```
   
   4. Test route (upstream default response data is the requested uri)
   
   ```shell
   curl http://127.0.0.1:9080/hello -i
   HTTP/1.1 200 OK
   Content-Type: application/octet-stream
   Transfer-Encoding: chunked
   Connection: keep-alive
   Date: Thu, 25 Mar 2021 16:05:58 GMT
   Server: APISIX/2.4
   Access-Control-Allow-Origin: *
   Access-Control-Allow-Methods: *
   Access-Control-Max-Age: 5
   Access-Control-Expose-Headers: *
   Access-Control-Allow-Headers: *
   
   request_uri: /hello
   ```
   
   The `proxy-rewrite` plugin rewriting uri is not effective.
   
   `access.log` information:
   
   ```
   [warn] 2707#2707: *62368 [lua] proxy-rewrite.lua:182: phase_func(): upstream_uri: /route01, client: 127.0.0.1,
   ```
   
   5. Remove the log of `ctx.var.upstream_uri` in the global plugin `cors`
   
   ```shell
   diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua
   index c678b59..e0c3d58 100644
   --- a/apisix/plugins/proxy-rewrite.lua
   +++ b/apisix/plugins/proxy-rewrite.lua
   @@ -179,6 +179,7 @@ function _M.rewrite(conf, ctx)
        end
    
        if not conf.headers then
   +        core.log.warn("upstream_uri: ", ctx.var.upstream_uri)
            return
        end
   ```
   
   6. Test the route again (the upstream default response data is the requested uri)
   
   ```shell
   $ curl http://127.0.0.1:9080/hello -i
   HTTP/1.1 200 OK
   Content-Type: application/octet-stream
   Transfer-Encoding: chunked
   Connection: keep-alive
   Date: Thu, 25 Mar 2021 16:13:31 GMT
   Server: APISIX/2.4
   Access-Control-Allow-Origin: *
   Access-Control-Allow-Methods: *
   Access-Control-Max-Age: 5
   Access-Control-Expose-Headers: *
   Access-Control-Allow-Headers: *
   
   request_uri: /route01
   ```
   The `proxy-rewrite` plugin rewrites the uri to take effect.
   
   `access.log` information:
   
   ```
   [warn] 2707#2707: *62368 [lua] proxy-rewrite.lua:182: phase_func(): upstream_uri: /route01, client: 127.0.0.1,
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] zwjzxh520 commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
zwjzxh520 commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-812387306


   懂了,难道我一直在 #3914 里找不到修复的源码,只看到增加的测试用例。谢谢


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-807926957


   One solution is to use ngx.var instead:
   https://github.com/apache/apisix/pull/3914


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] Firstsawyou commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-805742477


   Hi, @Gary-Airwallex 
   I did not reproduce this problem locally. I will provide a recurring use case later.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-820165163


   Solved by #3915 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] Firstsawyou commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-812377486


   Hi, @zwjzxh520 
   @spacewander  provides two solutions, you can refer to the following:
   
   Method one: use `ngx.var`  instead `ctx.var`, for example: `ngx.var.upstream_uri` .  For detailed examples, you can refer to #3914 
   
   Method two:  use a new way to cache ctx.var: #3915
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] zwjzxh520 commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
zwjzxh520 commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-812347987


   臣附议


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] Firstsawyou commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-805622830


   Not sure if this problem is fixed, maybe you can try the latest version.
   Let me check if I have this problem in the latest version.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander closed issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
spacewander closed issue #3903:
URL: https://github.com/apache/apisix/issues/3903


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] Firstsawyou commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-807035124


   Hi, @Gary-Airwallex  Sorry, my previous test method was incorrect. I checked the latest version again, and there seems to be such a problem.   @spacewander  Please take a look at this question when you are free.
   
   Here are the steps to reproduce:
   
   1. Add the log of `ctx.var.upstream_uri` in the rewrite phase of the `cors` plugin, and print the rewritten uri in the `proxy-rewrite` plugin.
   
   ```shell
   diff --git a/apisix/plugins/cors.lua b/apisix/plugins/cors.lua
   index 4b0b7c4..98beecc 100644
   --- a/apisix/plugins/cors.lua
   +++ b/apisix/plugins/cors.lua
   @@ -225,6 +225,7 @@ end
    
    
    function _M.rewrite(conf, ctx)
   +    core.log.warn("Access upstream uri: ", ctx.var.upstream_uri)
        if ctx.var.request_method == "OPTIONS" then
            return 200
        end
   diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua
   index c678b59..e0c3d58 100644
   --- a/apisix/plugins/proxy-rewrite.lua
   +++ b/apisix/plugins/proxy-rewrite.lua
   @@ -179,6 +179,7 @@ function _M.rewrite(conf, ctx)
        end
    
        if not conf.headers then
   +        core.log.warn("upstream_uri: ", ctx.var.upstream_uri)
            return
        end
   ```
   
   2. Add the global plugin `cors`
   
   ```shell
   curl -X PUT http://127.0.0.1:9080/apisix/admin/global_rules/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -d '{
           "plugins": {
              "cors": {}
           }
   }'
   ```
   
   3. Add a route and bind the `proxy-rewrite` plugin
   
   ```shell
   curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
   {
       "methods": ["GET"],
       "uri": "/hello",
       "plugins": {
           "proxy-rewrite": {
               "uri": "/route01"
           }
       },
       "upstream": {
           "type": "roundrobin",
           "nodes": {
               "127.0.0.1:1990": 1
           }
       }
   }'
   ```
   
   4. Test route (upstream default response data is the requested uri)
   
   ```shell
   curl http://127.0.0.1:9080/hello -i
   HTTP/1.1 200 OK
   Content-Type: application/octet-stream
   Transfer-Encoding: chunked
   Connection: keep-alive
   Date: Thu, 25 Mar 2021 16:05:58 GMT
   Server: APISIX/2.4
   Access-Control-Allow-Origin: *
   Access-Control-Allow-Methods: *
   Access-Control-Max-Age: 5
   Access-Control-Expose-Headers: *
   Access-Control-Allow-Headers: *
   
   request_uri: /hello
   ```
   
   The `proxy-rewrite` plug-in rewriting uri is not effective.
   
   `access.log` information:
   
   ```
   [warn] 2707#2707: *62368 [lua] proxy-rewrite.lua:182: phase_func(): upstream_uri: /route01, client: 127.0.0.1,
   ```
   
   5. Remove the log of `ctx.var.upstream_uri` in the global plugin `cors`
   
   ```shell
   diff --git a/apisix/plugins/proxy-rewrite.lua b/apisix/plugins/proxy-rewrite.lua
   index c678b59..e0c3d58 100644
   --- a/apisix/plugins/proxy-rewrite.lua
   +++ b/apisix/plugins/proxy-rewrite.lua
   @@ -179,6 +179,7 @@ function _M.rewrite(conf, ctx)
        end
    
        if not conf.headers then
   +        core.log.warn("upstream_uri: ", ctx.var.upstream_uri)
            return
        end
   ```
   
   6. Test the route again (the upstream default response data is the requested uri)
   
   ```shell
   $ curl http://127.0.0.1:9080/hello -i
   HTTP/1.1 200 OK
   Content-Type: application/octet-stream
   Transfer-Encoding: chunked
   Connection: keep-alive
   Date: Thu, 25 Mar 2021 16:13:31 GMT
   Server: APISIX/2.4
   Access-Control-Allow-Origin: *
   Access-Control-Allow-Methods: *
   Access-Control-Max-Age: 5
   Access-Control-Expose-Headers: *
   Access-Control-Allow-Headers: *
   
   request_uri: /route01
   ```
   The `proxy-rewrite` plug-in rewrites the uri to take effect.
   
   `access.log` information:
   
   ```
   [warn] 2707#2707: *62368 [lua] proxy-rewrite.lua:182: phase_func(): upstream_uri: /route01, client: 127.0.0.1,
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] spacewander commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
spacewander commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-807927812


   Another solution is to use a new way to cache `ctx.var`:
   https://github.com/apache/apisix/pull/3915
   
   This way has performance slow down, CC @membphis 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] Firstsawyou commented on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-807952132


   > It is because `ctx.var` caches the result:
   > https://github.com/apache/apisix/blob/83931ba0a5c95e04ec867a4ffd0821d426ca0d27/apisix/core/ctx.lua#L186
   
   Got it, thank you. 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [apisix] zwjzxh520 edited a comment on issue #3903: request help: Accessing ctx.var.upstream_uri in global rule causes failure of proxy-rewrite

Posted by GitBox <gi...@apache.org>.
zwjzxh520 edited a comment on issue #3903:
URL: https://github.com/apache/apisix/issues/3903#issuecomment-812387306


   懂了,难怪我一直在 #3914 里找不到修复的源码,只看到增加的测试用例。谢谢


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org