Skip to content

Latest commit

 

History

History
44 lines (30 loc) · 1.3 KB

nginx_re_location_proxypass.md

File metadata and controls

44 lines (30 loc) · 1.3 KB

...menustart

...menuend

Use Regular Expression Location and Proxy Pass

It is an nginx location example, which has an regular expression location , and a proxy-pass directive.

When the location is matched, that url will be rewrited , and pass to local k8s cluster.

upstream dot_cluster {
    server you-k8s-server:10080;
}

server {
    # ~ : regular expression in location
    location ~ ^/(dot)(iap|game) {
        # rewrite target, so that
        #    /dotiap -> /   , iap server ,  works w/ or w/o a trailing slash
        #    /dotiap/ -> /   , iap server
        #    /dotgame/doc -> /doc   , game server
        rewrite ^/(dot)(iap|game)(?:/(.*))? /$3 break;
        # when useing regular expression in location
        #     you must construct proxy_pass url by using some captured re variables
        # actually same stream , here it is dot_cluster
        proxy_pass  http://$1_cluster ;

        # host header, used by k8s ingress to dispatch traffic
        proxy_set_header            Host "dot-$2-dev.imac";
        proxy_set_header            X-real-ip $remote_addr;
        proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}