Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...