【问题标题】:How to Rewrite Internally in NGINX如何在 NGINX 内部重写
【发布时间】:2021-05-24 18:31:23
【问题描述】:

完成一些 basic_auth 检查后,我想将 URL 位置 /private/ping 重写为另一个位置 /ping

当我打电话给http://localhost:8080/ping 时,我没问题。 当我打电话给http://localhost:8080/private/ping 时,我只能使用#3 配置。其他 2 个配置返回 404。

我知道 NGINX 正在成功地将 /private/ping 重写为 /ping,因为 #3 配置返回 OK 但这会产生额外的不必要的 HTTP 请求。

为什么 NGINX 不重新处理它自己的重写检查它们是否与现有位置或我怎样才能做到这一点?

server {

  listen 8080;

  location = /ping {
    return 200 'OK';
  }

  location /private/ {
    # basic_auth stuff here
    #rewrite ^/private/(.*) /$1 break; #1 No
    rewrite ^/private/(.*) /ping break; #2 No
    #proxy_pass http://localhost:8080/; #3 Yes
  }

【问题讨论】:

  • 尝试使用last 而不是break
  • 就是这样!谢谢!请发布答案。

标签: nginx


【解决方案1】:

使用rewrite ... last 根据重写的URI 重新开始搜索另一个location

【讨论】:

    猜你喜欢
    • 2013-12-16
    • 2014-04-25
    • 1970-01-01
    • 2018-01-16
    • 2013-08-21
    • 2011-05-16
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多