共用方式為


使用自動 Proxy 設定文稿將效能最佳化 (PAC)

警告

已淘汰、不受支援的 Internet Explorer 11 傳統型應用程式已於特定 Windows 10 版本透過 Microsoft Edge 更新永久停用。 如需詳細資訊,請參閱 Internet Explorer 11 傳統型應用程式淘汰常見問題集 (英文)。

本主題說明如何優化自動 Proxy 組態腳本的效能(也稱為 Wpad.dat PAC 檔案)。 本檔著重於如何透過 Proxy-server 直接解決內部網路伺服器和外部內部流量的問題。

如需有關用於評估位址(URL 或主機名稱)的函式的詳細資訊,請參閱:使用 IEAK 11 的代理伺服器自動設定 (.pac) 檔案

注意

isInNet()isResolvable()dnsResolve()函數會將查詢傳送至 DNS 子系統。 因此,您應該避免,或至少減少使用這些函式。

查詢 NetBIOS 名稱

NetBIOS 名稱(不含句點的伺服器名稱)只會用於內部網路,而且不會透過 Proxy 路由傳送:

if (isPlainHostName(host))
    return "DIRECT";

查詢內部 DNS 後綴

內部使用的 DNS 區域通常會直接路由傳送。 判斷這類主機最簡單的方式是使用 函 dnsDomainis 式:

if (dnsDomainIs(host, ".dns.company.com"))
    return "DIRECT";

使用ShExMatch()即可獲得相同結果的替代方案和更快速的方法。 此函式會執行字串比較。 它會傳回相同的結果,但新增星號 \ 作為通配符:

if (shExpMatch(host, "*.dns.company.com"))
    return "DIRECT";

查詢IP範圍

如果主機的IP位址屬於本機內部網路,不論Web伺服器名稱為何,它都應該略過 Proxy才能直接巡覽。

如果IP位址直接輸入到網址列中,您就不需要再次加以解析。 您可以使用下列程式代碼來檢查主機是否已經採用 IP 位址格式:

var isIpV4Addr = /^(\d+.){3}\d+$/;
  ret = isIpV4Addr.test(host);

此程式代碼會檢查變數主機地址是否包含三個數字,後面接著句號,然後再接著另一個數字。 結果接著會傳遞至 return 變數。 如果是 IP,結果為「true」,否則為「false」。

在下列代碼段中,變數主機IP包含在未來檢查中使用的IP位址:

var hostIP;
var isIpV4Addr = /^(\d+.){3}\d+$/;
if (isIpV4Addr.test(host))
    hostIP = host;
else
    hostIP = dnsResolve(host);

當非現有主機傳遞至函式時(例如,使用者在網址列中輸入錯誤),主機 IP 的結果可能為零。 代理伺服器會執行任何其他錯誤處理。

if (hostIP==0)
    return "PROXY myproxy:80";

因為我們有主機的IP位址,因此必須檢查內部IP範圍。 儘可能使用 shExpMatch 功能,而不是 isInNet。 下列代碼段的結果完全相同,但 shExpMatch 執行速度較快:

if (isInNet(hostIP, "95.53.0.0", "255.255.0.0"))
    return "DIRECT";
if (shExpMatch(hostIP, "95.53.*"))
    return "DIRECT";

JavaScript 區分大小寫

Proxy 腳本會使用JavaScript語言。 JavaScript 會區分大小寫。 因此, if 大寫的 子句永遠不會變成 true,而其他參數則使用小寫。 Internet Explorer 本身會在函式 host 呼叫之前,將變數 urlFindProxyForURL 轉換成小寫。

此條件不適用於 WinHTTP。 這是因為 WinHTTP 會將 hosturl 直接傳遞至函式。

因此,在評估 PAC 檔案之前,應該先在 PAC 檔案內轉換檢查的參數:

host = host.toLowerCase();

使用 IPv6

如果您想要使用及處理 IPv6 位址,Internet Explorer 支援它們,因為 Internet Explorer 包含在目前支援的每個 Windows 版本中(以及自 Windows Vista 以來的 WinHTTP 中)。 不過,在此情況下,您必須使用 「Ex」 函式(例如 isInNetEx()),如下列文章所述:

IPv6 感知 Proxy 協助 API 定義

如需實作的 myIpAddressEx 範例,請參閱 「myIpAddress」 函式在 Internet Explorer 9 中傳回不正確的結果。

測試 PAC 檔案

如果文稿包含任何語法錯誤(例如語句中 if 遺漏的 “)” 字元,則不會執行腳本。 若要將錯誤降到最低,請考慮使用執行語法檢查的腳本編輯器。 藉由使用 Visual Studio,您可以在編輯期間將 PAC 檔案的擴展名重新命名為 「.js」 但在將它上傳至 Web 伺服器之前,請將它重新命名回 「.pac」。

注意

從 Windows 10 開始,您無法再使用檔案型 PAC 檔案。 如需詳細資訊,請參閱下列文章:

使用 Autoprox.exe 進行測試

有時候,即使您沒有網站的存取權,您仍必須測試 PAC 檔案。 若要這樣做,您可以使用 Autoprox.exe命令行工具

如果您在命令內開啟工具而不使用其他參數,則會傳回下列具有說明的輸出:

C:\temp>autoprox
Help for AUTOPROX.EXE
Version : 2.44 (12/16/2019)
Usage : AUTOPROX -a  (calling DetectAutoProxyUrl and saving wpad.dat file in temporary file if success)
Usage : AUTOPROX -n  (calling DetectAutoProxyUrl with PROXY_AUTO_DETECT_TYPE_DNS_A only and saving wpad.dat file in temporary file if success)
Usage : AUTOPROX  [-o] [-d] [-v] [-u:url] [-p:Path to autoproxy file] [-i:IP address]
      -o: calls InternetInitializeAutoProxyDll with helper functions implemented in AUTOPROX
       -i:IP Address: calls InternetInitializeAutoProxyDll with helper functions implemented in AUTOPROX and using provided IP Address
       -v: verbose output for helper functions
For debugging: -d plus HKEY_CURRENT_USER\Software\Microsoft\Windows Script\Settings\JITDebug=1
AUTOPROX -u:url: calling DetectAutoProxyUrl and using autoproxy file to find the proxy for the url
AUTOPROX -u:url -p:path: using the autoproxy file/url from the path to find proxy for the url
Example: autoprox http://www.microsoft.com -> calling DetectAutoProxyUrl and using WPAD if found
Example: autoprox -o -u:http://www.microsoft.com -p:c:\inetpub\wwwroot\wpad.dat
Example: autoprox -u:http://www.microsoft.com -p:http://proxy/wpad.dat
Example: autoprox -d -u:http://www.microsoft.com -p:http://proxy/wpad.dat

以下是使用我們樣本時的輸出:

C:\temp>autoprox -u:https://us.msn.com -p:c:\temp\sample.pac
Searching proxy for url : https://us.msn.com
Searching proxy using file : c:\temp\sample.pac
The Winsock 2.2 dll was found okay
Calling InternetInitializeAutoProxyDll with c:\temp\sample.pac
        Calling InternetGetProxyInfo for url https://us.msn.com and host us.msn.com
        Proxy returned for url https://us.msn.com is:
PROXY myproxy:80;

Autoprox.exe 中的錯誤處理

如果 PAC 檔案包含語法錯誤,您會收到下列訊息:

錯誤:InternetGetProxyInfo 失敗,錯誤號碼0x3eb 1003。

完成本機測試之後,您應該將 PAC 檔案複製到要透過 HTTP 通訊協定存取的網頁伺服器。

例如:

function FindProxyForURL(url, host) {
    // NetBIOS-names
    if (isPlainHostName(host))
        return "DIRECT";
    // change to lower case, if not already been done
    host = host.toLowerCase();
    // internal DNS-suffixes
    if (shExpMatch(host, "*.corp.company.com") ||
        shExpMatch(host, "*.dns.company.com"))
        return "DIRECT";
    // Save the IP-address to variable hostIP
    var hostIP;
    var isIpV4Addr = /^(\d+.){3}\d+$/;
    if (isIpV4Addr.test(host))
        hostIP = host;
    else
        hostIP = dnsResolve(host);
    // IP could not be determined -> go to proxy
    if (hostIP == 0)
        return "PROXY myproxy:80";
    // These 3 scopes are used only internally
    if (shExpMatch(hostIP, "95.53.*") ||
        shExpMatch(hostIP, "192.168.*") ||
        shExpMatch(hostIP, "127.0.0.1"))
        return "DIRECT";
    // Eveything else goes through the proxy
    return "PROXY myproxy:80;";
}

協力廠商連絡資訊免責聲明

Microsoft 提供協力廠商連絡資訊,以協助您尋找有關此主題的其他資訊。 此連絡資訊可能會變更而不另行通知。 Microsoft 不保證協力廠商連絡資訊的準確性。