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

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

我们是 运行 Azure AKS 上的几个 k8s 集群。 该服务(幽灵博客)位于 Nginx 入口后面,并使用 Letsencrypt 的证书进行保护。所有这些都工作正常,但重定向行为是我遇到的问题。

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.

该问题会导致在大多数社交媒体资源上分享网站任何页面的用户收到 'Preview Link' — 页面标题和页面元预览不起作用,而是被替换为 '308永久重定向'文本——看起来像这样:

ingress-nginx docs over here 我可以看出这是预期的行为(即 308 重定向),我认为不是预期的是当这些服务试图创建页面预览时与社交共享服务的交互。

虽然默认情况下直接指向 https 站点的 Facebook(或 twitter 等)可以解决这个问题,但我目前无法强制这些站点查看 https 以获取将用于的内容创建预览。

设置永久Re-Direct代码

我还可以看到我应该能够将重定向代码设置为我想要的任何值(我相信 301 重定向将允许 Facebook 等人正确提取 post/page 代码段元), docs on that found here.

问题是,当我按指定添加 redirect-code 注释时:

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

尽管能够(从我的 kubectl 代理)看到正确应用了 redirect-code 注释,但我的资源仍然得到 308 re-direct。作为参考,我在 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)?

我的猜测是 TLS 重定向隐藏了 nginx.ingress.kubernetes.io/permanent-redirect-code 注释。

您实际上可以为 nginx-configuration 更改 ConfigMap,以便默认重定向为 301。这是您的 nginx 入口控制器用于 nginx 本身的配置。 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"

您可以找到有关 ConfigMap 选项的更多信息 here。请注意,如果您更改 ConfigMap,则必须重新启动 nginx-ingress-controller pod。

您还可以 shell 进入 nginx-ingress-controller pod 并查看控制器创建的实际 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.

感谢 Rico 提供解决方案!由于这仅在 Facebook 上进行了测试,您可能希望也可能不希望采用 ConfigMap 应用程序路线(Rico 在上面提到过),这将逐步完成手动编辑 ConfigMap,而不是使用 kubectl apply -f 应用本地保存的 ConfigMap。

  1. 为您的集群 (az login) 提取 AZ 凭据
  2. 代入您的集群的角色:az aks get-credentials --resource-group yourGroup --name your-cluster
  3. 浏览您的集群:az aks browse --resource-group yourGroup --name your-cluster
  4. 导航到包含 Ingress nGinx 容器的命名空间(不是后端服务——尽管它们可能在同一个 NS 中)。
  5. 在左侧导航菜单(设置上方)找到 'ConfigMaps' 选项卡并单击它。
  6. 编辑 YAML 的 'Data' 元素并添加以下行(注意 key/value 中名称和编号两边的引号): "data": { "some-other-setting-here": "false", "http-redirect-code": "301" } 除了最后一行 key/value 之外,每个 key/value 行后都需要一个逗号。
  7. 通过删除它来重新启动您的 nginx-controller POD 确保您不要像我一样删除部署。
  8. 如果你想提高工作效率,你可以升级你的 nginx 安装(从 helm),这将通过使用以下命令重新启动/重新创建进程中的容器: helm upgrade ngx-ingress stable/nginx-ingress 其中 ngx-ingress 是您的 helm 安装的名称。另请注意,使用“--reuse-values”标志将导致升级失败(回复:https://github.com/helm/helm/issues/4337
  9. 如果您不知道最初从 Helm 安装 nginx 时使用的名称,您可以使用 helm list 找到它。
  10. 最后要测试并确保您的重定向使用正确的 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>

```

这里要注意的一件重要事情是,如果您要更改 301 重定向以尝试修复 facebook 或其他社交媒体属性之一(twitter 等)的预览 link ) 那么很可能这不会将任何 link 修复到您已经 link 访问过的任何页面 / post — 至少不会立即修复。

社交属性都使用密集缓存来限制其资源使用,但您可以通过 linking 到新页面/[=73 来检查以上内容是否解决了您的预览 link 问题=],您之前没有引用过。

注意对 'POST'

的影响

所以 nginx-ingress 使用代码 308 的主要原因是因为它在您发送 POST 请求(与普通 GET 相反)的情况下保持 'body' / 有效负载不变请求 link 你用浏览器等)。

对我来说,这不是问题,但如果您出于某种原因 post 正在访问 http 地址并期望它被无缝重定向,这可能行不通 — 在您切换到post 中讨论的 301 重定向。

但是,如果您在发送 POST 请求时不期望无缝重定向(我想大多数人可能不是,我知道我不是)那么我认为这是修复 Facebook 308 的最佳方法永久重定向行为。