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 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.