简体   繁体   English

Kubernetes Nginx Ingress HTTP到HTTPS是通过301而不是308重定向的?

[英]Kubernetes Nginx Ingress HTTP to HTTPS redirect via 301 instead of 308?

We are running a couple of k8s clusters on Azure AKS. 我们正在Azure AKS上运行几个k8s群集。 The service (ghost blog) is behind the Nginx ingress and secured with a cert from Letsencrypt. 该服务(幽灵博客)位于Nginx入口后面,并由Letsencrypt提供的证书进行保护。 All of that works fine but the redirect behavior is what I am having trouble with. 所有这些都很好,但是重定向行为是我遇到的麻烦。

The Ingress correctly re-directs from http://whatever.com to https://whatever.com — the issue is that it does so using a 308 redirect which strips all post/page Meta anytime a user shares a page from the site. Ingress可以正确地从http://whatever.com重定向到https://whatever.com-问题是它使用308重定向来做到这一点,只要用户在网站上共享页面,该重定向就会剥离所有帖子/页面元。

The issue results in users who share any page of the site on most social properties receiving a 'Preview Link' — where the title of the page and the page meta preview do not work and are instead replaced with '308 Permanent Redirect' text — which looks like this: 此问题导致在大多数社交媒体资源上共享网站任何页面的用户都收到“预览链接”(页面标题和页面元预览不起作用,而是由“ 308永久重定向”文本代替)看起来像这样:

在此处输入图片说明

From the ingress-nginx docs over here I can see that this is the intended behavior (ie. 308 redirect) what I believe is not intended is the interaction with social sharing services when those services attempt to create a page preview. 从这里的ingress-nginx文档中,我可以看到这是预期的行为(即308重定向),我认为这不是在社交共享服务尝试创建页面预览时与社交共享服务的交互。

While the issue would be solved by Facebook (or twitter, etc etc) pointing direct to the https site by default, I currently have no way to force those sites to look to https for the content that will be used to create the previews. 尽管默认情况下Facebook(或twitter等)直接指向https网站可以解决此问题,但我目前无法强制这些网站将要用于创建预览的内容转向https。

Setting Permanent Re-Direct Code 设置永久重定向代码

I can also see that it looks like I should be able to set the redirect code to whatever I want it to be (I believe a 301 redirect will allow Facebook et al. to correctly pull post/page snippet meta), docs on that found here . 我还看到,看来我应该能够将重定向代码设置为所需的格式(我相信301重定向将使Facebook等人正确提取帖子/网页摘要元), 有关该文档在这里

The problem is that when I add the redirect-code annotation as specified: 问题是当我按指定添加重定向代码注释时:

nginx.ingress.kubernetes.io/permanent-redirect-code: "301"

I still get a 308 re-direct on my resources despite being able to see (from my kubectl proxy) that the redirect-code annotation correctly applied. 尽管能够(从我的kubectl代理中)看到重定向代码注释正确应用,但我仍然在资源上获得308重定向。 For reference, my full list of annotations on my Ingress looks like this: 作为参考,我在Ingress上的注释的完整列表如下所示:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ghost-ingress
  annotations:
    kubernetes.io/tls-acme: "true"
    nginx.ingress.kubernetes.io/permanent-redirect-code: "301"

To reiterate — my question is; 重申-我的问题是; what is the correct way to force a redirect to https via a custom error code (in my case 301)? 通过自定义错误代码(在我的情况下为301)强制重定向到https的正确方法是什么?

My guess is the TLS redirect shadows the nginx.ingress.kubernetes.io/permanent-redirect-code annotation. 我的猜测是TLS重定向nginx.ingress.kubernetes.io/permanent-redirect-codenginx.ingress.kubernetes.io/permanent-redirect-code注释。

You can actually change the ConfigMap for your nginx-configuration so that the default redirect is 301. That's the configuration your nginx ingress controller uses for nginx itself. 实际上,您可以为nginx-configuration更改ConfigMap ,以使默认重定向为301。这就是您的nginx入口控制器用于nginx本身的配置。 The ConfigMap looks like this: ConfigMap如下所示:

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  name: nginx-configuration
  namespace: ingress-nginx
data:
  use-proxy-protocol: "true"
  http-redirect-code: "301"

You can find more about the ConfigMap options here . 您可以在此处找到有关ConfigMap选项的更多信息。 Note that if you change the ConfigMap you'll have to restart your nginx-ingress-controller pod. 请注意,如果您更改ConfigMap ,则必须重新启动nginx-ingress-controller窗格。

You can also shell into the nginx-ingress-controller pod and see the actual nginx configs that the controller creates: 您还可以将其nginx-ingress-controllernginx-ingress-controller窗格中,并查看控制器创建的实际nginx配置:

kubectl -n ingress-nginx exec -it nginx-ingress-controller-xxxxxxxxxx-xxxxx bash
www-data@nginx-ingress-controller-xxxxxxxxx-xxxxx:/etc/nginx$ cat /etc/nginx/nginx.conf

These directions are for Azure AKS users but the solution for this solution for facebook / social property preview links showing as 308 permanent redirect will probably work on any cloud provider (though it has not been tested) — you would just need to change the way you login / get your credentials etc. 这些说明适用于Azure AKS用户,但针对该解决方案的facebook /社交媒体预览链接的解决方案显示为308永久重定向,该解决方案可能适用于任何云提供商(尽管尚未经过测试)-您只需要更改您的方式即可登录/获取您的凭据等

Thanks to Rico for the solution! 感谢Rico提供的解决方案! Since this is only tested with Facebook you may or may not want to go the ConfigMap application route (which Rico mentions above) this walks through manually editing the ConfigMap as opposed to using kubectl apply -f to apply one saved locally. 由于仅在Facebook上进行了测试,因此您可能会或可能不想走ConfigMap应用程序路线(Rico在上面提到过),这将通过手动编辑ConfigMap来完成,而不是使用kubectl apply -f在本地应用一个保存的kubectl apply -f

  1. Pickup AZ Credentials for your cluser ( az login ) 为您的cluser拾取AZ凭证( az login
  2. Assume the role for your cluster: az aks get-credentials --resource-group yourGroup --name your-cluster 假设您的集群的角色: az aks get-credentials --resource-group yourGroup --name your-cluster
  3. Browse your Cluster: az aks browse --resource-group yourGroup --name your-cluster 浏览您的集群: az aks browse --resource-group yourGroup --name your-cluster
  4. Navigate to the namespace containing your Ingress nGinx containers (not the backend services — although they could be in the same NS). 导航到包含您的Ingress nGinx容器的名称空间(不是后端服务,尽管它们可以在同一NS中)。
  5. On the left hand side navigation menu (just above settings) find the 'ConfigMaps' tab and click it. 在左侧导航菜单(位于设置上方)上,找到“ ConfigMaps”选项卡,然后单击它。
  6. Edit the 'Data' element of the YAML and add the following line (note the quotes around both the name and number in the key/value): "data": { "some-other-setting-here": "false", "http-redirect-code": "301" } You will need a comma after each key/value line except the last. 编辑YAML的'Data'元素,并添加以下行(请注意键/值中名称和数字的引号): "data": { "some-other-setting-here": "false", "http-redirect-code": "301" }除最后一行外,您需要在每个键/值行后加一个逗号。
  7. Restart your nginx-controller POD by deleting it make SURE you don't delete the deployment like I did. 通过删除它重新启动nginx-controller POD,以确保您不会像我一样删除部署。
  8. If you want to be productive you can upgrade your nginx install (from helm) which will restart / re-create the container in the process by using: helm upgrade ngx-ingress stable/nginx-ingress Where ngx-ingress is the name of your helm install. 如果您想提高工作效率,可以通过以下方式升级nginx安装(从helm): helm upgrade ngx-ingress stable/nginx-ingress其中ngx-ingress是您的名称掌舵安装。 Also note that using the '--reuse-values' flag will cause your upgrade to fail (re: https://github.com/helm/helm/issues/4337 ) 还要注意,使用'--reuse-values'标志将导致您的升级失败(re: https : //github.com/helm/helm/issues/4337
  9. If you don't know the name you used for nginx when you installed it from Helm originally you can use helm list to find it. 如果您最初在Helm上安装nginx时不知道使用的名称,则可以使用helm list进行查找。
  10. Finally to test and make sure your Re-Directs are using the correct ConfigMap code, curl your http site with: curl myhttpdomain.com You should receive something like this: 最后,要测试并确保您的重定向使用了正确的ConfigMap代码,请使用以下命令卷曲您的http网站: curl myhttpdomain.com您应该收到类似以下内容:

``` ```

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.15.3</center>
</body>
</html>

``` ```

One important thing to note here is that if you are making the change to a 301 re-direct to try to fix the preview link for facebook or one of the other social media properties (twitter etc etc) then in all likelihood this will not fix any link to any page / post that you have already linked to — at least not right away. 这里要注意的一件事是,如果您要更改为301重定向以尝试修复facebook或其他社交媒体属性之一(推特等)的预览链接,则很可能无法解决您已链接到的任何页面/帖子的任何链接-至少不是马上。

The social properties all use intense caching to limit their resource usage but you can check to see if the above fixes your preview link issue by linking to a NEW page / post that you have not previously referenced. 所有社交媒体资源都使用密集缓存来限制其资源使用,但是您可以通过链接到以前未引用的新页面/帖子来检查以上内容是否解决了预览链接问题。

Be Aware of Implications for 'POST' 注意“ POST”的含义

So the major reason that nginx-ingress uses a code 308 is because it keeps the 'body' / payload intact in cases where you are sending a POST request (as opposed to a normal GET request link you do with a browser etc). 因此,nginx-ingress使用代码308的主要原因是因为在您发送POST请求的情况下(与您使用浏览器进行的常规GET请求链接相反),它可以使“主体” /有效负载保持完整。

For me this wasn't a concern but if you are for whatever reason posting to the http address and expecting that to be re-directed seamlessly that will probably not work — after you swap to the 301 redirect discussed in post that is. 对我来说,这不是问题,但是如果出于任何原因将其发布到http地址,并希望将其无缝重定向,则可能无法正常工作-在交换到本文中讨论的301重定向之后。

HOWEVER if you are not expecting a seamless redirect when sending POST requests (I think most people probably are not, I know I am not) then I think this is the best way to fix the Facebook 308 Permanent redirect behavior. 但是,如果您在发送POST请求时不期望无缝重定向(我想大多数人可能不是,我知道我不是),那么我认为这是修复Facebook 308永久重定向行为的最佳方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM