Nginx反向代理正则表达式过滤器

问题描述 投票:0回答:1

我正在将NodeJS反向代理从Apache迁移到Nginx。最后要解决的问题是如何为NodeJS后端过滤传入的URI。

由于种种遗留原因,NodeJS API会响应example.com/route/1001之类的URI,而Apache前端和所有客户端代码实际上正在发送至example.com/api/route/1001。因此,我希望剥离请求URI的/ api部分,仅将后半部分发送到Node API。

[我在Regxr上玩耍,试图把它弄对,并以(?!api)\b\S+做得很好:regexr.com/4lp4e

但是我不熟悉如何将Nginx放入变量中的Nginx语法。这是我现在所拥有的:

server {
  server_name <domain>;

  access_log  /var/log/nginx/<domain>-access.log;

  # API endpoint -  (?!api)\b\S+
  location /api {
    if ($request_uri ~* ((?!api)\b\S+) ) {
      set  $last $1;
    }
    add_header X-uri "$last";
    rewrite /api /$last break;
    #proxy_redirect off;
    proxy_pass http://127.0.0.1:4000;
  }

  listen 80;
  listen [::]:80;
}
node.js regex nginx reverse-proxy
1个回答
0
投票

尝试在代理通行证的末尾添加尾随/

proxy_pass http://127.0.0.1:4000/;

Without末尾/,将传递完整路径:http://127.0.0.1:4000/app/something

With尾随的/,位置模式/app将替换为/http://127.0.0.1:4000/something

© www.soinside.com 2019 - 2024. All rights reserved.