前端-nginx.conf文件中proxy_pass变量值的结尾有无斜杠的区别

server {
        listen 8080;
        server_name localhost;

        location ^~/mgrcontrol/{
            proxy_pass  '$MGR_SERVICE';
        }
        
    }

在Nginx配置文件中,proxy_pass 指令用于将请求代理到指定的后端服务。在配置中,proxy_pass 后面使用了变量 $MGR_SERVICE,而这个变量的值是后端服务的地址。

影响的主要点是关于路径的处理。具体而言,是关于location块中的路径和proxy_pass中的路径的组合。

  1. 无斜杠结尾(例如 http://backend):

    • location ^~/mgrcontrol/ 匹配的是以 /mgrcontrol/ 开头的请求。
    • proxy_pass '$MGR_SERVICE'; 中的 $MGR_SERVICE 是一个不以斜杠结尾的URL,例如 http://backend
    • 当请求为 /mgrcontrol/somepath 时,Nginx会将请求代理到 $MGR_SERVICE 后面追加 /mgrcontrol/somepath,即最终请求的后端地址是 http://backend/mgrcontrol/somepath
  2. 有斜杠结尾(例如 http://backend/):

    • location ^~/mgrcontrol/ 依然匹配以 /mgrcontrol/ 开头的请求。
    • proxy_pass '$MGR_SERVICE'; 中的 $MGR_SERVICE 是一个以斜杠结尾的URL,例如 http://backend/
    • 当请求为 /mgrcontrol/somepath 时,Nginx会将请求代理到 $MGR_SERVICE 后面追加 somepath,即最终请求的后端地址是 http://backend/somepath

所以,主要取决于$MGR_SERVICE 的结尾是否有斜杠,以及location块中的路径是否以斜杠结尾。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-27 14:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-27 14:40:02       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-27 14:40:02       82 阅读
  4. Python语言-面向对象

    2023-12-27 14:40:02       91 阅读

热门阅读

  1. 正则表达式:断言

    2023-12-27 14:40:02       72 阅读
  2. 嵌入式硬件电路学习之阻抗

    2023-12-27 14:40:02       67 阅读
  3. 安装electron项目报错问题

    2023-12-27 14:40:02       43 阅读