Ingress-nginx: How to remove path from proxy_pass

Created on 4 Feb 2017  路  13Comments  路  Source: kubernetes/ingress-nginx

I'm having an issue with using the path /jupyter to send traffic to another service, but then needing to drop the juptyer portion and have the request sent as / since my upstream service complains with 404 not found /juptyer. It's similar to the issue describe here: http://serverfault.com/questions/562756/how-to-remove-the-path-with-an-nginx-proxy-pass and I not sure if this is possible with the nginx ingress today?

Most helpful comment

I know this is an old issue, but this might help someone else. I was actually missing the trailing slash from my path, so it should be.

...
paths:
  - path: /owncloud/
...

The redirect now works as expected without changing the path prefix in the docker container.

All 13 comments

@jason-riddle please check the rewrite annotation.
Check this example

Ah yes that's it. Thanks!

Alright so I closed this prematurely when I shouldn't have. It looks like some css resources aren't being loaded properly. Here's a minimal example I was able to come up with.

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: 'true'
    ingress.kubernetes.io/rewrite-target: /
    ingress.kubernetes.io/ssl-redirect: 'true'
  name: owncloud-nginx-ingress
spec:
  rules:
    - http:
        paths:
          # - path: /
          #   backend:
          #     serviceName: owncloud
          #     servicePort: 80
          - path: /owncloud
            backend:
              serviceName: owncloud
              servicePort: 80
  tls:
    - hosts:
        - myhost
      secretName: my-cert

I've compared what is expected with the actual result.

screen shot 2017-02-09 at 9 53 05 pm

However, this scenario works fine.

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: 'true'
    ingress.kubernetes.io/rewrite-target: /
    ingress.kubernetes.io/ssl-redirect: 'true'
  name: owncloud-nginx-ingress
spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: owncloud
              servicePort: 80
          - path: /owncloud
            backend:
              serviceName: owncloud
              servicePort: 80
  tls:
    - hosts:
        - myhost
      secretName: my-cert

screen shot 2017-02-09 at 9 59 43 pm

@jason-riddle please keep in mind the redirect do not rewrites the content returned by the backend.
If the application uses relative URLs in the css you can add the annotation ingress.kubernetes.io/add-base-url

https://github.com/kubernetes/ingress/blob/master/controllers/nginx/configuration.md#rewrite

It doesn't look like it's using relative url's.

<!DOCTYPE html>
<!--[if lte IE 8]><html class="ng-csp ie ie8 lte9 lte8" data-placeholder-focus="false" lang="en" ><![endif]-->
<!--[if IE 9]><html class="ng-csp ie ie9 lte9" data-placeholder-focus="false" lang="en" ><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html class="ng-csp" data-placeholder-focus="false" lang="en" ><!--<![endif]-->
    <head data-requesttoken="17TPDX3LpDcnxbRaeAzHeff6WD2UOJ"><base href="https://_/">
        <meta charset="utf-8">
        <title>
        ownCloud        </title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0">
        <meta name="apple-itunes-app" content="app-id=543672169">
        <link rel="shortcut icon" type="image/png" href="/core/img/favicon.png">
        <link rel="apple-touch-icon-precomposed" href="/core/img/favicon-touch.png">
                    <link rel="stylesheet" href="/core/css/styles.css?v=10fcce33cd4fe6baab7a9ec702b14685" media="screen">
                    <link rel="stylesheet" href="/core/css/header.css?v=10fcce33cd4fe6baab7a9ec702b14685" media="screen">

I see that /core/* is hardcoded into the application. Is there anything else I can do about this?

@jason-riddle can you share the owncloud service definition (removing private information)

owncloud-service.yml

kind: Service
apiVersion: v1
metadata:
  name: owncloud
  labels:
    app: owncloud
spec:
  type: ClusterIP
  selector:
    app: owncloud
  ports:
    - name: http
      port: 80

owncloud-ingress.yml

kind: Ingress
apiVersion: extensions/v1beta1
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: 'true'
    ingress.kubernetes.io/add-base-url: 'true'
    ingress.kubernetes.io/rewrite-target: /
    ingress.kubernetes.io/ssl-redirect: 'true'
  name: owncloud-nginx-ingress
spec:
  rules:
    - http:
        paths:
          - path: /owncloud
            backend:
              serviceName: owncloud
              servicePort: 80
  tls:
    - hosts:
        - example.com
      secretName: my-cert

@jason-riddle please check this image that allows you to change the path (TARGET_SUBDIR)

@jason-riddle please reopen if you still have issues.

@aledbf Wow that looks promising. Thanks for that. I'll look into it.

I know this is an old issue, but this might help someone else. I was actually missing the trailing slash from my path, so it should be.

...
paths:
  - path: /owncloud/
...

The redirect now works as expected without changing the path prefix in the docker container.

@jason-riddle
The non-relative urls like /core/ are still not getting replaced... any issue on that...
Pls note I am using my own service that returns non relative urls for css and js files.

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    ingress.kubernetes.io/add-base-url: "true"
    kubernetes.io/ingress.class: nginx
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/rewrite-target: "/"
    nginx.ingress.kubernetes.io/ssl-redirect: "true"
  generation: 7
  labels:
    app: testingress
  name: test
  namespace: dev
spec:
  rules:
  - host: '*.test.mydomain.com'
    http:
      paths:
      - backend:
          serviceName: api-service
          servicePort: http
        path: /path1/
      - backend:
          serviceName: ui-service
          servicePort: http
        path: /path2/

I get the index.html served well from path2 service. But the css and js files which are non-relative refer directly to test.mydomain.com/static path instead of test.mydomain.com/path2/static

Was this page helpful?
0 / 5 - 0 ratings