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/04/22 10:07:11 UTC
[GitHub] [apisix] Hxhong opened a new issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Hxhong opened a new issue #4110:
URL: https://github.com/apache/apisix/issues/4110
### Issue description
### 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:
* luarocks version, if the issue is about installation (cmd: `luarocks --version`):
I rewrite ctx.var.upstream_uri, and it did changed. But in error.log, the log's param "request" is not the new ctx.var.upstream_uri. The below methods didn't work, either.
ngx.request_uri = ctx.var.upstream_uri
ngx.req.upstream_uri = ctx.var.upstream_uri
ngx.req.set_uri(ctx.var.upstream_uri)
ngx.regex_uri = ctx.var.upstream_uri
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825441990
> ctx.var.upstream_uri = upstream_uri
Try to rewrite your URI in this way?
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826202505
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826271607
> ![image](https://user-images.githubusercontent.com/42258295/115848274-46637d00-a456-11eb-87e7-6e2e6e7e1d1e.png)
> the first image I print the param 'upstream_uri' before I add other params into uri
> ![image](https://user-images.githubusercontent.com/42258295/115848425-6dba4a00-a456-11eb-9d7e-abe974292651.png)
> the second image i print the param 'ctx.var.upstream_uri' after I add two params, 'token' and 'srcSystemCode'. But you can see in the log, the param 'request' without my params.
From these two pictures, `upstream_uri` is different from `ctx.var.upstream_uri`, it seems that `uri` has been rewritten.
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826271607
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826288713
> <img alt="截屏2021-04-25 下午3 14 35" width="545" src="https://user-images.githubusercontent.com/52862365/115984317-185a7600-a5d9-11eb-80a2-d9253c09102d.png">
>
> You can try to add more log output here to get a detailed comparison of uri rewriting. For example, print `upstream_uri` and compare with `ctx.var.upstream_uri`.
> > ![image](https://user-images.githubusercontent.com/42258295/115848274-46637d00-a456-11eb-87e7-6e2e6e7e1d1e.png)
> > the first image I print the param 'upstream_uri' before I add other params into uri
> > ![image](https://user-images.githubusercontent.com/42258295/115848425-6dba4a00-a456-11eb-9d7e-abe974292651.png)
> > the second image i print the param 'ctx.var.upstream_uri' after I add two params, 'token' and 'srcSystemCode'. But you can see in the log, the param 'request' without my params.
>
> From these two pictures, `upstream_uri` is different from `ctx.var.upstream_uri`, it seems that `uri` has been rewritten.
yes, 'uri' has been rewritten, but the actual request doesn't take the param 'token' with it. You can try it with my demo.
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-824744966
You can refer to the proxy-rewrite plugin. https://github.com/apache/apisix/blob/master/apisix/plugins/proxy-rewrite.lua#L146-L179
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826202505
> > But you can see in the log, the param 'request' without my params.
>
> You should provide sample code for this part.
![image](https://user-images.githubusercontent.com/42258295/115977535-cc450c80-a5ab-11eb-9875-893241381eb4.png)
I've already provided it. Please refer to this demo.
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-824776964
> You can refer to the proxy-rewrite plugin. https://github.com/apache/apisix/blob/master/apisix/plugins/proxy-rewrite.lua#L146-L179
yes, I did refer to proxy-rewite plugin
--
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] Hxhong edited a comment on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong edited a comment on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825515989
![image](https://user-images.githubusercontent.com/42258295/115848274-46637d00-a456-11eb-87e7-6e2e6e7e1d1e.png)
the first image I print the param 'upstream_uri' before I add other params into uri
![image](https://user-images.githubusercontent.com/42258295/115848425-6dba4a00-a456-11eb-9d7e-abe974292651.png)
the second image i print the param 'ctx.var.upstream_uri' after I add two params, 'token' and 'srcSystemCode'. But you can see in the log, the param 'request' without my params.
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825585144
> But you can see in the log, the param 'request' without my params.
You should provide sample code for this part.
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-824777706
-- this is a demo, you can refer to it
local core = require("apisix.core")
local http = require("resty.http")
local plugin_name = "request-rewrite"
local pairs = pairs
local ipairs = ipairs
local ngx = ngx
local type = type
local re_sub = ngx.re.sub
local sub_str = string.sub
local str_find = core.string.find
local schema = {
type = "object",
properties = {
}
}
local _M = {
version = 0.1,
priority = 1000,
name = plugin_name,
schema = schema,
}
function _M.check_schema(conf)
local ok, err = core.schema.check(schema, conf)
if not ok then
return false, err
end
if conf.regex_uri and #conf.regex_uri > 0 then
local _, _, err = re_sub("/fake_uri", conf.regex_uri[1],
conf.regex_uri[2], "jo")
if err then
return false, "invalid regex_uri(" .. conf.regex_uri[1] ..
", " .. conf.regex_uri[2] .. "): " .. err
end
end
-- check headers
if not conf.headers then
return true
end
for field, value in pairs(conf.headers) do
if type(field) ~= 'string' then
return false, 'invalid type as header field'
end
if type(value) ~= 'string' and type(value) ~= 'number' then
return false, 'invalid type as header value'
end
if #field == 0 then
return false, 'invalid field length in header'
end
core.log.info("header field: ", field)
if not core.utils.validate_header_field(field) then
return false, 'invalid field character in header'
end
if not core.utils.validate_header_value(value) then
return false, 'invalid value character in header'
end
end
return true
end
do
local upstream_vars = {
host = "upstream_host",
upgrade = "upstream_upgrade",
connection = "upstream_connection",
}
local upstream_names = {}
for name, _ in pairs(upstream_vars) do
core.table.insert(upstream_names, name)
end
function _M.rewrite(conf, ctx)
for _, name in ipairs(upstream_names) do
if conf[name] then
ctx.var[upstream_vars[name]] = conf[name]
end
end
if conf["scheme"] then
ctx.upstream_scheme = conf["scheme"]
end
local upstream_uri = ctx.var.uri
core.log.warn("upstream_uri: "..upstream_uri)
if conf.uri ~= nil then
upstream_uri = core.utils.resolve_var(conf.uri, ctx.var)
elseif conf.regex_uri ~= nil then
local uri, _, err = re_sub(ctx.var.uri, conf.regex_uri[1],
conf.regex_uri[2], "jo")
if uri then
upstream_uri = uri
else
local msg = "failed to substitute the uri " .. ctx.var.uri ..
" (" .. conf.regex_uri[1] .. ") with " ..
conf.regex_uri[2] .. " : " .. err
-- core.log.error(msg)
return 500, {message = msg}
end
end
local index = str_find(upstream_uri, "?")
if index then
upstream_uri = core.utils.uri_safe_encode(sub_str(upstream_uri, 1, index-1)) ..
sub_str(upstream_uri, index)
else
upstream_uri = core.utils.uri_safe_encode(upstream_uri)
end
if ctx.var.is_args == "?" then
if index then
ctx.var.upstream_uri = upstream_uri .. "&" .. (ctx.var.args or "")
else
ctx.var.upstream_uri = upstream_uri .. "?" .. (ctx.var.args or "")
end
ctx.var.upstream_uri = ctx.var.upstream_uri .. "&token=123456"
else
ctx.var.upstream_uri = upstream_uri .. "&token=123456"
end
core.log.warn("ctx.var.upstream_uri: "..ctx.var.upstream_uri)
-- 设置以下四种方式,error.log 打印的日志里,ctx.var.upstream_uri 是新的 uri, 但日志自带的 request 参数的 uri 仍是原始的 uri
-- ngx.request_uri = ctx.var.upstream_uri
-- ngx.req.upstream_uri = ctx.var.upstream_uri
-- ngx.req.set_uri(ctx.var.upstream_uri)
-- ngx.regex_uri = ctx.var.upstream_uri
if not conf.headers then
return
end
if not conf.headers_arr then
conf.headers_arr = {}
for field, value in pairs(conf.headers) do
core.table.insert_tail(conf.headers_arr, field, value)
end
end
local field_cnt = #conf.headers_arr
for i = 1, field_cnt, 2 do
ngx.req.set_header(conf.headers_arr[i],
core.utils.resolve_var(conf.headers_arr[i+1], ctx.var))
end
end
end -- do
return _M
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825472803
> > ctx.var.upstream_uri = upstream_uri
>
> Try to rewrite your URI in this way?
it's the same...
--
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] Hxhong edited a comment on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong edited a comment on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826202505
> > But you can see in the log, the param 'request' without my params.
>
> You should provide sample code for this part.
![image](https://user-images.githubusercontent.com/42258295/115977535-cc450c80-a5ab-11eb-9875-893241381eb4.png)
I've already provided it. Please refer to this demo. The param "request" is the log's param, not mine.
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825473436
> @Hxhong you demo `priority = 1000` it seems to be duplicate of the existing plugin
> and in plugin develop doc i find this
>
> > Note : The priority of the new plugin cannot be same to any existing ones, you can use the /v1/schema method of control API to view the priority of all plugins. In addition, plugins with higher priority value will be executed first in a given phase (see the definition of phase in choose-phase-to-run). For example, the priority of example-plugin is 0 and the priority of ip-restriction is 3000. Therefore, the ip-restriction plugin will be executed first, then the example-plugin plugin.
it's not the reason
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826272617
<img width="545" alt="截屏2021-04-25 下午3 14 35" src="https://user-images.githubusercontent.com/52862365/115984317-185a7600-a5d9-11eb-80a2-d9253c09102d.png">
You can try to add more log output here to get a detailed comparison of uri rewriting. For example, print `upstream_uri` and compare with `ctx.var.upstream_uri`.
--
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] xxm404 commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
xxm404 commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-824793141
@Hxhong you demo `priority = 1000` it seems to be duplicate of the existing plugin
and in plugin develop doc i find this
> Note : The priority of the new plugin cannot be same to any existing ones, you can use the /v1/schema method of control API to view the priority of all plugins. In addition, plugins with higher priority value will be executed first in a given phase (see the definition of phase in choose-phase-to-run). For example, the priority of example-plugin is 0 and the priority of ip-restriction is 3000. Therefore, the ip-restriction plugin will be executed first, then the example-plugin plugin.
--
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] Hxhong commented on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825515989
![image](https://user-images.githubusercontent.com/42258295/115847191-47e07580-a455-11eb-8f06-81fa459aec66.png)
the first image I print the param 'upstream_uri' before I add other params into uri
![image](https://user-images.githubusercontent.com/42258295/115847423-88d88a00-a455-11eb-9bf6-67fb68123898.png)
the second image i print the param 'ctx.var.upstream_uri' after I add two params, 'token' and 'srcSystemCode'. But you can see in the log, the param 'request' without my params.
--
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] Hxhong edited a comment on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong edited a comment on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-824776964
> You can refer to the proxy-rewrite plugin. https://github.com/apache/apisix/blob/master/apisix/plugins/proxy-rewrite.lua#L146-L179
yes, I did refer to proxy-rewrite plugin
--
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 #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Firstsawyou commented on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-825492956
> > > ctx.var.upstream_uri = upstream_uri
> >
> >
> > Try to rewrite your URI in this way?
>
> it's the same...
After using this method, can you provide access.log log information?
--
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] Hxhong edited a comment on issue #4110: request help: I develop a plugin to rewrite URI, but it doesn't work. How to rewrite URI
Posted by GitBox <gi...@apache.org>.
Hxhong edited a comment on issue #4110:
URL: https://github.com/apache/apisix/issues/4110#issuecomment-826202505
> > But you can see in the log, the param 'request' without my params.
>
> You should provide sample code for this part.
![image](https://user-images.githubusercontent.com/42258295/115977535-cc450c80-a5ab-11eb-9875-893241381eb4.png)
I've already provided it. Please refer to this demo. The param "request" is the log's param, not mine.
--
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