【问题标题】:Nginx Reverse Proxy Regex FilterNginx 反向代理正则表达式过滤器
【发布时间】:2020-01-27 00:28:45
【问题描述】:

我正在将 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 语法。这是我现在拥有的:

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】:

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

    proxy_pass http://127.0.0.1:4000/;
    

    没有结尾的/,将传递完整路径:
    http://127.0.0.1:4000/app/something

    使用结尾的/,位置模式/app将替换为/
    http://127.0.0.1:4000/something

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      相关资源
      最近更新 更多