Reputation: 5648
We want to load example.xyz.com in site.abc.com. The best way is to redirect/rewrite all the requests from site.abc.com to example.xyz.com. However, we don't want the browser URL to be changed. From this similar SO problem we understand that we need an Nginx location config as below
server {
servername site.abc.com;
listen 80;
....
....
....
....
location / {
proxy_pass http://example.xyz.com;
rewrite /(.*)$ /$1 break;
}
}
However, I'm not sure how to create a similar rule in Kubernetes ingress-nginx as it adds proxy_pass for each rule, which prevents us from adding proxy_pass config in nginx.ingress.kubernetes.io/configuration-snippet:
annotation.
Also providing nginx.ingress.kubernetes.io/rewrite-target: http://example.xyz.com/$1
annotation in ingress as below, redirects to example.xyz.com instead of loading example.xyz.com in site.abc.com.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: http://example.xyz.com/$1
name: url-rewrite
namespace: default
spec:
rules:
- host: site.abc.com
http:
paths:
- backend:
service:
name: service
port:
number: 80
path: /(.*)
pathType: ImplementationSpecific
How can we load example.xyz.com in site.abc.com without any change in browser URL using ingress-nginx in this case?
Upvotes: 1
Views: 2620
Reputation: 5648
With this solution as a reference, pointed out by @WytrzymałyWiktor I was able to make changes and it worked. Here is the updated ingress file.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
location ~ "^/(.*)" {
proxy_pass http://example.xyz.com;
rewrite /(.*)$ /$1 break;
}
name: url-rewrite
namespace: default
spec:
rules:
- host: site.abc.com
One problem though here in making SSL redirect work. In some cases the target(http://example.xyz.com) will return 302 /some/other/path
in such cases http://site.abc.com gets redirected as http://site.abc.com/some/other/path. Not sure how to make it to redirect as https://site.abc.com/some/other/path.
Setting nginx.ingress.kubernetes.io/ssl-redirect: "true"
and nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
doesn't seem to work.
Adding this as an answer for documentation, As it will be helpful for people with a similar problem. Not a possible duplicate as the referenced solution addresses on adding proxy_pass
whereas this, addresses URL rewrite without changing browser URL.
Upvotes: 1