[英]Passing parameters down to permanent redirect in ingress-nginx
I'm using nginx.ingress.kubernetes.io/permanent-redirect: https://www.google.com
but want to pass down parameters after / in the redirect. 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.但是不能让它与纯 ingress-nginx 一起工作,如果你可以设置 /$1 或类似的东西会很好。 Is this possible with a mix of other annotations or tricks?
这可能与其他注释或技巧的混合吗?
For example https://www.example.com/hello例如https://www.example.com/hello
Should redirect to https://www.google.com/hello应该重定向到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
:它在
nginx.conf
中导致这样的事情:
if ($uri ~* /) {
return 301 http://www.google.com$request_uri;
}
It seems you can use any nginx variable in the address.看来您可以在地址中使用任何nginx 变量。 So I guess you could do things like
$scheme://www.google.com$request_uri
as well:)所以我想你也可以做类似
$scheme://www.google.com$request_uri
的事情:)
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;
我首先尝试了一个
nginx.ingress.kubernetes.io/server-snippet
块,就像在另一个答案中建议的那样,但redirect 301 http://www.google.com$request_uri;
hijacked too much in nginx.conf
and stopped cert-manager from working.在
nginx.conf
中劫持了太多,并阻止了cert-manager的工作。 So I needed a better integrated solution.所以我需要一个更好的集成解决方案。
If you want to debug, you can just look at nginx.conf
yourself, it's really easy:如果要调试,可以自己看
nginx.conf
,真的很简单:
$ 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:可以使用以下
ingress.kubernetes.io/configuration-snippet
配置进行这种重定向:
My ingress.yaml
:我的
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
.验证入口配置并检查其
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:最后测试 if 重定向是否有效:
$ 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.确保您的入口控制器正常运行并将 IP 地址分配给入口。 Note that redirection works even without endpoint service.
请注意,即使没有端点服务,重定向也有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.