我使用nginx.ingress.kubernetes.io/permanent-redirect: https://www.google.com,但希望在重定向之后传递参数。但是,如果你能设置/$1或类似的东西,那么它就不能与纯入口-nginx一起工作了。这是否可以与其他注释或技巧相结合?
发布于 2019-11-27 13:20:06
可以通过以下ingress.kubernetes.io/configuration-snippet配置来进行这种重定向:
我的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应用入口配置:
$ kubectl apply -f ingress.yaml
ingress.extensions/ingress-redirect created验证入口配置并检查其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最后测试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确保您的入口控制器运行正常,并将IP地址分配给入口。注意,即使没有端点服务,重定向也能工作。
发布于 2020-06-04 19:40:30
令人惊讶的是,这很容易:
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在nginx.conf中,它会导致类似的情况
if ($uri ~* /) {
return 301 http://www.google.com$request_uri;
}似乎您可以在地址中使用任何nginx变量。所以我想你也可以做一些像$scheme://www.google.com$request_uri这样的事情:)
我首先尝试了一个nginx.ingress.kubernetes.io/server-snippet块,就像这里的另一个答案所建议的那样,但是redirect 301 http://www.google.com$request_uri;在nginx.conf中劫持了太多,阻止了证书经理的工作。所以我需要一个更好的综合解决方案。
如果您想调试,您可以自己查看nginx.conf,这非常简单:
$ kubectl exec -n nginx-ingress nginx-ingress-controller-*** -- cat /etc/nginx/nginx.confhttps://stackoverflow.com/questions/58993607
复制相似问题