我正在寻找重定向所有流量
http://example.com - > https://example.com就像几乎所有网站一样。
我看过这个链接没有成功:Kubernetes HTTPS Ingress in Google Container Engine
并在我的ingress.yaml文件中尝试了以下注释。
nginx.ingress.kubernetes.io/configuration-snippet: |
if ($http_x_forwarded_proto != 'https') {
return 301 https://$host$request_uri;
}
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
nginx.ingress.kubernetes.io/ssl-redirect: "true"
kubernetes.io/ingress.allow-http: "false"
一切都没有成功。要清楚,我可以访问https://example.com和http://example.com没有任何错误,我需要http调用重定向到https。
谢谢
GKE使用GCE L7。不支持您在示例中引用的规则,并且应在应用程序级别控制HTTP到HTTPS重定向。
L7插入x-forwarded-proto
标头,您可以使用它来了解前端流量是使用HTTP还是HTTPS。看看这里:Redirecting HTTP to HTTPS
Nginx的链接中也有一个例子(为方便起见而复制):
# Replace '_' with your hostname.
server_name _;
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
GKE使用自己的Ingress Controller,不支持强制https。
这就是为什么你必须自己管理NGINX Ingress控制器的原因。
有关如何在GKE上执行此操作,请参阅this post。
希望能帮助到你。
为了它的价值,我最终在NGINX中使用了反向代理。
这是我的配置:
worker_processes 1;
events {
worker_connections 1024;
}
http {
default_type application/octet-stream;
# Logging Configs
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
# Puntdoctor Proxy Config
include /path/to/config-file.conf;
# PubSub allows 10MB Files. lets allow 11 to give some space
client_max_body_size 11M;
}
然后是config.conf
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name example.com;
ssl_certificate /certs/tls.crt;
ssl_certificate_key /certs/tls.key;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-RSA-RC4-SHA:AES128-GCM-SHA256:HIGH:!RC4:!MD5:!aNULL:!EDH:!CAMELLIA;
ssl_prefer_server_ciphers on;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
# Fix the “It appears that your reverse proxy set up is broken" error.
proxy_pass http://deployment-name:8080/;
proxy_read_timeout 90;
proxy_redirect http://deployment-name:8080/ https://example.com/;
}
}
这是.yaml文件
---
apiVersion: v1
kind: Service
metadata:
name: puntdoctor-lb
spec:
ports:
- name: https
port: 443
targetPort: 443
- name: http
port: 80
targetPort: 80
selector:
app: puntdoctor-nginx-deployment
type: LoadBalancer
loadBalancerIP: 35.195.214.7
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: puntdoctor-nginx-deployment
spec:
replicas: 2
template:
metadata:
labels:
app: puntdoctor-nginx-deployment
spec:
containers:
- name: adcelerate-nginx-proxy
image: nginx:1.13
volumeMounts:
- name: certs
mountPath: /certs/
- name: site-config
mountPath: /etc/site-config/
- name: default-config
mountPath: /etc/nginx/
ports:
- containerPort: 80
name: http
- containerPort: 443
name: https
volumes:
- name: certs
secret:
secretName: nginxsecret
- name: site-config
configMap:
name: nginx-config
- name: default-config
configMap:
name: default
希望这有助于某人解决这个问题,感谢其他2个答案,他们都给了我宝贵的见解。
目前,关于如何正确执行此操作的文档(注释,SSL / HTTPS,运行状况检查等)严重缺乏,并且已经存在了太长时间。我怀疑这是因为他们更喜欢你使用App Engine,这是神奇而又愚蠢的昂贵。对于GKE,这里有两个选择:
以下是使用前者进行工作设置的步骤。
nginx.conf :(省略号表示其他不相关的非强制设置)
user nginx;
worker_processes auto;
events {
worker_connections 1024;
}
http {
...
keepalive_timeout 620s;
## Logging ##
...
## MIME Types ##
...
## Caching ##
...
## Security Headers ##
...
## Compression ##
....
server {
listen 80;
## HTTP Redirect ##
if ($http_x_forwarded_proto = "http") {
return 301 https://[YOUR DOMAIN]$request_uri;
}
location /health/liveness {
access_log off;
default_type text/plain;
return 200 'Server is LIVE!';
}
location /health/readiness {
access_log off;
default_type text/plain;
return 200 'Server is READY!';
}
root /usr/src/app/www;
index index.html index.htm;
server_name [YOUR DOMAIN] www.[YOUR DOMAIN];
location / {
try_files $uri $uri/ /index.html;
}
}
}
注意:仅限一个服务端口。全局转发规则将http_x_forwarded_proto标头添加到通过它的所有流量。因为您域中的所有流量现在都通过此规则(请记住,容器,服务和入口上的一个端口),此标头将(至关重要!)始终设置。请注意上面的检查和重定向:如果标头值为“https”,则仅继续提供服务。根,索引和位置值可能因项目而异(这是一个角度项目)。 keepalive_timeout设置为值recommended by google。我更喜欢使用主要的nginx.conf文件,但大多数人都将custom.conf文件添加到/etc/nginx/conf.d;如果你这样做,只需确保使用includes语句将文件导入主nginx.conf http块。这些注释会突出显示您在一切正常工作时可能要添加的其他设置,例如gzip / brotli,安全标头,保存日志的位置等等。
Dockerfile:
...
COPY nginx.conf /etc/nginx/nginx.conf
CMD ["nginx", "-g", "daemon off;"]
注意:只有最后两行。不需要指定EXPOSE端口。 COPY用修改后的版本替换默认的nginx.conf。 CMD启动轻型服务器。
deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: uber-dp
spec:
replicas: 1
selector:
matchLabels:
app: uber
template:
metadata:
labels:
app: uber
spec:
containers:
- name: uber-ctr
image: gcr.io/uber/beta:v1 // or some other registry
livenessProbe:
failureThreshold: 3
initialDelaySeconds: 60
httpGet:
path: /health/liveness
port: 80
scheme: HTTP
readinessProbe:
failureThreshold: 3
initialDelaySeconds: 30
httpGet:
path: /health/readiness
port: 80
scheme: HTTP
ports:
- containerPort: 80
imagePullPolicy: Always
注意:只需要一个指定的端口,因为我们要指向它的所有(HTTP和HTTPS)流量。为简单起见,我们使用相同的路径进行活动和准备探测;这些检查将在NGINX服务器上处理,但您可以而且应该添加检查应用程序本身健康状况的检查(例如,如果健康,则返回200的专用页面。)GCE也会收集准备情况调查,默认情况下,它有自己的不可移除的运行状况检查。
service.yaml:
apiVersion: v1
kind: Service
metadata:
name: uber-svc
labels:
app: uber
spec:
ports:
- name: default-port
port: 80
selector:
app: uber
sessionAffinity: None
type: NodePort
注意:default-port指定容器上的端口80。
在汉堡菜单中的GCP:VPC网络 - >外部IP地址。转换您自动生成的短暂IP或创建一个新IP。记下姓名和地址。
在汉堡菜单中:网络服务 - >负载平衡 - >单击“高级菜单” - >“证书” - >“创建SSL证书”。按照说明,创建或上载证书,并记下名称。然后,从菜单中:云DNS - >创建区域。按照说明,create a default zone为您的域名。添加一个CNAME记录,其中www为DNS名称,您的域名为规范名称。添加一个空DNS名称值和静态IP作为IPV4的A记录。保存。
ingress.yaml:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: mypt-ingress
annotations:
kubernetes.io/ingress.global-static-ip-name: [NAME OF YOUR STATIC IP ADDRESS]
kubernetes.io/ingress.allow-http: "true"
ingress.gcp.kubernetes.io/pre-shared-cert: [NAME OF YOUR GOOGLE-MANAGED SSL]
spec:
backend:
serviceName: mypt-svc
servicePort: 80
注意:后端属性指向服务,该服务指向容器,其中包含由服务器“保护”的应用程序。注释将您的应用程序与SSL连接,并强制允许http用于运行状况检查。结合起来,服务和入口配置G7 load balancer(组合全局转发规则,后端和前端'服务',SSL证书和目标代理等)。
一切都需要大约10分钟来配置。清除缓存并使用各种浏览器(Tor,Opera,Safari,IE等)测试您的域。一切都将通过https服务。
NGINX Ingress控制器怎么样?我已经看到讨论它更好,因为它更便宜/使用更少的资源,更灵活。它并不便宜:它需要额外的部署/工作量和服务(GCE L4)。而且你需要做更多的配置。它更灵活吗?是。但是在处理大部分工作时,第一个选项为您提供了更为重要的灵活性 - 即允许您继续处理更紧迫的事情。