ingress-nginx-helm/user-guide/fcgi-services/index.html

98 lines
40 KiB
HTML
Raw Permalink Normal View History

2024-09-15 15:04:08 +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/fcgi-services/ rel=canonical><link href=../exposing-tcp-udp-services/ rel=prev><link href=../ingress-path-matching/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.5.3, mkdocs-material-9.4.5"><title>Exposing FCGI services - Ingress-Nginx Controller</title><link rel=stylesheet href=../../assets/stylesheets/main.6a10b989.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.356b1318.min.css><link rel=preconnect href=https://fonts.gstatic.com crossorigin><link rel=stylesheet href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback"><style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style><link rel=stylesheet href=../../extra.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce((e,_)=>(e<<5)-e+_.charCodeAt(0),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default 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=#exposing-fastcgi-servers class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class="md-header md-header--shadow md-header--lifted" data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="Ingress-Nginx Controller" class="md-header__button md-logo" aria-label="Ingress-Nginx Controller" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 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__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__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> Ingress-Nginx Controller </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> Exposing FCGI services </span> </div> </div> </div> <label class="md-header__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 0 1 16 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 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.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 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 0 1 16 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 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5Z"/></svg> <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> </label> <nav class=md-search__options aria-label=Sea
2023-10-12 19:31:18 +00:00
<span class=nt>kind</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">Pod</span>
2023-06-13 19:57:05 +00:00
<span class=nt>metadata</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-app</span>
2024-07-12 07:47:42 +00:00
<span class=w> </span><span class=nt>labels</span><span class=p>:</span>
<span class=w> </span><span class=nt>app</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-app</span>
2023-06-13 19:57:05 +00:00
<span class=nt>spec</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>containers</span><span class=p>:</span>
<span class=w> </span><span class="p p-Indicator">-</span><span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-app</span>
2024-06-12 07:39:40 +00:00
<span class=w> </span><span class=nt>image</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">php:fpm-alpine</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>ports</span><span class=p>:</span>
<span class=w> </span><span class="p p-Indicator">-</span><span class=w> </span><span class=nt>containerPort</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">9000</span>
<span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">fastcgi</span>
2024-06-12 07:39:40 +00:00
</code></pre></div> <ul> <li>For this example to work, a HTML response should be received from the FastCGI server being exposed</li> <li>A HTTP request to the FastCGI server pod should be sent</li> <li>The response should be generated by a php script as that is what we are demonstrating here</li> </ul> <p>The image we are using here <code>php:fpm-alpine</code> does not ship with a ready to use php script inside it. So we need to provide the image with a simple php-script for this example to work.</p> <ul> <li>Use <code>kubectl exec</code> to get into the example-app pod</li> <li>You will land at the path <code>/var/www/html</code></li> <li>Create a simple php script there at the path /var/www/html called index.php</li> <li>Make the index.php file look like this</li> </ul> <div class=highlight><pre><span></span><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php echo &#39;&lt;p&gt;FastCGI Test Worked!&lt;/p&gt;&#39;; ?&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre></div> <ul> <li>Save and exit from the shell in the pod</li> <li>If you delete the pod, then you will have to recreate the file as this method is not persistent</li> </ul> <h3 id=the-fastcgi-service>The FastCGI service<a class=headerlink href=#the-fastcgi-service title="Permanent link"></a></h3> <p>The <em>Service</em> object example below matches port <code>9000</code> from the <em>Pod</em> object above.</p> <div class=highlight><pre><span></span><code><span class=nt>apiVersion</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">v1</span>
2023-10-12 19:31:18 +00:00
<span class=nt>kind</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">Service</span>
2023-06-13 19:57:05 +00:00
<span class=nt>metadata</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-service</span>
2023-06-13 19:57:05 +00:00
<span class=nt>spec</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>selector</span><span class=p>:</span>
<span class=w> </span><span class=nt>app</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-app</span>
<span class=w> </span><span class=nt>ports</span><span class=p>:</span>
<span class=w> </span><span class="p p-Indicator">-</span><span class=w> </span><span class=nt>port</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">9000</span>
<span class=w> </span><span class=nt>targetPort</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">9000</span>
<span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">fastcgi</span>
2024-06-12 07:39:40 +00:00
</code></pre></div> <h3 id=the-configmap-object-and-the-ingress-object>The configMap object and the ingress object<a class=headerlink href=#the-configmap-object-and-the-ingress-object title="Permanent link"></a></h3> <p>The <em>Ingress</em> and <em>ConfigMap</em> objects below demonstrate the supported <em>FastCGI</em> specific annotations.</p> <div class="admonition important"> <p class=admonition-title>Important</p> <p>NGINX actually has 50 <a href=https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#directives>FastCGI directives</a> All of the nginx directives have not been exposed in the ingress yet</p> </div> <h3 id=the-configmap-object>The ConfigMap object<a class=headerlink href=#the-configmap-object title="Permanent link"></a></h3> <p>This configMap object is required to set the parameters of <a href=https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#directives>FastCGI directives</a></p> <div class="admonition attention"> <p class=admonition-title>Attention</p> <ul> <li>The <em>ConfigMap</em> <strong>must</strong> be created before creating the ingress object</li> </ul> </div> <ul> <li>The <em>Ingress Controller</em> needs to find the configMap when the <em>Ingress</em> object with the FastCGI annotations is created</li> <li>So create the configMap before the ingress</li> <li>If the configMap is created after the ingress is created, then you will need to restart the <em>Ingress Controller</em> pods.</li> </ul> <div class=highlight><pre><span></span><code><span class=nt>apiVersion</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">v1</span>
2023-10-12 19:31:18 +00:00
<span class=nt>kind</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">ConfigMap</span>
2023-06-13 19:57:05 +00:00
<span class=nt>metadata</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-cm</span>
2023-06-13 19:57:05 +00:00
<span class=nt>data</span><span class=p>:</span>
2024-06-12 07:39:40 +00:00
<span class=w> </span><span class=nt>SCRIPT_FILENAME</span><span class=p>:</span><span class=w> </span><span class=s>&quot;/var/www/html/index.php&quot;</span>
</code></pre></div> <h3 id=the-ingress-object>The ingress object<a class=headerlink href=#the-ingress-object title="Permanent link"></a></h3> <ul> <li>Do not create the ingress shown below until you have created the configMap seen above.</li> <li>You can see that this ingress matches the service <code>example-service</code>, and the port named <code>fastcgi</code> from above.</li> </ul> <div class=highlight><pre><span></span><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/backend-protocol: &quot;FCGI&quot;
nginx.ingress.kubernetes.io/fastcgi-index: &quot;index.php&quot;
nginx.ingress.kubernetes.io/fastcgi-params-configmap: &quot;example-cm&quot;
name: example-app
spec:
ingressClassName: nginx
rules:
- host: app.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: example-service
port:
name: fastcgi
</code></pre></div> <h2 id=send-a-request-to-the-exposed-fastcgi-server>Send a request to the exposed FastCGI server<a class=headerlink href=#send-a-request-to-the-exposed-fastcgi-server title="Permanent link"></a></h2> <p>You will have to look at the external-ip of the ingress or you have to send the HTTP request to the ClusterIP address of the ingress-nginx controller pod.</p> <div class=highlight><pre><span></span><code>% curl 172.19.0.2 -H &quot;Host: app.example.com&quot; -vik
* Trying 172.19.0.2:80...
* Connected to 172.19.0.2 (172.19.0.2) port 80
&gt; GET / HTTP/1.1
&gt; Host: app.example.com
&gt; User-Agent: curl/8.6.0
&gt; Accept: */*
&gt;
&lt; HTTP/1.1 200 OK
HTTP/1.1 200 OK
&lt; Date: Wed, 12 Jun 2024 07:11:59 GMT
Date: Wed, 12 Jun 2024 07:11:59 GMT
&lt; Content-Type: text/html; charset=UTF-8
Content-Type: text/html; charset=UTF-8
&lt; Transfer-Encoding: chunked
Transfer-Encoding: chunked
&lt; Connection: keep-alive
Connection: keep-alive
&lt; X-Powered-By: PHP/8.3.8
X-Powered-By: PHP/8.3.8
2023-06-13 19:57:05 +00:00
2024-06-12 07:39:40 +00:00
&lt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;PHP Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;FastCGI Test Worked&lt;/p&gt; &lt;/body&gt;
&lt;/html&gt;
</code></pre></div> <h2 id=fastcgi-ingress-annotations>FastCGI Ingress Annotations<a class=headerlink href=#fastcgi-ingress-annotations title="Permanent link"></a></h2> <p>To enable FastCGI, the <code>nginx.ingress.kubernetes.io/backend-protocol</code> annotation needs to be set to <code>FCGI</code>, which overrides the default <code>HTTP</code> value.</p> <blockquote> <p><code>nginx.ingress.kubernetes.io/backend-protocol: "FCGI"</code></p> </blockquote> <p><strong>This enables the <em>FastCGI</em> mode for all paths defined in the <em>Ingress</em> object</strong></p> <h3 id=the-nginxingresskubernetesiofastcgi-index-annotation>The <code>nginx.ingress.kubernetes.io/fastcgi-index</code> Annotation<a class=headerlink href=#the-nginxingresskubernetesiofastcgi-index-annotation title="Permanent link"></a></h3> <p>To specify an index file, the <code>fastcgi-index</code> annotation value can optionally be set. In the example below, the value is set to <code>index.php</code>. This annotation corresponds to <a href=https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_index>the <em>NGINX</em> <code>fastcgi_index</code> directive</a>.</p> <blockquote> <p><code>nginx.ingress.kubernetes.io/fastcgi-index: "index.php"</code></p> </blockquote> <h3 id=the-nginxingresskubernetesiofastcgi-params-configmap-annotation>The <code>nginx.ingress.kubernetes.io/fastcgi-params-configmap</code> Annotation<a class=headerlink href=#the-nginxingresskubernetesiofastcgi-params-configmap-annotation title="Permanent link"></a></h3> <p>To specify <a href=https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_param><em>NGINX</em> <code>fastcgi_param</code> directives</a>, the <code>fastcgi-params-configmap</code> annotation is used, which in turn must lead to a <em>ConfigMap</em> object containing the <em>NGINX</em> <code>fastcgi_param</code> directives as key/values.</p> <blockquote> <p><code>nginx.ingress.kubernetes.io/fastcgi-params-configmap: "example-configmap"</code></p> </blockquote> <p>And the <em>ConfigMap</em> object to specify the <code>SCRIPT_FILENAME</code> and <code>HTTP_PROXY</code> <em>NGINX's</em> <code>fastcgi_param</code> directives will look like the following:</p> <div class=highlight><pre><span></span><code><span class=nt>apiVersion</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">v1</span>
2023-10-12 19:31:18 +00:00
<span class=nt>kind</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">ConfigMap</span>
2023-06-13 19:57:05 +00:00
<span class=nt>metadata</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>name</span><span class=p>:</span><span class=w> </span><span class="l l-Scalar l-Scalar-Plain">example-configmap</span>
2023-06-13 19:57:05 +00:00
<span class=nt>data</span><span class=p>:</span>
2023-10-12 19:31:18 +00:00
<span class=w> </span><span class=nt>SCRIPT_FILENAME</span><span class=p>:</span><span class=w> </span><span class=s>&quot;/example/index.php&quot;</span>
<span class=w> </span><span class=nt>HTTP_PROXY</span><span class=p>:</span><span class=w> </span><span class=s>&quot;&quot;</span>
2024-06-12 07:39:40 +00:00
</code></pre></div> <p>Using the <em>namespace/</em> prefix is also supported, for example:</p> <blockquote> <p><code>nginx.ingress.kubernetes.io/fastcgi-params-configmap: "example-namespace/example-configmap"</code></p> </blockquote> </article> </div> </div> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> Made with <a href=https://squidfunk.github.io/mkdocs-material/ target=_blank rel=noopener> Material for MkDocs </a> </div> </div> </div> </footer> </div> <div class=md-dialog data-md-component=dialog> <div class="md-dialog__inner md-typeset"></div> </div> <script id=__config type=application/json>{"base": "../..", "features": ["navigation.tabs", "navigation.tabs.sticky", "navigation.instant", "navigation.sections"], "search": "../../assets/javascripts/workers/search.f886a092.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version": "Select version"}}</script> <script src=../../assets/javascripts/bundle.aecac24b.min.js></script> </body> </html>