Nginx reverse proxy URL getting rewritten

0 votes
I am using nginx reverse proxy with ingress object to route my requests to kubernetes pod.

With the below config in place, when I place a request to http://myservices.myorg.com/jenkins

My request is going to http://myservices.myorg.com/login?from=%2F
But i want it to go to http://myservices.myorg.com/jenkins/login?from=%2F

So after I manually replaced to above correct path, all the resources like css, js are rendered with wrong path as http://myservices.myorg.com/static/beacae7e/css/simple-page.css

But it must be http://myservices.myorg.com/jenkins/static/beacae7e/css/simple-page.css

Similarly, after I logged in (I think http POST method) it is going to home page (in browser it changes from
http://myservices.myorg.com/jenkins/login  to  
http://myservices.myorg.com/jenkins

But all the resources rendered again are with wrong path (without /jenkins) -
http://myservices.myorg.com/static/beacae7e/css/layout-common.css

````
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myingress
spec:
  rules:
  - host: myservices.myorg.com
    http:
      paths:
      - path: /jenkins
        backend:
           serviceName: jenkins
           service port: 80
````

And below is nginx.conf -

````
http {
server {
    listen 80;
    server_name ip-10-118-6-35.ec2.internal;
     location /jenkins {
       proxy_pass https://backend_nodes_jenkins/;
      }
   }
 upstream backend_nodes_jenkins {
    server 10.102.194.242:80;
  }
}

````
 

As you can see in above nginx.conf , in proxy_pass I am having slash at the end of the URL since I should not pass /jenkins to my pod.

So this is working as expected but only issue is get requests are rewriting my url I believe. Please help.
Feb 12, 2019 in Kubernetes by Gopi

recategorized Feb 12, 2019 by Kalgi 4,725 views

3 answers to this question.

+1 vote

Hey @Gopi, try your ingress probably like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: myingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: "/jenkins"
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^(/jenkins)$ http://$best_http_host$1/ permanent;
spec:
  rules:
  - host: myservices.myorg.com
    http:
      paths:
      - path: /jenkins
        backend:
          serviceName: jenkins
          servicePort: 80
answered Feb 12, 2019 by Kalgi
• 52,350 points
Hey thanks for the response
Tried that but still same.. I still see css and js rendered in the path /static/.. instead of /jenkins/static/..

I also tried nginx.ingress.kubernetes.io/app-root: "/jenkins" annotation instead of rewrite-target but still same :(

I believe Jenkins app is generating the urls dynamically..
Looks like Jenkins will generate hyperlinks like this. Read from this page - https://wiki.jenkins.io/display/JENKINS/Hyperlinks+in+HTML

So not sure how to proceed..
Figured it out by passing as env variable in docker CMD - JENKINS_OPTS="--prefix=/jenkins"

What does CMD - JENKINS_OPTS="--prefix=/jenkins" do?

0 votes
Hi i had the same issue.  i had re- written plugin or logic to append route path to service url(kong (nginx wrapper) terminology).
so better change configurationand append  proxy pass parh param in  upstream url.
answered Mar 6, 2019 by anonymous
0 votes

 take a look at 
nginx.ingress.kubernetes.io/app-root
and 
nginx.ingress.kubernetes.io/rewrite-target
annotations(https://kubernetes.github.io/.../nginx.../annotations/). I'm not sure if you're editing the
nginx.conf manually, if so don't do that. Use annotations instead.

answered Mar 6, 2019 by Madhu

Related Questions In Kubernetes

0 votes
1 answer

Getting the pod's ip address for the container inside of it

You can make sure that the replication ...READ MORE

answered Aug 24, 2018 in Kubernetes by DareDev
• 6,890 points
2,880 views
0 votes
1 answer

Kubernetes Ingress controllers for wildcard url mapping

If you use the stock controllers you ...READ MORE

answered Sep 10, 2018 in Kubernetes by Kalgi
• 52,350 points
3,031 views
0 votes
1 answer

ingress port getting redirected in kubernetes

Try the following, it worked for me Changed ...READ MORE

answered Sep 10, 2018 in Kubernetes by Kalgi
• 52,350 points
823 views
0 votes
1 answer

Ingress nginx loading resource 404 in kubernetes

This is not a routing problem on ...READ MORE

answered Sep 10, 2018 in Kubernetes by Kalgi
• 52,350 points
8,142 views
+1 vote
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
15,476 views
+15 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,035 views
0 votes
2 answers

Kubernetes Ingress running behind nginx reverse proxy

You need an ingress as mentioned by ...READ MORE

answered Sep 10, 2018 in Kubernetes by Hannah
• 18,520 points
8,531 views
0 votes
2 answers

DNS does not resolve with NGINX in Kubernetes

adding to @kalgi's answer Using just the hostname ...READ MORE

answered Aug 30, 2018 in Kubernetes by Nilesh
• 7,060 points
5,620 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP