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

我们正在 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.


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

来自 ingress-nginx docs over here我可以看到这是预期的行为(即 308 重定向),我认为不希望的是当这些服务尝试创建页面预览时与社交共享服务的交互。
虽然默认情况下 Facebook(或 twitter 等)直接指向 https 站点可以解决这个问题,但我目前无法强制这些站点查看 https 以获取将用于创建预览的内容。
设置永久重定向代码
我还可以看到,看起来我应该能够将重定向代码设置为我想要的任何内容(我相信 301 重定向将允许 Facebook 等人正确拉取帖子/页面片段元),docs on that found here .
问题是,当我添加指定的重定向代码注释时:
nginx.ingress.kubernetes.io/permanent-redirect-code: "301"
尽管能够看到(从我的 kubectl 代理)正确应用了重定向代码注释,但我仍然在我的资源上获得了 308 重定向。作为引用,我在 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注解。

您实际上可以更改 ConfigMap为您 nginx-configuration以便默认重定向为 301。这是您的 nginx 入口 Controller 用于 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荚。

你也可以shell进入nginx-ingress-controller pod 并查看 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

关于Kubernetes Nginx Ingress HTTP 通过 301 而不是 308 重定向到 HTTPS?,我们在Stack Overflow上找到一个类似的问题: Kubernetes Nginx Ingress HTTP to HTTPS redirect via 301 instead of 308? - Stack Overflow

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值