ingress-nginx-helm/user-guide/ingress-path-matching/index.html

96 lines
33 KiB
HTML
Raw Normal View History

2021-07-29 21:24:20 +00:00
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><link href=https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/ rel=canonical><link rel="shortcut icon" href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.1.2, mkdocs-material-6.2.4"><title>Regular expressions in paths - NGINX Ingress Controller</title><link rel=stylesheet href=../../assets/stylesheets/main.15aa0b43.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.75751829.min.css><meta name=theme-color content=#009485><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:300,400,400i,700%7CRoboto+Mono&display=fallback"><style>body,input{font-family:"Roboto",-apple-system,BlinkMacSystemFont,Helvetica,Arial,sans-serif}code,kbd,pre{font-family:"Roboto Mono",SFMono-Regular,Consolas,Menlo,monospace}</style><link rel=stylesheet href=../../extra.css><script>window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)},ga.l=+new Date,ga("create","UA-118407822-1","kubernetes.github.io"),ga("set","anonymizeIp",!0),ga("send","pageview"),document.addEventListener("DOMContentLoaded",function(){document.forms.search&&document.forms.search.query.addEventListener("blur",function(){if(this.value){var e=document.location.pathname;ga("send","pageview",e+"?q="+this.value)}})}),document.addEventListener("DOMContentSwitch",function(){ga("send","pageview",document.location.pathname)})</script><script async src=https://www.google-analytics.com/analytics.js></script></head> <body dir=ltr data-md-color-scheme data-md-color-primary=teal data-md-color-accent=green> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#ingress-path-matching class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header-nav md-grid" aria-label=Header> <a href=https://kubernetes.github.io/ingress-nginx title="NGINX Ingress Controller" class="md-header-nav__button md-logo" aria-label="NGINX Ingress Controller"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 003-3 3 3 0 00-3-3 3 3 0 00-3 3 3 3 0 003 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54z"/></svg> </a> <label class="md-header-nav__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg> </label> <div class=md-header-nav__title data-md-component=header-title> <div class=md-header-nav__ellipsis> <div class=md-header-nav__topic> <span class=md-ellipsis> NGINX Ingress Controller </span> </div> <div class=md-header-nav__topic> <span class=md-ellipsis> Regular expressions in paths </span> </div> </div> </div> <label class="md-header-nav__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0116 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 019.5 16 6.5 6.5 0 013 9.5 6.5 6.5 0 019.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg> </label> <div class=md-search data-md-component=search role=dialog> <label class=md-search__overlay for=__search></label> <div class=md-search__inner role=search> <form class=md-search__form name=search> <input type=text class=md-search__input name=query aria-label=Search placeholder=Search autocapitalize=off autocorrect=off autocomplete=off spellcheck=false data-md-component=search-query data-md-state=active required> <label class="md-search__icon md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0116 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l
2021-01-11 15:59:14 +00:00
<span class=nt>kind</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">Ingress</span>
<span class=nt>metadata</span><span class=p>:</span>
<span class=nt>name</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test-ingress</span>
<span class=nt>annotations</span><span class=p>:</span>
<span class=nt>nginx.ingress.kubernetes.io/use-regex</span><span class=p>:</span> <span class=s>&quot;true&quot;</span>
<span class=nt>spec</span><span class=p>:</span>
<span class=nt>rules</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>host</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test.com</span>
<span class=nt>http</span><span class=p>:</span>
<span class=nt>paths</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>path</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/foo/.*</span>
<span class=nt>backend</span><span class=p>:</span>
<span class=nt>serviceName</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test</span>
<span class=nt>servicePort</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">80</span>
</code></pre></div> <p>The preceding ingress definition would translate to the following location block within the NGINX configuration for the <code>test.com</code> server:</p> <div class=highlight><pre><span></span><code>location ~* &quot;^/foo/.*&quot; {
2020-04-15 17:09:38 +00:00
...
}
2021-07-29 21:24:20 +00:00
</code></pre></div> <h2 id=path-priority>Path Priority<a class=headerlink href=#path-priority title="Permanent link"></a></h2> <p>In NGINX, regular expressions follow a <strong>first match</strong> policy. In order to enable more accurate path matching, ingress-nginx first orders the paths by descending length before writing them to the NGINX template as location blocks.</p> <p><strong>Please read the <a href=#warning>warning</a> before using regular expressions in your ingress definitions.</strong></p> <h3 id=example>Example<a class=headerlink href=#example title="Permanent link"></a></h3> <p>Let the following two ingress definitions be created:</p> <div class=highlight><pre><span></span><code><span class=nt>apiVersion</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">networking.k8s.io/v1beta1</span>
2021-01-11 15:59:14 +00:00
<span class=nt>kind</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">Ingress</span>
<span class=nt>metadata</span><span class=p>:</span>
<span class=nt>name</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test-ingress-1</span>
<span class=nt>spec</span><span class=p>:</span>
<span class=nt>rules</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>host</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test.com</span>
<span class=nt>http</span><span class=p>:</span>
<span class=nt>paths</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>path</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/foo/bar</span>
<span class=nt>backend</span><span class=p>:</span>
<span class=nt>serviceName</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">service1</span>
<span class=nt>servicePort</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">80</span>
<span class="p p-Indicator">-</span> <span class=nt>path</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/foo/bar/</span>
<span class=nt>backend</span><span class=p>:</span>
<span class=nt>serviceName</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">service2</span>
<span class=nt>servicePort</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">80</span>
2021-07-29 21:24:20 +00:00
</code></pre></div> <div class=highlight><pre><span></span><code><span class=nt>apiVersion</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">networking.k8s.io/v1beta1</span>
2021-01-11 15:59:14 +00:00
<span class=nt>kind</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">Ingress</span>
<span class=nt>metadata</span><span class=p>:</span>
<span class=nt>name</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test-ingress-2</span>
<span class=nt>annotations</span><span class=p>:</span>
<span class=nt>nginx.ingress.kubernetes.io/rewrite-target</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/$1</span>
<span class=nt>spec</span><span class=p>:</span>
<span class=nt>rules</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>host</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test.com</span>
<span class=nt>http</span><span class=p>:</span>
<span class=nt>paths</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>path</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/foo/bar/(.+)</span>
<span class=nt>backend</span><span class=p>:</span>
<span class=nt>serviceName</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">service3</span>
<span class=nt>servicePort</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">80</span>
</code></pre></div> <p>The ingress controller would define the following location blocks, in order of descending length, within the NGINX template for the <code>test.com</code> server:</p> <div class=highlight><pre><span></span><code>location ~* ^/foo/bar/.+ {
2020-04-15 17:09:38 +00:00
...
}
2018-10-01 19:44:22 +00:00
2020-04-15 17:09:38 +00:00
location ~* &quot;^/foo/bar/&quot; {
...
}
2018-10-01 19:44:22 +00:00
2020-04-15 17:09:38 +00:00
location ~* &quot;^/foo/bar&quot; {
...
}
2021-07-29 21:24:20 +00:00
</code></pre></div> <p>The following request URI's would match the corresponding location blocks:</p> <ul> <li><code>test.com/foo/bar/1</code> matches <code>~* ^/foo/bar/.+</code> and will go to service 3.</li> <li><code>test.com/foo/bar/</code> matches <code>~* ^/foo/bar/</code> and will go to service 2.</li> <li><code>test.com/foo/bar</code> matches <code>~* ^/foo/bar</code> and will go to service 1.</li> </ul> <p><strong>IMPORTANT NOTES</strong>:</p> <ul> <li>If the <code>use-regex</code> OR <code>rewrite-target</code> annotation is used on any Ingress for a given host, then the case insensitive regular expression <a href=https://nginx.org/en/docs/http/ngx_http_core_module.html#location>location modifier</a> will be enforced on ALL paths for a given host regardless of what Ingress they are defined on.</li> </ul> <h2 id=warning>Warning<a class=headerlink href=#warning title="Permanent link"></a></h2> <p>The following example describes a case that may inflict unwanted path matching behavior.</p> <p>This case is expected and a result of NGINX's a first match policy for paths that use the regular expression <a href=https://nginx.org/en/docs/http/ngx_http_core_module.html#location>location modifier</a>. For more information about how a path is chosen, please read the following article: <a href=https://www.digitalocean.com/community/tutorials/understanding-nginx-server-and-location-block-selection-algorithms>"Understanding Nginx Server and Location Block Selection Algorithms"</a>.</p> <h3 id=example_1>Example<a class=headerlink href=#example_1 title="Permanent link"></a></h3> <p>Let the following ingress be defined:</p> <div class=highlight><pre><span></span><code><span class=nt>apiVersion</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">networking.k8s.io/v1beta1</span>
2021-01-11 15:59:14 +00:00
<span class=nt>kind</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">Ingress</span>
<span class=nt>metadata</span><span class=p>:</span>
<span class=nt>name</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test-ingress-3</span>
<span class=nt>annotations</span><span class=p>:</span>
<span class=nt>nginx.ingress.kubernetes.io/use-regex</span><span class=p>:</span> <span class=s>&quot;true&quot;</span>
<span class=nt>spec</span><span class=p>:</span>
<span class=nt>rules</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>host</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test.com</span>
<span class=nt>http</span><span class=p>:</span>
<span class=nt>paths</span><span class=p>:</span>
<span class="p p-Indicator">-</span> <span class=nt>path</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/foo/bar/bar</span>
<span class=nt>backend</span><span class=p>:</span>
<span class=nt>serviceName</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test</span>
<span class=nt>servicePort</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">80</span>
<span class="p p-Indicator">-</span> <span class=nt>path</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">/foo/bar/[A-Z0-9]{3}</span>
<span class=nt>backend</span><span class=p>:</span>
<span class=nt>serviceName</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">test</span>
<span class=nt>servicePort</span><span class=p>:</span> <span class="l l-Scalar l-Scalar-Plain">80</span>
</code></pre></div> <p>The ingress controller would define the following location blocks (in this order) within the NGINX template for the <code>test.com</code> server:</p> <div class=highlight><pre><span></span><code>location ~* &quot;^/foo/bar/[A-Z0-9]{3}&quot; {
2020-04-15 17:09:38 +00:00
...
}
2018-10-01 19:44:22 +00:00
2020-04-15 17:09:38 +00:00
location ~* &quot;^/foo/bar/bar&quot; {
...
}
2021-01-11 15:59:14 +00:00
</code></pre></div> <p>A request to <code>test.com/foo/bar/bar</code> would match the <code>^/foo/bar/[A-Z0-9]{3}</code> location block instead of the longest EXACT matching path.</p> </article> </div> </div> </main> <footer class=md-footer> <div class=md-footer-nav> <nav class="md-footer-nav__inner md-grid" aria-label=Footer> <a href=../fcgi-services/ class="md-footer-nav__link md-footer-nav__link--prev" rel=prev> <div class="md-footer-nav__button md-icon"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg> </div> <div class=md-footer-nav__title> <div class=md-ellipsis> <span class=md-footer-nav__direction> Previous </span> Exposing FCGI services </div> </div> </a> <a href=../external-articles/ class="md-footer-nav__link md-footer-nav__link--next" rel=next> <div class=md-footer-nav__title> <div class=md-ellipsis> <span class=md-footer-nav__direction> Next </span> External Articles </div> </div> <div class="md-footer-nav__button md-icon"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M4 11v2h12l-5.5 5.5 1.42 1.42L19.84 12l-7.92-7.92L10.5 5.5 16 11H4z"/></svg> </div> </a> </nav> </div> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-footer-copyright> Made with <a href=https://squidfunk.github.io/mkdocs-material/ target=_blank rel=noopener> Material for MkDocs </a> </div> </div> </div> </footer> </div> <script src=../../assets/javascripts/vendor.93c04032.min.js></script> <script src=../../assets/javascripts/bundle.83e5331e.min.js></script><script id=__lang type=application/json>{"clipboard.copy": "Copy to clipboard", "clipboard.copied": "Copied to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.placeholder": "Type to start searching", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.term.missing": "Missing"}</script> <script>
2020-04-15 17:09:38 +00:00
app = initialize({
base: "../..",
2021-01-11 15:59:14 +00:00
features: ['navigation.tabs', 'navigation.tabs.sticky', 'navigation.instant', 'navigation.sections'],
2020-04-15 17:09:38 +00:00
search: Object.assign({
2021-01-11 15:59:14 +00:00
worker: "../../assets/javascripts/worker/search.8c7e0a7e.min.js"
2020-04-15 17:09:38 +00:00
}, typeof search !== "undefined" && search)
})
2021-01-11 15:59:14 +00:00
</script> </body> </html>