NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
icomelook
V2EX  ›  NGINX

nignx 如何批量匹配路径

  •  
  •   icomelook · Mar 7, 2019 · 2168 views
    This topic created in 2626 days ago, the information mentioned may be changed or developed.

    location ~*/(1|2|3...109)$ { proxy_pass http://127.0.0.1; }

    location ~*/(110|111|112...235)$ { proxy_pass http://127.0.0.1; }

    location ~*/(236|237|238...2235)$ { proxy_pass http://127.0.0.1; }

    如何批量匹配路径 总不能几千个文件夹都列出来吧?

    Lax
        1
    Lax  
       Mar 7, 2019
    rewrite_by_lua

    或者没有 lua 模块时,从 location 取个变量出来,用 map。

    map $var $backend_group {
    1 127.0.0.1
    2 127.0.0.2
    ...
    default 127.0.0.3
    }

    location ~ /(?<var>.+)$ {
    proxy_pass http://$backend_group
    }
    icomelook
        2
    icomelook  
    OP
       Mar 7, 2019
    @Lax 不知道 lua 和 map 哪个效率高?
    Lax
        3
    Lax  
       Mar 7, 2019   ❤️ 1
    @icomelook 用 ab 测一下就知道了。

    map 也支持正则,可以 map $uri $backend_group {}, 这样的话 location 匹配里就可以不写正则啦。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   926 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 19:36 · PVG 03:36 · LAX 12:36 · JFK 15:36
    ♥ Do have faith in what you're doing.