英文:
How do I transform query string parameters in nginx?
问题
如何使用nginx根据某些规则来转换传入的查询字符串参数?具体来说,我想要在每个查询字符串参数值前添加一个前缀。特别是,我想要执行以下操作。
给定这个URI:
http://localhost:3000/account?id=12345&name=bill
我希望将其转换为以下形式:
http://localhost:3000/account?id=eq.12345&name=eq.bill
其中每个项目中的前缀是 eq.
。原因是我正在将nginx用作PostgREST的反向代理,而PostgREST使用了类似 eq.
的操作符构建了一个丰富的URL语法,但我需要与不了解该语法的客户一起使用。
我已经研究了nginx的rewrite和map模块。后者看起来很有前途,但我还没有弄清楚如何使用它。
我还尝试了一些Lua脚本,试图循环遍历 ngx.req.get_uri_args
,但目前我正在调试Lua代码。
events {
}
http {
resolver 127.0.0.11 ipv6=off;
server {
listen 80;
listen [::]:80;
location / {
default_type text/plain;
access_by_lua_block {
local qs = {}
for key, val in pairs(ngx.req.get_uri_args()) do
table.insert(qs, key, "eq." .. val)
end
ngx.req.set_uri_args(qs)
}
proxy_pass http://postgrest:3000;
}
}
}
英文:
How do I use nginx to transform incoming query string parameters according to some rule? In particular, I want to prepend a prefix onto each and every query string parameter value. In particular, I would like to do the following.
Given this URI:
http://localhost:3000/account?id=12345&name=bill
I would like it to be such that this is transformed into
http://localhost:3000/account?id=eq.12345&name=eq.bill
Where the prefix in each item is eq.
The reason is that I'm using nginx as a reverse proxy for PostgREST, and PostgREST has a rich URL syntax built around operators like eq.
, but I need to use it with clients who don't know that syntax.
I've looked into the nginx rewrite and the nginx map module. The latter looks promising but I haven't yet figured out out to use it.
I did also try some lua scripting, to try to loop over npx.req.get_uri_args
but at present I'm debugging the Lua code.
events {
}
http {
resolver 127.0.0.11 ipv6=off;
server {
listen 80;
listen [::]:80;
location / {
default_type text/plain;
access_by_lua_block {
local qs = {}
for key, val in pairs(ngx.req.get_uri_args()) do
table.insert(qs, key, "eq." .. val)
end
ngx.req.set_uri_args(qs)
}
proxy_pass http://postgrest:3000;
}
}
}
答案1
得分: 2
我相信我回答了自己的问题,但我欢迎更多的意见和贡献。以下Lua脚本在nginx.conf
中有效:
events {
}
http {
resolver 127.0.0.11 ipv6=off;
server {
listen 80;
listen [::]:80;
location / {
access_by_lua_block {
local qs = {}
for key, val in pairs(ngx.req.get_uri_args()) do
qs[key] = "eq." ..val
end
ngx.req.set_uri_args(qs)
}
proxy_pass http://postgrest:3000;
}
}
}
我很想知道在nginx中是否有Lua脚本的替代方法,但这似乎起到了作用。
英文:
I believe I answered my own question, though I welcome more input and contributions. The following Lua scripting worked in nginx.conf
:
events {
}
http {
resolver 127.0.0.11 ipv6=off;
server {
listen 80;
listen [::]:80;
location / {
access_by_lua_block {
local qs = {}
for key, val in pairs(ngx.req.get_uri_args()) do
qs[key] = "eq." ..val
end
ngx.req.set_uri_args(qs)
}
proxy_pass http://postgrest:3000;
}
}
}
I'd love to know if there are alternatives in nginx to Lua scripting, but this seemed to do the trick.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论