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

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

大家帮忙看下, 这个 nginx 的 rewrite 规则的正确写法是什么, 关于反向代理的

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;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...