9阅网

您现在的位置是:首页 > 知识 > 正文

知识

nginx - nginx位置 常规速记

admin2022-11-05知识26

我有很多关于位置的代码,很复杂,很困难

        location /addressJson/0{
          proxy_pass http://wangzc.wang:1337/0;
        }
        location /addressJson/1{
          proxy_pass http://wangzc.wang:1337/1;
        }
        location /addressJson/2{
          proxy_pass http://wangzc.wang:1337/2;
        }
        location /addressJson/3{
          proxy_pass http://wangzc.wang:1337/3;
        }
          location /addressJson/4{
          proxy_pass http://wangzc.wang:1337/4;
        }
          location /addressJson/5{
          proxy_pass http://wangzc.wang:1337/5;
        }
         location /addressJson/6{
          proxy_pass http://wangzc.wang:1337/6;
        }
          location /addressJson/7{
          proxy_pass http://wangzc.wang:1337/7;
        }

如何缩写?



【回答】:

你不需要使用 正则表达式 location 来匹配任何以相同文本开头的URI。使用前缀 location. 该 location 匹配您问题中所有位置的是。

 location /addressJson/ { ... }

可选的 尿素 一部分 proxy_pass 值执行一个别名功能,它将代替原来的 前缀 (在前缀 location)与 尿素 在内 proxy_pass 值。见 本文件 以了解详情。

比如说

location /addressJson/ { 
    proxy_pass http://example.com:1337/;
}

如果原始请求的URI是 /addressJson/foo 那么 /addressJson/ 的部分被移除,而 / 在发送修改后的URL之前,将其作为 http://example.com:1337/foo.

注意 location 值应以 / 为了使文字替换工作正常进行。