首页 > 解决方案 > NGINX - 使用 sub_filter 更改代理传递位置块中的路径

问题描述

因此,我有一个 NGNIX 服务器,它从同一位置 (Akamai) 接收流量,并根据传入 URL 的路径将流量发送到不同的应用程序。

我已向 Akamai 添加了一个新来源,这意味着一些传入请求现在有了新路径。问题是我的应用程序需要路径是某个值。

当我与其他来源共享 Akamai 插槽时,我无法将具有相同路径的请求发送到两个不同的来源,因为插槽对将流量引导至哪个源服务器感到困惑。

所以我想做的是在将路径传递给应用程序之前更改路径。我不确定执行此操作的最佳方法,需要一些帮助。我应该使用重写、重定向还是 sub_filter?

我实际上已经尝试了所有这三个,但我在这个非常简单的任务中遗漏了一些东西。

location /incoming_path {
      max_ranges 0;
      proxy_set_header Accept-Encoding "";
      proxy_pass https://\$upstream_application:9002;
      proxy_ssl_server_name on;
      proxy_ssl_certificate     /etc/nginx/conf.d/server_cert.pem;
      proxy_ssl_certificate_key /etc/nginx/conf.d/server_key.pem;
      proxy_set_header Host               \$host;
      proxy_set_header X-Real-IP          \$proxy_protocol_addr;
      sub_filter_types *;
      sub_filter "https://\$proxy_host/incoming_path" "https://\$host/new_path"
      sub_filter_once on;
    }

真的很感激任何关于如何实现这一目标的想法/想法,在此先感谢。

标签: nginxnginx-reverse-proxynginx-confignginx-location

解决方案


这看起来奏效了:

location /incoming_path {
      max_ranges 0;
      rewrite /incoming_path/(.*) /new_path/$1  break;
      proxy_pass https://\$upstream_application:9002;
      proxy_ssl_server_name on;
      proxy_ssl_certificate     /etc/nginx/conf.d/server_cert.pem;
      proxy_ssl_certificate_key /etc/nginx/conf.d/server_key.pem;
      proxy_set_header Host               \$host;
      proxy_set_header X-Real-IP          \$proxy_protocol_addr;
    }

推荐阅读