1. 程式人生 > >php讀取不到https的域名

php讀取不到https的域名

ddr encoding 讀取 org pst index 通過 代理 type

因測試環境php遇到無法正常讀取到https的域名,但是域名配置了ssl證書,故做如下排查。

php測試代碼如下

$config[‘base_url‘] = ‘‘;

#開啟調試模式
#echo "<pre>";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還是https
define(‘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的內容已經恢復正常。

php讀取不到https的域名