I'm using nginx.ingress.kubernetes.io/permanent-redirect: https://www.google.com
but want to pass down parameters after / in the redirect. But can't get it to work with pure ingress-nginx, would be sweet if you could set /$1 or something similar. Is this possible with a mix of other annotations or tricks?
For example https://www.example.com/hello
Should redirect to https://www.google.com/hello
It's surprisingly easy:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
nginx.ingress.kubernetes.io/permanent-redirect: http://www.google.com$request_uri
spec:
rules:
- host: www.example.com
http:
paths:
- path: /testpath
pathType: Prefix
backend:
serviceName: test
servicePort: 80
It leads to something like this in nginx.conf
:
if ($uri ~* /) {
return 301 http://www.google.com$request_uri;
}
It seems you can use any nginx variable in the address. So I guess you could do things like $scheme://www.google.com$request_uri
as well :)
I first tried a nginx.ingress.kubernetes.io/server-snippet
block like suggested in another answer here, but redirect 301 http://www.google.com$request_uri;
hijacked too much in nginx.conf
and stopped cert-manager from working. So I needed a better integrated solution.
If you want to debug, you can just look at nginx.conf
yourself, it's really easy:
$ kubectl exec -n nginx-ingress nginx-ingress-controller-*** -- cat /etc/nginx/nginx.conf
It is possible to do this kind of redirection with following ingress.kubernetes.io/configuration-snippet
configuration:
My ingress.yaml
:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-redirect
annotations:
nginx.ingress.kubernetes.io/server-snippet: |
return 301 http://www.google.com$request_uri;
spec:
rules:
- host: www.example.com
http:
paths:
- path: /
backend:
serviceName: servicename
servicePort: 80
Apply ingress configuration:
$ kubectl apply -f ingress.yaml
ingress.extensions/ingress-redirect created
Verify ingress configuration and check its address
.
$ kubectl describe ingress
Name: ingress-redirect
Namespace: default
Address: 10.156.0.33
Default backend: default-http-backend:80 (<none>)
Rules:
Host Path Backends
---- ---- --------
www.example.com
/ my-nginx:80 (<none>)
Annotations:
kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/server-snippet":"return 301 http://www.google.com$request_uri; \n"},"name":"ingress-redirect","namespace":"default"},"spec":{"rules":[{"host":"www.example.com","http":{"paths":[{"backend":{"serviceName":"my-nginx","servicePort":80},"path":"/"}]}}]}}
nginx.ingress.kubernetes.io/server-snippet: return 301 http://www.google.com$request_uri;
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal CREATE 33m nginx-ingress-controller Ingress default/ingress-redirect
Normal UPDATE 30m (x2 over 33m) nginx-ingress-controller Ingress default/ingress-redirect
And Finally test the if redirection works:
$ curl -v http://10.156.0.33/test/with?some=query -H "Host: www.example.com"
* Trying 10.156.0.33...
* TCP_NODELAY set
* Connected to 10.156.0.33 (10.156.0.33) port 80 (#0)
> GET /test/with?some=query HTTP/1.1
> Host: www.example.com
> User-Agent: curl/7.52.1
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: openresty/1.15.8.2
< Date: Wed, 27 Nov 2019 13:03:08 GMT
< Content-Type: text/html
< Content-Length: 175
< Connection: keep-alive
< Location: http://www.google.com/test/with?some=query
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>openresty/1.15.8.2</center>
</body>
</html>
* Curl_http_done: called premature == 0
* Connection #0 to host 10.156.0.33 left intact
Make sure that Your ingress-controller is running properly and assigns IP address to ingress. Note that redirection works even without endpoint service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With