istio: http 流量 301重定向到 https

现在 Chrome 浏览器默认 HTTPS,HTTPS 是大势所趋,只需 2 行配置,即可让 istio 支持将 http 流量 301 重定向到 https。

下面截取本站 Gateway 配置中的一部分,只需添加最后两行即可实现流量重定向。

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: dhcp-cn
  namespace: dhcp-cn
spec:
  selector:
    app: dhcp-cn
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - dhcp.cn
    tls: # 只能在协议为 HTTP 的 配置下添加如下配置 
      httpsRedirect: true #  HTTP 请求 301 重定向到 HTTPS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

效果验证

~$ curl -I http://dhcp.cn
HTTP/1.1 301 Moved Permanently
location: https://dhcp.cn/
date: Sat, 28 Aug 2021 07:58:46 GMT
server: istio-envoy
transfer-encoding: chunked

~$ curl -I http://dhcp.cn/curl.html
HTTP/1.1 301 Moved Permanently
location: https://dhcp.cn/curl.html
date: Sat, 28 Aug 2021 07:58:54 GMT
server: istio-envoy
transfer-encoding: chunked
1
2
3
4
5
6
7
8
9
10
11
12
13

reference