【问题标题】:Nginx catch and rewrite all requests begining with /api to proxyNginx 捕获并重写所有以 /api 开头的请求到代理
【发布时间】:2020-01-18 19:56:32
【问题描述】:

我想将所有具有/api/ (http://example.com/api/) 的请求重定向到我的代理应用程序,并在此过程中删除/api。我已经尝试过了,但是在卷曲时我得到了 404。它没有到达应用程序,我认为这与rewrite 有关,但我不确定它是否正确。

location ~ ^/api {
  rewrite ^/api/(.*) /$1 break;
  proxy_pass http://127.0.0.1:8070;
}

以下内容在硬编码时有效。它会将我重定向到我的应用程序/error。

location = /api/error {
  proxy_pass http://127.0.0.1:8070/error;
}

例如,我希望http://example.com/api/login 像http://example.com/login 那样访问我的代理应用程序。

【问题讨论】:

    标签: nginx proxy


    【解决方案1】:

    这对我有用。 $1$is_args$args 部分传递 URL 中 /api 之后的所有内容。现在,当访问我的域时,example.com/api/something 将 /something 传递给我的代理应用程序。

    location ~/api(.*)$ {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header Host $host;
      proxy_pass http://127.0.0.1:8070$1$is_args$args;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多