强制重定向到 www. 时 gcp http-health-check 失败
gcp http-health-check fails when forcing redirecting to `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
重定向
我正在使用 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
重定向