<p>I have an issue, I am deploying an application on [hostname]/product/console, but the .css .js files are being requested from [hostname]/product/static, hence they are not being loaded and I get 404.</p>

<p>I have tried <code>nginx.ingress.kubernetes.io/rewrite-target:</code> to no avail.</p>

<p>I also tried using: <code>nginx.ingress.kubernetes.io/location-snippet: |
                 location = /product/console/ {
                   proxy_pass http://[hostname]/product/static/;
                 }</code></p>

<p>But the latter does not seem to be picked up by the nginx controller at all. This is my ingress.yaml</p>

<pre><code>---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-resource
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/enable-rewrite-log: "true"
    # nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/location-snippet: |
      location = /product/console/ {
        proxy_pass http://[hostname]/product/static/;
        }
spec:
  rules:
    - host: {{.Values.HOSTNAME}}
      http:
        paths:
        - path: /product/console
          backend:
            serviceName: product-svc
            servicePort: prod ##25022
        - path: /product/
          backend:
            serviceName: product-svc
            servicePort: prod #25022
</code></pre>

<p>--
Can I ask for some pointers? I have been trying to google this out and tried some different variations, but I seem to be doing something wrong. Thanks!</p>

voidcraft
<p><strong>TL;DR</strong></p>

<p>To diagnose the reason why you get error 404 you can check in <code>nginx-ingress</code> controller pod logs. You can do it with below command: </p>

<p><code>kubectl logs -n ingress-nginx INGRESS_NGINX_CONTROLLER_POD_NAME</code></p>

<p>You should get output similar to this (depending on your use case): </p>

<pre class="lang-sh prettyprint-override"><code>CLIENT_IP - - [12/May/2020:11:06:56 +0000] "GET / HTTP/1.1" 200 238 "-" "REDACTED" 430 0.003 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 276 0.003 200 
CLIENT_IP - - [12/May/2020:11:06:56  +0000] "GET /assets/styles/style.css HTTP/1.1" 200 22 "http://SERVER_IP/" "REDACTED" 348 0.002 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 22 0.002 200 
</code></pre>

<p>With above logs you can check if the requests are handled properly by <code>nginx-ingress</code> controller and where they are sent. </p>

<p>Also you can check the <a href="https://kubernetes.github.io/ingress-nginx/user-guide/ingress-path-matching/" rel="noreferrer">Kubernetes.github.io: ingress-nginx: Ingress-path-matching</a>. It's a document describing how <code>Ingress</code> matches paths with regular expressions. </p>

<hr>

<p>You can experiment with <code>Ingress</code>, by following below example: </p>

<ul>
<li>Deploy <code>nginx-ingress</code> controller</li>
<li>Create a <code>pod</code> and a <code>service</code></li>
<li>Run example application </li>
<li>Create an <code>Ingress</code> resource </li>
<li>Test</li>
<li>Rewrite example </li>
</ul>

<h3>Deploy <code>nginx-ingress</code> controller</h3>

<p>You can deploy your <code>nginx-ingress</code> controller by following official documentation: </p>

<p><a href="https://kubernetes.github.io/ingress-nginx/deploy/" rel="noreferrer">Kubernetes.github.io: Ingress-nginx</a></p>

<h3>Create a <code>pod</code> and a <code>service</code></h3>

<p>Below is an example definition of a pod and a service attached to it which will be used for testing purposes: </p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: ubuntu-deployment
spec:
  selector:
    matchLabels:
      app: ubuntu
  replicas: 1 
  template:
    metadata:
      labels:
        app: ubuntu
    spec:
      containers:
      - name: ubuntu
        image: ubuntu
        command:
        - sleep
        - "infinity" 
---
apiVersion: v1
kind: Service
metadata:
  name: ubuntu-service
spec:
  selector:
    app: ubuntu
  ports:
    - name: ubuntu-port
      port: 8080
      targetPort: 8080
      nodePort: 30080
  type: NodePort 
</code></pre>

<h3>Example page</h3>

<p>I created a basic <code>index.html</code> with one <code>css</code> to simulate the request process. You need to create this files inside of a pod (manually or copy them to pod). </p>

<p>The file tree looks like this: </p>

<ul>
<li><strong>index.html</strong></li>
<li>assets/styles/<strong>style.css</strong></li>
</ul>

<p><strong>index.html</strong>: </p>

<pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
  &lt;meta charset="UTF-8"&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  &lt;link rel="stylesheet" href="assets/styles/style.css"&gt;
  &lt;title&gt;Document&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
  &lt;h1&gt;Hi&lt;/h1&gt;
&lt;/body&gt;
</code></pre>

<p>Please take a specific look on a line: </p>

<pre class="lang-html prettyprint-override"><code>  &lt;link rel="stylesheet" href="assets/styles/style.css"&gt;
</code></pre>

<p><strong>style.css</strong>:</p>

<pre class="lang-css prettyprint-override"><code>h1 {
  color: red;
}
</code></pre>

<p>You can run above page with <code>python</code>: </p>

<ul>
<li><code>$ apt update &amp;&amp; apt install -y python3</code></li>
<li><code>$ python3 -m http.server 8080</code> where the <code>index.html</code> and <code>assets</code> folder is stored. </li>
</ul>

<h2>Create an <code>Ingress</code> resource</h2>

<p>Below is an example <code>Ingress</code> resource configured to use <code>nginx-ingress</code> controller: </p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress-example
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
  - host: 
    http:
      paths:
      - path: /
        backend:
          serviceName: ubuntu-service 
          servicePort: ubuntu-port
</code></pre>

<p>After applying above resource you can start to test. </p>

<h3>Test</h3>

<p>You can go to your browser and enter the external IP address associated with your <code>Ingress</code> resource. </p>

<p><strong>As I said above you can check the logs of <code>nginx-ingress</code> controller pod to check how your controller is handling request.</strong></p>

<p>If you run command mentioned earlier <code>python3 -m http.server 8080</code> you will get logs too: </p>

<pre class="lang-sh prettyprint-override"><code>$ python3 -m http.server 8080
Serving HTTP on 0.0.0.0 port 8080 (http://0.0.0.0:8080/) ...
10.48.0.16 - - [12/May/2020 11:06:56] "GET / HTTP/1.1" 200 -
10.48.0.16 - - [12/May/2020 11:06:56] "GET /assets/styles/style.css HTTP/1.1" 200 -
</code></pre>

<h3>Rewrite example</h3>

<p>I've edited the <code>Ingress</code> resource to show you an example of a path rewrite: </p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress-example
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - host: 
    http:
      paths:
      - path: /product/(.*)
        backend:
          serviceName: ubuntu-service 
          servicePort: ubuntu-port
</code></pre>

<p>Changes were made to lines: </p>

<pre class="lang-yaml prettyprint-override"><code>    nginx.ingress.kubernetes.io/rewrite-target: /$1
</code></pre>

<p>and: </p>

<pre class="lang-yaml prettyprint-override"><code>      - path: /product/(.*)
</code></pre>

<p>Steps: </p>

<ul>
<li>The browser sent: <code>/product/</code></li>
<li>Controller got <code>/product/</code> and had it rewritten to <code>/</code></li>
<li>Pod got <code>/</code> from a controller. </li>
</ul>

<p>Logs from the<code>nginx-ingress</code> controller: </p>

<pre class="lang-sh prettyprint-override"><code>CLIENT_IP - - [12/May/2020:11:33:23 +0000] "GET /product/ HTTP/1.1" 200 228 "-" "REDACTED" 438 0.002 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 276 0.001 200 fb0d95e7253335fc82cc84f70348683a
CLIENT_IP - - [12/May/2020:11:33:23 +0000] "GET /product/assets/styles/style.css HTTP/1.1" 200 22 "http://SERVER_IP/product/" "REDACTED" 364 0.002 [default-ubuntu-service-ubuntu-port] [] 10.48.0.13:8080 22 0.002 200 
</code></pre>

<p>Logs from the pod: </p>

<pre class="lang-sh prettyprint-override"><code>10.48.0.16 - - [12/May/2020 11:33:23] "GET / HTTP/1.1" 200 -
10.48.0.16 - - [12/May/2020 11:33:23] "GET /assets/styles/style.css HTTP/1.1" 200 -
</code></pre>

<p>Please let me know if you have any questions in that. </p>
