GCP HTTP-health-CHECK 在强制重定向到"www."时失败



我正在使用GCP,我想强制将我网站的所有流量从example.com重定向到www.example.com,所以我在我的Nginx配置中使用了一个简单的重定向,如下所示:

server {
listen      80;
server_name example.com;
return      301         https://www.example.com$request_uri;
}
server {
listen      80;
server_name www.example.com;
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri; #permanent;
}
#Other configurations
}

(此 Nginx 配置位于负载均衡器后端服务的实例中,负载均衡器的前端具有 SSL 证书(

但是现在 GCP 的默认 http-health-check 不起作用,因为它正在等待200响应,而不是我正在响应的301

那么,如何使用http-health-check使www.重定向呢?

我发现它:),我将问题中提到的Nginx配置更改为以下内容:

server {
listen      80;
server_name www.example.com;
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri; #permanent;
}
# to force adding 'www.' without bothering GCP health checks which connects with direct IP
if ($host = "example.com") {
return 301 https://www.example.com$request_uri; #permanent;
}
#Other configurations
}

这里的诀窍是检查$host变量,而普通用户会键入example.com来打开站点,GCP 运行状况检查将使用直接 IP 进行连接,因此他们不会看到301🎉

重定向

相关内容

最新更新