我有一个在Nginx泊坞窗容器中运行的Angular应用程序,它在Google Kubernetes中运行,并在带有Google托管证书的Google Kubernetes Ingress后面运行。如果请求来自HTTP /端口80,我似乎无法让Ingress自动重定向到HTTPS。
显然Google Ingress“无法做到”,所以我一直在努力让nginx实例为我做到。但我认为在那种情况下,我需要实际的证书,但我也想不出如何离开Google。
但是...必须有一种简单的方法来做到这一点。 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;
}
}
}
吊舱中的错误正在寻找要安装的证书,但我不知道如何从Google托管证书中获取证书以复制到nginx映像:
"textPayload": "2019/05/08 21:02:13 [emerg] 1#1: no \"ssl_certificate\" is defined for the \"listen ... ssl\" directive in /etc/nginx/nginx.conf:27\n"
答案 0 :(得分:2)
您是对的,目前,Google Cloud HTTPS负载均衡器不允许http-> https重定向。
我不知道如何从Google托管证书中获取证书以复制到nginx图像。
不能。 Google管理证书并为您续订。因此,您将无权访问TLS密钥。
由于Google Cloud HTTPS LB可以侦听HTTPS或HTTP,因此您无法从HTTP重定向到HTTPS。
我建议最快完成此操作的建议是:
nginx-ingress
控制器安装到您的GKE集群,然后将其提供的https重定向注释放在您的Ingress对象上。Secret
字段中安装/使用此tls:
,nginx-ingress控制器将在其nginx.conf上对其进行配置。答案 1 :(得分:2)
对于仍然像我一样遇到此问题的任何人,现在有一种官方方法可以做到这一点:
您可以编写和部署一个 FrontendConfig 文件,其中设置了 redirectToHttps
参数,并将此文件作为前端配置添加到您的入口。这会自动将 HTTP 流量重定向到 HTTPS。
前端配置:
apiVersion: networking.gke.io/v1beta1
kind: FrontendConfig
metadata:
name: my-frontend-config
spec:
redirectToHttps:
enabled: true
responseCodeName: PERMANENT_REDIRECT
入口
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: ingress-name
annotations:
[...]
networking.gke.io/v1beta1.FrontendConfig: my-frontend-config
namespace: default
spec:
[...]
这仅在版本 >= 1.17.x 的集群中可用
见:https://cloud.google.com/kubernetes-engine/docs/how-to/ingress-features#https_redirect
答案 2 :(得分:0)
我正在阅读有关设置其他负载均衡器的信息,偶然发现了我要找的答案。代替基于nginx中的$ scheme进行重定向,您必须改为在$http_x_forwarded_proto
上进行重定向。这样可以防止Google运行状况检查失败,因为它们没有通过http_x_forwarded_proto标头,因此在进行运行状况检查时永远不会达到该重定向。
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}