phpMyAdmin 是一套用來管理 MySQL 的 Web 介面,如果要讓 phpMyAdmin 強制走 https 的話,可以透過兩種方式,一種是直接設定 phpMyAdmin,另外一種方式是透過 Apache rewrite 或 Nginx 設定,底下來分別說明。
1. phpMyAdmin 設定
直接設定 config.inc.php,加入底下設定
$cfg['ForceSSL'] = true;
2. Nginx 或 Apache 設定
打開 Apache mod_rewrite 功能,將設定寫入 .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/directory(.*)$ https://%{HTTP_HOST}/directory$1 [L,R]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/directory(.*)$ https://%{HTTP_HOST}/directory$1 [L,R]
或是使用 Nginx 設定,將 80 port 轉到 https,設定 443 port 的 SSL 憑證。
server {
listen 80;
server_name xxx.xxx.xxx.xxx;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
# listen 80 default_server deferred; # for Linux
# listen 80 default_server accept_filter=httpready; # for FreeBSD
#listen 80;
listen 443 ssl spdy;
ssl on;
ssl_certificate /etc/nginx/conf/api.ovoq.tv/server.crt;
ssl_certificate_key /etc/nginx/conf/api.ovoq.tv/ssl.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
# The host name to respond to
server_name xxx.xxx.xxx.xxx;
}
listen 80;
server_name xxx.xxx.xxx.xxx;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
# listen 80 default_server deferred; # for Linux
# listen 80 default_server accept_filter=httpready; # for FreeBSD
#listen 80;
listen 443 ssl spdy;
ssl on;
ssl_certificate /etc/nginx/conf/api.ovoq.tv/server.crt;
ssl_certificate_key /etc/nginx/conf/api.ovoq.tv/ssl.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
# The host name to respond to
server_name xxx.xxx.xxx.xxx;
}
上面 Nginx 設定完成後,會發現登入 phpMyAdmin 後,網址被轉成
http://xxx.xxx.xxx.xx:443
馬上看到網頁噴
the plain http request was sent to https port
看到這訊息,是 Nginx 產生的,error code 是 497,其實將此 error 導向正確的地方就可以了,在 Nginx 設定檔加入
error_page 497 https://$host$request_uri;
就可以解決此問題了。
Ref:
The plain HTTP request was sent to HTTPS port
Forcing SSL with phpMyAdmin