如何在nginx中转换查询字符串参数?

huangapple go评论90阅读模式
英文:

How do I transform query string parameters in nginx?

问题

如何使用nginx根据某些规则来转换传入的查询字符串参数?具体来说,我想要在每个查询字符串参数值前添加一个前缀。特别是,我想要执行以下操作。

给定这个URI:

  1. http://localhost:3000/account?id=12345&name=bill

我希望将其转换为以下形式:

  1. http://localhost:3000/account?id=eq.12345&name=eq.bill

其中每个项目中的前缀是 eq.。原因是我正在将nginx用作PostgREST的反向代理,而PostgREST使用了类似 eq. 的操作符构建了一个丰富的URL语法,但我需要与不了解该语法的客户一起使用。

我已经研究了nginx的rewritemap模块。后者看起来很有前途,但我还没有弄清楚如何使用它。

我还尝试了一些Lua脚本,试图循环遍历 ngx.req.get_uri_args,但目前我正在调试Lua代码。

  1. events {
  2. }
  3. http {
  4. resolver 127.0.0.11 ipv6=off;
  5. server {
  6. listen 80;
  7. listen [::]:80;
  8. location / {
  9. default_type text/plain;
  10. access_by_lua_block {
  11. local qs = {}
  12. for key, val in pairs(ngx.req.get_uri_args()) do
  13. table.insert(qs, key, "eq." .. val)
  14. end
  15. ngx.req.set_uri_args(qs)
  16. }
  17. proxy_pass http://postgrest:3000;
  18. }
  19. }
  20. }
英文:

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:

  1. http://localhost:3000/account?id=12345&name=bill

I would like it to be such that this is transformed into

  1. 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.

  1. events {
  2. }
  3. http {
  4. resolver 127.0.0.11 ipv6=off;
  5. server {
  6. listen 80;
  7. listen [::]:80;
  8. location / {
  9. default_type text/plain;
  10. access_by_lua_block {
  11. local qs = {}
  12. for key, val in pairs(ngx.req.get_uri_args()) do
  13. table.insert(qs, key, "eq." .. val)
  14. end
  15. ngx.req.set_uri_args(qs)
  16. }
  17. proxy_pass http://postgrest:3000;
  18. }
  19. }
  20. }

答案1

得分: 2

我相信我回答了自己的问题,但我欢迎更多的意见和贡献。以下Lua脚本在nginx.conf中有效:

  1. events {
  2. }
  3. http {
  4. resolver 127.0.0.11 ipv6=off;
  5. server {
  6. listen 80;
  7. listen [::]:80;
  8. location / {
  9. access_by_lua_block {
  10. local qs = {}
  11. for key, val in pairs(ngx.req.get_uri_args()) do
  12. qs[key] = "eq." ..val
  13. end
  14. ngx.req.set_uri_args(qs)
  15. }
  16. proxy_pass http://postgrest:3000;
  17. }
  18. }
  19. }

我很想知道在nginx中是否有Lua脚本的替代方法,但这似乎起到了作用。

英文:

I believe I answered my own question, though I welcome more input and contributions. The following Lua scripting worked in nginx.conf:

  1. events {
  2. }
  3. http {
  4. resolver 127.0.0.11 ipv6=off;
  5. server {
  6. listen 80;
  7. listen [::]:80;
  8. location / {
  9. access_by_lua_block {
  10. local qs = {}
  11. for key, val in pairs(ngx.req.get_uri_args()) do
  12. qs[key] = "eq." ..val
  13. end
  14. ngx.req.set_uri_args(qs)
  15. }
  16. proxy_pass http://postgrest:3000;
  17. }
  18. }
  19. }

I'd love to know if there are alternatives in nginx to Lua scripting, but this seemed to do the trick.

huangapple
  • 本文由 发表于 2023年6月1日 09:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定