gpt4 book ai didi

nginx - 将 Google Kubernetes Ingress 重定向到 HTTPS

转载 作者:行者123 更新时间:2023-12-01 21:57:09 25 4
gpt4 key购买 nike

我有一个 Angular 应用程序,在 nginx docker 容器中运行,它在 Google Kubernetes 中运行,并在具有 Google 托管证书的 Google Kubernetes Ingress 后面运行。如果请求来自 HTTP/端口 80,我似乎无法让 Ingress 自动重定向到 HTTPS。

显然 Google Ingress“做不到”,所以我一直在尝试让 nginx 实例为我做这件事。但我认为在那种情况下我需要实际的证书,我也不知道如何离开谷歌。

但是...必须有一个简单的方法来做到这一点。 HTTPS 在网站上运行良好,我只是无法让它从 HTTP 重定向到 HTTPS...

这是我的 Ingress YAML:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ui-ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: my-ip
networking.gke.io/managed-certificates: my-certificate
spec:
rules:
- host: example.com
http:
paths:
- backend:
serviceName: web-ui
servicePort: 80

还有我的 nginx.conf 带有一个不起作用的注释掉的重定向:

worker_processes  1;

events {
worker_connections 1024;
}

http {

server {
listen 80 default_server;
server_name example.com;

root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

location / {
try_files $uri $uri/ /index.html;
}

#if ($scheme = http) {
# return 301 https://$server_name$request_uri;
#}
}
}

我还尝试了更长的 nginx.conf,导致 Google/Ingress 出现 502 错误:

worker_processes  1;

events {
worker_connections 1024;
}

http {

server {
listen 80;
server_name example.com;

return 301 https://example.com$request_uri;
}

server {
listen 443 ssl http2;
server_name example.com;

return 301 $scheme://example.com$request_uri;
}

server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name example.com;

root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;

gzip on;
gzip_min_length 1000;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

location / {
try_files $uri $uri/ /index.html;
}


}
}

pod 的错误是寻找要安装的证书,但我不知道如何从 Google Managed Certs 中获取证书以复制到 nginx 图像:

"textPayload": "2019/05/08 21:02:13 [emerg] 1#1: 没有为/中的\"listen ... ssl\"指令定义\"ssl_certificate\"etc/nginx/nginx.conf:27\n"

最佳答案

您是对的,目前,Google Cloud HTTPS 负载均衡器不允许 http-->https 重定向。

I don't know how to get the certificate out of Google Managed Certs to copy to the nginx image.

你不能。 Google 管理证书并为您续订它们。因此,您将无权访问您的 TLS key 。

由于 Google Cloud HTTPS LB 可以监听 HTTPS 或 HTTP,因此您无法从 HTTP 重定向到 HTTPS。

我建议以最快的方式完成这项工作:

  • nginx-ingress Controller 安装到您的 GKE 集群,并将它提供的 https 重定向注释放在您的 Ingress 对象上。
  • 这将创建 L3 (TCP/IP) 负载均衡器而不是 HTTPS 负载均衡器,因此您可以同时处理 HTTP 和 HTTPS 流量。
  • 安装和使用 cert-manager Controller 为您的 Ingress(不是 GKE ingress、Nginx ingress)获取证书。这将为您提供 tls key + 证书文件作为 secret (我的 gke-letsencrypt 教程可能会有所帮助)。
  • 然后您可以从您的 nginx Ingress 的 tls: 字段装载/使用此 Secret,nginx-ingress Controller 将在其 nginx.conf 上配置该字段。

关于nginx - 将 Google Kubernetes Ingress 重定向到 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56049181/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com