因测试环境php遇到无法正常读取到https的域名,但是域名配置了ssl证书,故做如下排查。
php测试代码如下
$config['base_url'] = '';#开启调试模式#echo "";print_r($_SERVER);die;$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://':"http://";define('HTTP_TYPE', $http_type);//定义当前域名使用协议是http还是httpsdefine('URL', $http_type.$_SERVER['HTTP_HOST']);define('OLD_URL', $http_type.$_SERVER['HTTP_HOST']);//老版本专用
通过上述代码调试出来如下结果。
Array( [USER] => nginx [HOME] => /var/cache/nginx [HTTP_COOKIE] => ___rl__test__cookies=1533966737026; OUTFOX_SEARCH_USER_ID_NCOO=909839729.8454164; PHPSESSID=borlvm751o6fj00qgk8fhcejv3; PKBET_ORG=Y [HTTP_ACCEPT_LANGUAGE] => zh-CN,zh;q=0.9 [HTTP_ACCEPT_ENCODING] => gzip, deflate, br [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 [HTTP_USER_AGENT] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36 [HTTP_UPGRADE_INSECURE_REQUESTS] => 1 [HTTP_CACHE_CONTROL] => no-cache [HTTP_PRAGMA] => no-cache [HTTP_CONNECTION] => close [HTTP_X_FORWARDED_FOR] => xxx.xx.xxx.xx [HTTP_X_REAL_IP] => xxx.xx.xxx.xx [HTTP_HOST] => xxxx.com [REDIRECT_STATUS] => 200 [SERVER_NAME] => xxxx.com [SERVER_PORT] => 80 [SERVER_ADDR] => 10.10.10.208 [REMOTE_PORT] => 51618 [REMOTE_ADDR] => 10.10.10.207 [SERVER_SOFTWARE] => nginx/1.14.0 [GATEWAY_INTERFACE] => CGI/1.1 [REQUEST_SCHEME] => http [SERVER_PROTOCOL] => HTTP/1.0 [DOCUMENT_ROOT] => /data/www [DOCUMENT_URI] => /index.php [REQUEST_URI] => / [CONTENT_LENGTH] => [CONTENT_TYPE] => [REQUEST_METHOD] => GET [QUERY_STRING] => [PATH_INFO] => [SCRIPT_NAME] => /index.php [SCRIPT_FILENAME] => /data/www/index.php [FCGI_ROLE] => RESPONDER [PHP_SELF] => /index.php [REQUEST_TIME_FLOAT] => 1533971228.3193 [REQUEST_TIME] => 1533971228)
确实没有打印到https被开启的相关内容,检查nginx反向代理配置文件的ssl配置部分。
upstream xxxx.com { server 10.10.10.208:80;}server{ listen 443; server_name xxxx.com; ssl on; ssl_certificate /etc/nginx/crt/xxxx.com/xxxx.com.crt; ssl_certificate_key /etc/nginx/crt/xxxx.com/xxxx.com.key; ssl_session_timeout 5m; location / { proxy_pass http://xxxx.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}
发现上述配置文件ssl证书正常,但是PHP获取不到X-Forwarded-Proto或者HTTPS的变量值,尝试在反向代理配置中加入以下配置。
proxy_set_header X-Forwarded-Proto $scheme;
再次通过php调试打印出现https内容。
[HTTP_X_FORWARDED_PROTO] => https[HTTPS] => on
再次访问PHP的内容已经恢复正常。