Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

location ~* "(?<begin>rplocal|rposs)/([a-z0-9]{3})/([a-z0-9]{8})/(.*)$" {
    rewrite "(?<begin>rplocal|rposs)/([a-z0-9]{3})/([a-z0-9]{8})/(.*)$" /$2/$3/$4 break;
    if ($begin = 'rplocal') {
        set $proxy http://127.0.0.1:9222;
    }
    if ($begin = 'rposs') {
        set $proxy https://url-domain.com;
    }
    proxy_pass $proxy;
}

目的是通过 url 反向到不同的地址

这里补充一个可以通过的方案, 但是此方案还有冗余 rewrite, 是使用 openresty + echo 调试出来的

location ~* "(rplocal|rposs)/([a-z0-9]{3})/([a-z0-9]{8})/(.*)$" {
    set $type $1;
    if ($type = "rplocal") {
        rewrite "^/([a-z-]*)/(w{3})(w{5})/(.*)$" /$2/$3/$4 break;
        proxy_pass http://127.0.0.1:9222;
    }
    if ($type = "rposs") {
        rewrite "^/([a-z-]*)/(w{3})(w{5})/(.*)$" /$2/$3/$4 break;
        proxy_pass https://url-domain.com;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

这种情况,我都拆成两个 location 来写

location ~ /rplocal/ {
    proxy_pass http://127.0.0.1:9222;
}
location ~ /rposs/ {
    proxy_pass https://url-domain.com;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...