最近發生 PHP 呼叫其他 WebService 速度過慢的問題,在 Linux OS 使用 TCP Socket 進行 HTTP 連線速度正常,但是使用 PHP 呼叫 fsockopen() 或 file_get_contents() 就是特別慢。仔細測試後發現如果是使用 IP Address 進行連線速度就正常,使用 Domain 就特別慢,看來問題就是出在 DNS 解析!
後來發現 Linux 的 IPv6 是不工作的,一般來說裝好 Linux 我都會關閉 IPv6 的相關功能與設定,接著關閉 IPv6 之後 PHP 的連線速度也就正常了。以下是關閉 Linux IPv6 的方法:
Disable IPv6 Networking Support - 關閉 IPv6 網路支援
編輯 /etc/sysconfig/network 並加入以下設定
1 |
NETWORKING_IPV6=no |
Disable IPv6 Protocol Stack for Kernel - 關閉 IPv6 相關 module
編輯 /etc/modprobe.conf 並加入以下設定
1
2
|
alias net-pf-10 off
alias ipv6 off
|
Disable IPv6 Firewall - 關閉 IPv6 iptables 防火牆支援
1
2
3
4
5
6
7
|
[root@server ~]$ /etc/init.d/ip6tables stop
Flushing firewall rules: [ OK ]
Setting chains to policy ACCEPT: filter [ OK ]
Unloading ip6tables modules: [ OK ]
Turn ipv6 firewall off during future usage:
[root@server ~]$ /sbin/chkconfig --level 35 ip6tables off
|
Issue with localhost - 註解所有 IPv6 Hosts
編輯 /etc/hosts 註解所有 IPv6 解析設定
1 |
#::1 localhost.localdomain localhost |
參考資料