记一次接口HTTPS访问,日志却记录为HTTP问题
背景#
项目添加 ssl 证书后,APP/WEB 端已经修改访问接口域名,但是多次查看日志均为 http.
项目架构#
AWS route53 –> ALB –> 目标群组 (端口 80) –> EC2
ALB 挂载 ssl 证书,侦听器设置了两个规则
- 80 重定向到 443 (可以直接访问域名为 https)
- 443 转发目标组 80 端口
问题#
接口访问 URL 为 HTTPS
日志记录为 HTTP
打印日志代码
// 访问日志
CommonLog::writeAccessLog($request->url() . " parameters=" . json_encode($request->input()));
这是我打印请求的 server 的信息
其中 HTTP_REFERER,HTTP_ORIGIN,HTTP_X_FORWARDED_PORT,HTTP_X_FORWARDED_PROTO 信息都是 HTTPS
REQUEST_SCHEME,SERVER_PORT (这里是因为目标组和 EC2 端口为 80)
探究#
// 底层包路径 vendor\symfony\http-foundation\Request.php
/**
* Checks whether the request is secure or not.
*
* This method can read the client protocol from the "X-Forwarded-Proto" header
* when trusted proxies were set via "setTrustedProxies()".
*
* The "X-Forwarded-Proto" header must contain the protocol: "https" or "http".
*
* @return bool
*/
public function isSecure()
{
if ($this->isFromTrustedProxy() && $proto = $this->getTrustedValues(self::HEADER_X_FORWARDED_PROTO)) {
return \in_array(strtolower($proto[0]), ['https', 'on', 'ssl', '1'], true);
}
$https = $this->server->get('HTTPS');
return !empty($https) && 'off' !== strtolower($https);
}
官方说包含这个 X-Forwarded-Proto 为 https
我的 HTTP_X_FORWARDED_PROTO 这个显示为 https, 有知道的可以给说下原因
nginx 配置#
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: