# Apache模塊 mod_proxy
| [說明](#calibre_link-11) | 提供HTTP/1.1的代理/網關功能支持 |
| --- | --- |
| [狀態](#calibre_link-12) | 擴展(E) |
| [模塊名](#calibre_link-13) | proxy_module |
| [源文件](#calibre_link-14) | mod_proxy.c |
### 概述
### 警告
在您沒有對服務器[采取安全措施](#calibre_link-290)之前,請不要用`ProxyRequests`啟用代理。一個開放的代理服務器不僅對您的網絡有威脅,對整個因特網來說也同樣如此。
此模塊實現了Apache的代理/網關。它實現了以下規范的代理:`AJP13`(Apache JServe Protocol v1.3), `FTP`, `CONNECT`(用于SSL), `HTTP/0.9`, `HTTP/1.0`, `HTTP/1.1` 。此模塊經配置后可用上述或其它協議連接其它代理模塊。
Apache的代理功能(除`mod_proxy`以外)被劃分到了幾個不同的模塊中:`mod_proxy_http`, `mod_proxy_ftp`, `mod_proxy_ajp`, `mod_proxy_balancer`, `mod_proxy_connect` 。這樣,如果想使用一個或多個代理功能,就必須將`mod_proxy`_和_對應的模塊同時加載到服務器中(靜態連接或用`LoadModule`動態加載)。
另外,其它模塊還提供了擴展特性。`mod_cache`及其相關模塊提供了緩沖特性。`mod_ssl`提供的`SSLProxy*`系列指令可以使用SSL/TLS連接遠程服務器。這些提供擴展特性的模塊必須在被正確加載和配置以后才能提供這些擴展功能。
## 正向和反向代理
Apache可以被配置為<dfn class="calibre27">正向(forward)</dfn>和<dfn class="calibre27">反向(reverse)</dfn>代理。
<dfn class="calibre27">正向代理</dfn>是一個位于客戶端和_原始服務器(origin server)_之間的服務器,為了從原始服務器取得內容,客戶端向代理發送一個請求并指定目標(原始服務器),然后代理向原始服務器轉交請求并將獲得的內容返回給客戶端。客戶端必須要進行一些特別的設置才能使用正向代理。
正向代理的典型用途是為在防火墻內的局域網客戶端提供訪問Internet的途徑。正向代理還可以使用緩沖特性(由`mod_cache`提供)減少網絡使用率。
使用`ProxyRequests`指令即可激活正向代理。因為正向代理允許客戶端通過它訪問任意網站并且隱藏客戶端自身,因此你必須[采取安全措施](#calibre_link-290)以確保僅為經過授權的客戶端提供服務。
<dfn class="calibre27">反向代理</dfn>正好相反,對于客戶端而言它就像是原始服務器,并且客戶端不需要進行任何特別的設置。客戶端向反向代理的名字空間(name-space)中的內容發送普通請求,接著反向代理將判斷向何處(原始服務器)轉交請求,并將獲得的內容返回給客戶端,就像這些內容原本就是它自己的一樣。
反向代理的典型用途是將防火墻后面的服務器提供給Internet用戶訪問。反向代理還可以為后端的多臺服務器提供負載平衡,或為后端較慢的服務器提供緩沖服務。另外,還可以啟用高級URL策略和管理技術,從而使處于不同web服務器系統的web頁面同時存在于同一個URL空間下。
可以使用`ProxyPass`指令激活反向代理(在`RewriteRule`指令中使用`P]`標記也可以)。配置反向代理并**不需要**打開`[ProxyRequests`指令。
## 簡單示例
下面的例子僅僅是為了給你一個基本概念而幫助入門而已,請仔細閱讀每個指令的文檔。
另外,如果想使用緩沖特性,請查看`mod_cache`文檔。
### 正向代理
```
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Deny from all
Allow from internal.example.com
</Proxy>
```
### 反向代理
```
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /foo http://foo.example.com/bar
ProxyPassReverse /foo http://foo.example.com/bar
```
## 控制對代理服務器的訪問
您可以通過`<Proxy>`的阻止功能來控制誰能訪問您的代理。示例如下:
```
<Proxy *>
Order Deny,Allow
Deny from all
Allow from 192.168.0
</Proxy>
```
要了解更多訪問控制信息,請參見`mod_authz_host`文檔。
使用正向代理時嚴格控制訪問權限(使用`ProxyRequests`指令)是非常重要的。否則你的代理會被客戶端利用來訪問其它服務器并且隱藏客戶端的真實身份。這不僅對您的網絡有威脅,對整個因特網來說也同樣如此。當使用反向代理(在"`ProxyRequests Off`"條件下使用`ProxyPass`指令)的時候訪問控制要相對寬松,因為客戶端只能連接你配置的特定主機。
## 緩慢啟動
如果您使用了`ProxyBlock`指令,將會在啟動時查找并緩存主機名的IP地址以備后繼的匹配測試使用。這將會花費幾秒或更長的時間,這主要取決于主機名查找的速度。
## 局域網代理
位于局域網內的Apache代理服務器需要經由公司的防火墻轉發對外部的請求(使用`ProxyRemote`指令來配置)。但當它訪問局域網內的資源時,它能越過防火墻直接訪問目的主機。在訪問一個屬于局域網的服務器從而進行直接連接時,`NoProxy`指令就會很有用。
局域網內的用戶習慣于不在他們的WWW請求中加入本地域的名稱,于是會使用"http://somehost/"來取代`http://somehost.example.com/` 。一些商業代理服務器會不管這些,只是采用本地域的配置來簡單的伺服這個請求。當使用了`ProxyDomain`指令來為服務器[配置了一個代理服務](#calibre_link-407)時,Apache會發出一個重定向應答,以使客戶端請求到達正確的、能滿足要求的服務器地址。因為這樣一來,用戶的書簽文件就會隨之包含完整的主機名,所以這是首選的方法。
## 協議調整
當`mod_proxy`向一個沒有正確實現持久連接(KeepAlive)或HTTP/1.1的原始服務器發送請求的時候,可以通過設置兩個[環境變量](#calibre_link-232)來發送不帶持久連接(KeepAlive)的HTTP/1.0請求。這兩個變量是通過`SetEnv`指令設置的。
以下是`force-proxy-request-1.0`和`proxy-nokeepalive`的例子:
```
<Location /buggyappserver/>
ProxyPass http://buggyappserver:7001/foo/
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
</Location>
```
## 請求體
一些請求方法(如POST)包含一個請求體。HTTP協議要求包含請求體的請求或者使用塊傳輸編碼(chunked transfer encoding)或者包含一個`Content-Length`請求頭。當將這種請求傳遞給原始服務器的時候,`mod_proxy_http`會始終嘗試使用`Content-Length`請求頭。但如果原始請求使用的是塊編碼,那么塊編碼也同樣可以用于上行請求。可以使用[環境變量](#calibre_link-232)控制這種選擇。設置`proxy-sendcl`可以確保始終發送`Content-Length`頭以與上游服務器保持最大程度的兼容性,而設置`proxy-sendchunked`可以通過繼續使用塊編碼以盡可能最小化資源占用率。
## AllowCONNECT 指令
| [說明](#calibre_link-18) | 通過代理允許`CONNECT`的端口號 |
| --- | --- |
| [語法](#calibre_link-19) | `AllowCONNECT port [port] ...` |
| [默認值](#calibre_link-24) | `AllowCONNECT 443 563` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
`AllowCONNECT`指令指定了此代理的`CONNECT`方法可以連接的端口號列表。當今的瀏覽器在進行`https`連接請求時使用這種方法,而代理默認會將其轉為`http`。
默認只啟用了默認的https端口(`443`)和默認的snews端口(`563`)。使用`AllowCONNECT`指令可以覆蓋默認設置而改為僅允許連接列出的端口。
注意,必須確保`mod_proxy_connect`也同時存在于服務器中,這樣才能支持`CONNECT` 。
## NoProxy 指令
| [說明](#calibre_link-18) | 直接進行連接的主機/域/網絡 |
| --- | --- |
| [語法](#calibre_link-19) | `NoProxy host [host] ...` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
此指令僅適用于局域網內的Apache代理服務器。`NoProxy`指令指定了一個中間以空格分隔的子網、IP地址、主機和/或域的列表。對某個匹配上述一個或多個列表項的主機的請求將直接被其伺服而不會轉交到配置好的`ProxyRemote`代理服務器。
### 示例
```
ProxyRemote * http://firewall.mycompany.com:81
NoProxy .mycompany.com 192.168.112.0/21
```
`NoProxy`指令的host參數可以是以下選項之一:
域
<dfn class="calibre27">域</dfn>是一個DNS域名的一部分,并在前面加上點號。它表示一批邏輯上屬于同一個DNS區域的主機,也就是所有這些主機名具有相同的后綴,而這個"后綴"就是<dfn class="calibre27">域</dfn>。
### 例子
```
.com
.apache.org.
```
域和[主機名](#calibre_link-417)(一個DNS域甚至也可能有一條DNS"A記錄"!)的不同之處在于域始終有一個前導點。
### 注意
域名不區分大小寫并且始終認為是錨定在DNS樹根上的,因此`.MyDomain.com`和`.mydomain.com.`(注意結尾點號)是完全等同的。因為域的比較不需要進行DNS查詢,因此它比子網比較更加高效。
子網
<dfn class="calibre27">子網</dfn>以點分十進制形式表示了一個因特網地址的一部分,有時會跟著一個斜杠和子網掩碼,以指定子網中的有效bit位。它用于表示主機通過自身的普通網絡接口可以訪問的子網范圍。未指定子網掩碼的時候就假定忽略掉的(或為零的)結尾數字就是掩碼,在這種情況下,掩碼bit長度必須是8bit的整數倍。例如:
`192.168`或`192.168.0.0`
子網"`192.168.0.0`"表示掩碼為16bit(有時也用`255.255.0.0`表示)。
`192.168.112.0/21`
子網"`192.168.112.0/21`"表示掩碼為21bit(有時也用`255.255.248.0`表示)。
在退化到極限的情況下,一個掩碼為32bit的_子網_就等價于一個[IP地址](#calibre_link-418)。而零個合法bit的子網("0.0.0.0/0")等價于常量"_Default_",可以匹配任何IP地址。
IP地址
<dfn class="calibre27">IP地址</dfn>以點分十進制形式表示了一個完整的因特網地址。一般來說,此地址代表一個主機,但并不需要一個DNS域名與這個地址對應。
### 示例
```
192.168.123.7
```
### 注意
一個IP地址不需要為一個DNS系統所解析,所以它能使apache獲取更高性能。
主機名
<dfn class="calibre27">主機名</dfn>是一個完整的DNS域名,可以通過DNS域名服務解析為一個或多個[IP地址](#calibre_link-419)。它代表了一個邏輯主機(與[域](#calibre_link-420)相反)而且必須解析成至少一個[IP地址](#calibre_link-419)(或經常解析成具有不同[IP地址](#calibre_link-419)的主機列表)。
### 例子
```
prep.ai.mit.edu
www.apache.org
```
### 注意
在很多情況下,指定一個[IP地址](#calibre_link-419)代替主機名會更有效率。因為可以避免一次DNS查詢。當使用一個低速的PPP與域名服務器連接時,Apache的域名解析會花費相當可觀的時間。
主機名不區分大小寫并且始終認為是錨定在DNS樹根上的,因此`WWW.MyDomain.com`和`www.mydomain.com.`(注意結尾點號)是完全等同的。
### 參見
* [DNS相關問題](#calibre_link-55)
## <Proxy> 指令
| [說明](#calibre_link-18) | 應用于所代理資源的容器 |
| --- | --- |
| [語法](#calibre_link-19) | `<Proxy wildcard-url> ...</Proxy>` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
位于`<Proxy>`配置段中的指令僅作用于匹配的代理內容。語句中可以使用shell風格的通配符。
比如說:下例僅允許`yournetwork.example.com`中的主機通過您的代理服務器訪問代理內容:
```
<Proxy *>
Order Deny,Allow
Deny from all
Allow from yournetwork.example.com
</Proxy>
```
下例將在所有`example.com`的`foo`目錄下的文件通過代理服務器發送之前用`INCLUDES`過濾器進行處理:
```
<Proxy http://example.com/foo/*>
SetOutputFilter INCLUDES
</Proxy>
```
## ProxyBadHeader 指令
| [說明](#calibre_link-18) | 確定如何處理不合法的應答頭 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyBadHeader IsError|Ignore|StartBody` |
| [默認值](#calibre_link-24) | `ProxyBadHeader IsError` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
| [兼容性](#calibre_link-137) | 僅在 Apache 2.0.44 及以后的版本中可用 |
`ProxyBadHeader`指令決定`mod_proxy`如何處理不合法的應答頭(比如丟失冒號(:))。參數的取值范圍如下:
`IsError`
以"502"(Bad Gateway)應答中止請求。這是默認行為。
`Ignore`
忽略,就像它們不存在一樣。
`StartBody`
在接收到第一個非法頭行時停止讀取頭,并將剩余部分當作應答體。這樣做有助于和一個不規范的、經常忘記在應答頭和應答體之間插入空行的后端服務器協同工作。
## ProxyBlock 指令
| [說明](#calibre_link-18) | 設置被代理屏蔽的語句、主機、域 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyBlock *|word|host|domain [word|host|domain] ...` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
`ProxyBlock`指令指定了一個由空格分隔的語句、主機和/或域的列表。對所有匹配這些語句、主機和/或域的HTTP、HTTPS、FTP文檔的請求都將被代理服務器_阻斷_。代理模塊亦會在啟動時嘗試確定列表中可能是主機名的項目對應的IP地址,并將其緩沖用于匹配測試。比如說:
### 示例
```
ProxyBlock joes-garage.com some-host.co.uk rocky.wotsamattau.edu
```
通過IP地址,`rocky.wotsamattau.edu`將可能同樣被匹配。
請注意,`wotsamattau`已經足夠匹配`wotsamattau.edu`了。
請注意
```
ProxyBlock *
```
將屏蔽對所有站點的連接。
## ProxyDomain 指令
| [說明](#calibre_link-18) | 代理請求的默認域名 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyDomain Domain` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
此指令僅對位于局域網內的Apache代理服務器有用。`ProxyDomain`指令指定了apache代理服務器歸屬的默認域。如果遇到了一個對沒有域名的主機的請求,就會根據配置自動生成一個加上了Domain的重定向應答。
### 示例
```
ProxyRemote * http://firewall.mycompany.com:81
NoProxy .mycompany.com 192.168.112.0/21
ProxyDomain .mycompany.com
```
## ProxyErrorOverride 指令
| [說明](#calibre_link-18) | 覆蓋代理內容的錯誤頁 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyErrorOverride On|Off` |
| [默認值](#calibre_link-24) | `ProxyErrorOverride Off` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
| [兼容性](#calibre_link-137) | 僅在 Apache 2.0 及以后的版本中可用 |
此指令用于反向代理設置中您想為最終用戶提供觀感一致的錯誤頁面時。它也同樣允許包含文件(通過`mod_include`的SSI)獲取錯誤號并作出相應的動作。(默認行為是顯示被代理的服務器的錯誤頁面,將此項目設為"On"將顯示SSI錯誤信息。)
## ProxyIOBufferSize 指令
| [說明](#calibre_link-18) | 內部緩沖區大小 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyIOBufferSize bytes` |
| [默認值](#calibre_link-24) | `ProxyIOBufferSize 8192` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
`ProxyIOBufferSize`指令用于調整內部緩沖區(作為輸入輸出數據的暫存器)的大小。取值必須小于等于`8192` 。
在絕大多數情況下,不需要調整這個設置。
## <ProxyMatch> 指令
| [說明](#calibre_link-18) | 應用于匹配正則表達式的代理資源的容器 |
| --- | --- |
| [語法](#calibre_link-19) | `<ProxyMatch regex> ...</ProxyMatch>` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
`<ProxyMatch>`和`<Proxy>`指令基本相同,只是匹配字符串可以為[正則表達式](#calibre_link-67 "see glossary")。
## ProxyMaxForwards 指令
| [說明](#calibre_link-18) | 轉發請求的最大代理數目 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyMaxForwards number` |
| [默認值](#calibre_link-24) | `ProxyMaxForwards 10` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
| [兼容性](#calibre_link-137) | 僅在 Apache 2.0 及以后的版本中可用 |
`ProxyMaxForwards`指令指定了允許轉發請求的最大代理數目。這個設置是為了避免無限代理循環或DoS攻擊的發生。
### 示例
```
ProxyMaxForwards 15
```
## ProxyPass 指令
| [說明](#calibre_link-18) | 將一個遠端服務器映射到本地服務器的URL空間中 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyPass [path] !|url [key=value key=value ...]]` |
| [作用域](#calibre_link-20) | server config, virtual host, directory |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
該指令允許你將一個遠端服務器映射到本地服務器的URL空間中,此時本地服務器并不充當代理角色,而是充當遠程服務器的一個鏡像。path是一個本地虛擬路徑名,url是一個指向遠程服務器的部分URL,并且不允許包含查詢字符串。
當使用`ProxyPass`指令時,`ProxyRequests`指令通常應當被設為 **off** 。
假設本地服務器地址是:`http://example.com/` ,那么,
```
ProxyPass /mirror/foo/ http://backend.example.com/
```
將會導致對`http://example.com/mirror/foo/bar`的本地請求將會在內部轉換為一個代理請求:`http://backend.example.com/bar` 。
"`!`"指令對于您不想對某個子目錄進行反向代理時很有用。比如說:
```
ProxyPass /mirror/foo/i !
ProxyPass /mirror/foo http://backend.example.com
```
將會代理除`/mirror/foo/i`之外的所有對`backend.example.com`下`/mirror/foo`的請求。
### 注意
順序很重要,您需要把拒絕指令放置在普通`ProxyPass`指令_之前_。
As of Apache 2.1, the ability to use pooled connections to a backend server is available. Using the `key=value` parameters it is possible to tune this connection pooling. The default for a `Hard Maximum` for the number of connections is the number of threads per process in the active MPM. In the Prefork MPM, this is always 1, while with the Worker MPM it is controlled by the `ThreadsPerChild`.
Setting `min` will determine how many connections will always be open to the backend server. Upto the Soft Maximum or `smax` number of connections will be created on demand. Any connections above `smax` are subject to a time to live or `ttl`. Apache will never create more than the Hard Maximum or `max` connections to the backend server.
```
ProxyPass /example http://backend.example.com smax=5 max=20 ttl=120 retry=300
```
| Parameter | Default | Description |
| --- | --- | --- |
| min | 0 | Minumum number of connections that will always be open to the backend server. |
| max | 1...n | Hard Maximum number of connections that will be allowed to the backend server. The default for a Hard Maximum for the number of connections is the number of threads per process in the active MPM. In the Prefork MPM, this is always 1, while with the Worker MPM it is controlled by the `ThreadsPerChild`. Apache will never create more than the Hard Maximum connections to the backend server. |
| smax | max | Upto the Soft Maximum number of connections will be created on demand. Any connections above `smax` are subject to a time to live or `ttl`. |
| ttl | - | Time To Live for the inactive connections above the `smax` connections in seconds. Apache will close all connections that has not been used inside that time period. |
| timeout | `Timeout` | Connection timeout in seconds. If not set the Apache will wait until the free connection is available. This directive is used for limiting the number of connections to the backend server together with `max` parameter. |
| acquire | - | If set this will be the maximum time to wait for a free connection in the connection pool. If there are no free connections in the pool the Apache will return `SERVER_BUSY` status to the client. |
| keepalive | Off | This parameter should be used when you have a firewall between your Apache and the backend server, who tend to drop inactive connections. This flag will tell the Operating System to send `KEEP_ALIVE` messages on inactive connections (interval depends on global OS settings, generally 120ms), and thus prevent the firewall to drop the connection. To enable keepalive set this property value to `On`. |
| retry | 60 | Connection pool worker retry timeout in seconds. If the connection pool worker to the backend server is in the error state, Apache will not forward any requests to that server until the timeout expires. This enables to shut down the backend server for maintenance, and bring it back online later. |
| loadfactor | 1 | Worker load factor. Used with BalancerMember. It is a number between 1 and 100 and defines the normalized weighted load applied to the worker. |
| route | - | Route of the worker when used inside load balancer. The route is a value appended to seesion id. |
| redirect | - | Redirection Route of the worker. This value is usually set dynamically to enable safe removal of the node from the cluster. If set all requests without session id will be redirected to the BalancerMember that has route parametar equal as this value. |
If the Proxy directive scheme starts with the `balancer://` then a virtual worker that does not really communicate with the backend server will be created. Instead it is responsible for the management of several "real" workers. In that case the special set of parameters can be add to this virtual worker.
| Parameter | Default | Description |
| --- | --- | --- |
| lbmethod | - | Balancer load-balance method. Select the load-balancing scheduler method to use. Either `byrequests`, to perform weighted request counting or `bytraffic`, to perform weighted traffic byte count balancing. Default is `byrequests`. |
| stickysession | - | Balancer sticky session name. The value is usually set to something like `JSESSIONID`或`PHPSESSIONID`, and it depends on the backend application server that support sessions. |
| nofailover | Off | If set to `On` the session will break if the worker is in error state or disabled. Set this value to On if backend servers do not support session replication. |
| timeout | 0 | Balancer timeout in seconds. If set this will be the maximum time to wait for a free worker. Default is not to wait. |
| maxattempts | 1 | Maximum number of failover attempts before giving up. |
```
ProxyPass /special-area http://special.example.com/ smax=5 max=10
ProxyPass / balancer://mycluster stickysession=jsessionid nofailover=On
<Proxy balancer://mycluster>
BalancerMember http://1.2.3.4:8009
BalancerMember http://1.2.3.5:8009 smax=10
# Less powerful server, don't send as many requests there
BalancerMember http://1.2.3.6:8009 smax=1 loadfactor=20
</Proxy>
```
When used inside a `<Location>` section, the first argument is omitted and the local directory is obtained from the `<Location>`.
If you require a more flexible reverse-proxy configuration, see the `RewriteRule` directive with the `[P]` flag.
## ProxyPassReverse 指令
| [說明](#calibre_link-18) | 調整由反向代理服務器發送的HTTP應答頭中的URL |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyPassReverse [path] url` |
| [作用域](#calibre_link-20) | server config, virtual host, directory |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
此指令使Apache調整HTTP重定向應答中`Location`, `Content-Location`, `URI`頭里的URL。這樣可以避免在Apache作為反向代理使用時,后端服務器的HTTP重定向造成的繞過反向代理的問題。
只有明確指定的應答頭會被重寫,其它應答頭保持不變,并且HTML頁面中的URL也不會被修改。如果被代理的內容包含絕對URL引用,那么將會繞過代理。有一個第三方模塊可以檢查并改寫HTML中的URL引用,該模塊就是Nick Kew編寫的[mod_proxy_html](http://apache.webthing.com/mod_proxy_html/)。
path是本地虛擬路徑的名稱。url是遠端服務器的部分URL。與`ProxyPass`指令中的使用方法相同。
例如,假定本地服務器擁有地址`http://example.com/` ,那么
```
ProxyPass /mirror/foo/ http://backend.example.com/
ProxyPassReverse /mirror/foo/ http://backend.example.com/
ProxyPassReverseCookieDomain backend.example.com public.example.com
ProxyPassReverseCookiePath / /mirror/foo/
```
不僅會把所有對`http://example.com/mirror/foo/bar`的請求直接轉換為對`http://backend.example.com/bar`的代理請求(由`ProxyPass`提供的功能),它還會重定向服務器`backend.example.com`的發送:當`http://backend.example.com/bar`被它重定向到`http://backend.example.com/quux`時,Apache會在轉交HTTP重定向應答到客戶端之前調整它為`http://example.com/mirror/foo/quux` 。注意:被用于構建URL的主機名與`UseCanonicalName`指令的設置有關。
注意,此`ProxyPassReverse`指令亦可與`mod_rewrite`的代理穿透特性(`RewriteRule ... [P]`)聯用。因為它不依賴于相應的`ProxyPass`指令。
當在`<Location>`配置段中使用時,第一個參數會被忽略而采用由`<Location>`指令指定的本地目錄。
## ProxyPassReverseCookieDomain 指令
| [說明](#calibre_link-18) | Adjusts the Domain string in Set-Cookie headers from a reverse- proxied server |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyPassReverseCookieDomain internal-domain public-domain` |
| [作用域](#calibre_link-20) | server config, virtual host, directory |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
Usage is basically similar to `ProxyPassReverse`, but instead of rewriting headers that are a URL, this rewrites the `domain` string in `Set-Cookie` headers.
## ProxyPassReverseCookiePath 指令
| [說明](#calibre_link-18) | Adjusts the Path string in Set-Cookie headers from a reverse- proxied server |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyPassReverseCookiePath internal-path public-path` |
| [作用域](#calibre_link-20) | server config, virtual host, directory |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
Usage is basically similar to `ProxyPassReverse`, but instead of rewriting headers that are a URL, this rewrites the `path` string in `Set-Cookie` headers.
## ProxyPreserveHost 指令
| [說明](#calibre_link-18) | 使用進入的HTTP請求頭來發送代理請求 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyPreserveHost On|Off` |
| [默認值](#calibre_link-24) | `ProxyPreserveHost Off` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
| [兼容性](#calibre_link-137) | 僅在 Apache 2.0.31 及以后的版本中可用 |
當啟用時,此選項將把傳入請求的"Host:"行傳遞給被代理的主機,而不是傳遞在`ProxyPass`中指定的主機名。
此選項一般為`Off`狀態。It is mostly useful in special configurations like proxied mass name-based virtual hosting, where the original Host header needs to be evaluated by the backend server.
## ProxyReceiveBufferSize 指令
| [說明](#calibre_link-18) | 代理HTTP和FTP連接的接收緩沖區大小(字節) |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyReceiveBufferSize bytes` |
| [默認值](#calibre_link-24) | `ProxyReceiveBufferSize 0` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
`ProxyReceiveBufferSize`指令為增加的吞吐量指定了代理HTTP和FTP連接的(TCP/IP)網絡接收緩沖區。這個值必須大于`512` ,或設置為"`0`"表示使用系統默認的緩沖大小。
### 示例
```
ProxyReceiveBufferSize 2048
```
## ProxyRemote 指令
| [說明](#calibre_link-18) | 用于處理某些特定請求的遠端代理 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyRemote match remote-server` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
此指令定義了此代理的遠端代理。match可以是遠端服務器支持的URL形式的名稱、或是遠端服務器使用的部分URL、或是代表服務器可以接受所有請求的"`*`"。remote-server是遠端服務器的部分URL。語法為:
```
<dfn class="calibre40">remote-server</dfn> = <var class="calibre40">scheme</var>://<var class="calibre40">hostname</var>[:<var class="calibre40">port</var>]
```
scheme是與遠端服務器交換信息時使用的協議;本模塊暫時只支持`http`協議。
### 示例
```
ProxyRemote http://goodguys.com/ http://mirrorguys.com:8000
ProxyRemote * http://cleversite.com
ProxyRemote ftp http://ftpproxy.mydomain.com:8080
```
在最后一個例子中,代理會將封裝到另外一個HTTP代理請求中的FTP請求轉交到另外一個能處理它們的代理去。
此選項也支持反向代理配置:一個后端web服務器可以被嵌入到一個虛擬主機的URL空間中,哪怕它是由另一個代理轉交過來的。
## ProxyRemoteMatch 指令
| [說明](#calibre_link-18) | 處理匹配正則表達式的請求的遠端代理 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyRemoteMatch regex remote-server` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
`ProxyRemoteMatch`與`ProxyRemote`令基本相同。除了第一個參數是由一個請求的URL變成了匹配的[正則表達式](#calibre_link-67 "see glossary")。
## ProxyRequests 指令
| [說明](#calibre_link-18) | 啟用正向(標準)代理請求 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyRequests On|Off` |
| [默認值](#calibre_link-24) | `ProxyRequests Off` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
此指令將允許或禁止Apache作為正向代理服務器的功能(設置為`Off`并不會禁用`ProxyPass`指令)。
在一個典型的反向代理配置中,此可選項一般設置為`Off`。
為了能夠代理HTTP或FTP站點,`mod_proxy_http`或`mod_proxy_ftp`必須同時存在于服務器中。
### 警告
在您沒有對服務器[采取安全措施](#calibre_link-290)之前,請不要用`ProxyRequests`啟用您的代理。一個開放的代理服務器不僅對您的網絡有威脅,對整個因特網來說也同樣如此。
## ProxyTimeout 指令
| [說明](#calibre_link-18) | 代理請求的網絡超時 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyTimeout seconds` |
| [默認值](#calibre_link-24) | `ProxyTimeout 300` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
| [兼容性](#calibre_link-137) | 僅在 Apache 2.0.31 及以后的版本中可用 |
此指令允許用戶對代理請求指定一個超時值。當你有一個很慢/錯誤多多的應用服務器經常掛起,而您寧愿返回一個超時的失敗信息也不愿意繼續等待不知道多久的時候,這個功能是很有用的。
## ProxyVia 指令
| [說明](#calibre_link-18) | 控制代理對`Via`應答頭的使用 |
| --- | --- |
| [語法](#calibre_link-19) | `ProxyVia On|Off|Full|Block` |
| [默認值](#calibre_link-24) | `ProxyVia Off` |
| [作用域](#calibre_link-20) | server config, virtual host |
| [狀態](#calibre_link-21) | 擴展(E) |
| [模塊](#calibre_link-22) | mod_proxy |
此指令控制代理對"`Via:`"頭的使用。它的目的是控制位于代理服務器鏈中的代理請求的流向。參閱[RFC 2616](http://www.ietf.org/rfc/rfc2616.txt)(HTTP/1.1)14.45小節以獲得關于"`Via:`"頭的解釋。
* 如果設置為默認值`Off` ,將不會采取特殊的處理。如果一個請求或應答包含"`Via:`"頭,將不進行任何修改而直接通過。
* 如果設置為`On`每個請求和應答都會對應當前主機得到一個"`Via:`"頭。
* 如果設置為`Full` ,每個產生的"`Via:`"頭中都會額外加入Apache服務器的版本,以"`Via:`"注釋域出現。
* 如果設置為`Block` ,每個代理請求中的所有"`Via:`"頭行都將被刪除。且不會產生新的"`Via:`"頭。
- Apache HTTP Server Version 2.2 文檔 [最后更新:2006年3月21日]
- 版本說明
- 從1.3升級到2.0
- 從2.0升級到2.2
- Apache 2.2 新特性概述
- Apache 2.0 新特性概述
- The Apache License, Version 2.0
- 參考手冊
- 編譯與安裝
- 啟動Apache
- 停止和重啟
- 配置文件
- 配置段(容器)
- 緩沖指南
- 服務器全局配置
- 日志文件
- 從URL到文件系統的映射
- 安全方面的提示
- 動態共享對象(DSO)支持
- 內容協商
- 自定義錯誤響應
- 地址和端口的綁定(Binding)
- 多路處理模塊
- Apache的環境變量
- Apache處理器的使用
- 過濾器(Filter)
- suEXEC支持
- 性能方面的提示
- URL重寫指南
- Apache虛擬主機文檔
- 基于主機名的虛擬主機
- 基于IP地址的虛擬主機
- 大批量虛擬主機的動態配置
- 虛擬主機示例
- 深入研究虛擬主機的匹配
- 文件描述符限制
- 關于DNS和Apache
- 常見問題
- 經常問到的問題
- Apache的SSL/TLS加密
- SSL/TLS高強度加密:緒論
- SSL/TLS高強度加密:兼容性
- SSL/TLS高強度加密:如何...?
- SSL/TLS Strong Encryption: FAQ
- 如何.../指南
- 認證、授權、訪問控制
- CGI動態頁面
- 服務器端包含入門
- .htaccess文件
- 用戶網站目錄
- 針對特定平臺的說明
- 在Microsoft Windows中使用Apache
- 在Microsoft Windows上編譯Apache
- Using Apache With Novell NetWare
- Running a High-Performance Web Server on HPUX
- The Apache EBCDIC Port
- 服務器和支持程序
- httpd - Apache超文本傳輸協議服務器
- ab - Apache HTTP服務器性能測試工具
- apachectl - Apache HTTP服務器控制接口
- apxs - Apache 擴展工具
- configure - 配置源代碼樹
- dbmmanage - 管理DBM格式的用戶認證文件
- htcacheclean - 清理磁盤緩沖區
- htdbm - 操作DBM密碼數據庫
- htdigest - 管理用于摘要認證的用戶文件
- httxt2dbm - 生成RewriteMap指令使用的dbm文件
- htpasswd - 管理用于基本認證的用戶文件
- logresolve - 解析Apache日志中的IP地址為主機名
- rotatelogs - 滾動Apache日志的管道日志程序
- suexec - 在執行外部程序之前切換用戶
- 其他程序
- 雜項文檔
- 與Apache相關的標準
- Apache模塊
- 描述模塊的術語
- 描述指令的術語
- Apache核心(Core)特性
- Apache MPM 公共指令
- Apache MPM beos
- Apache MPM event
- Apache MPM netware
- Apache MPM os2
- Apache MPM prefork
- Apache MPM winnt
- Apache MPM worker
- Apache模塊 mod_actions
- Apache模塊 mod_alias
- Apache模塊 mod_asis
- Apache模塊 mod_auth_basic
- Apache模塊 mod_auth_digest
- Apache模塊 mod_authn_alias
- Apache模塊 mod_authn_anon
- Apache模塊 mod_authn_dbd
- Apache模塊 mod_authn_dbm
- Apache模塊 mod_authn_default
- Apache模塊 mod_authn_file
- Apache模塊 mod_authnz_ldap
- Apache模塊 mod_authz_dbm
- Apache模塊 mod_authz_default
- Apache模塊 mod_authz_groupfile
- Apache模塊 mod_authz_host
- Apache模塊 mod_authz_owner
- Apache模塊 mod_authz_user
- Apache模塊 mod_autoindex
- Apache模塊 mod_cache
- Apache模塊 mod_cern_meta
- Apache模塊 mod_cgi
- Apache模塊 mod_cgid
- Apache模塊 mod_charset_lite
- Apache模塊 mod_dav
- Apache模塊 mod_dav_fs
- Apache模塊 mod_dav_lock
- Apache模塊 mod_dbd
- Apache模塊 mod_deflate
- Apache模塊 mod_dir
- Apache模塊 mod_disk_cache
- Apache模塊 mod_dumpio
- Apache模塊 mod_echo
- Apache模塊 mod_env
- Apache模塊 mod_example
- Apache模塊 mod_expires
- Apache模塊 mod_ext_filter
- Apache模塊 mod_file_cache
- Apache模塊 mod_filter
- Apache模塊 mod_headers
- Apache模塊 mod_ident
- Apache模塊 mod_imagemap
- Apache模塊 mod_include
- Apache模塊 mod_info
- Apache模塊 mod_isapi
- Apache模塊 mod_ldap
- Apache模塊 mod_log_config
- Apache模塊 mod_log_forensic
- Apache模塊 mod_logio
- Apache模塊 mod_mem_cache
- Apache模塊 mod_mime
- Apache模塊 mod_mime_magic
- Apache模塊 mod_negotiation
- Apache模塊 mod_nw_ssl
- Apache模塊 mod_proxy
- Apache模塊 mod_proxy_ajp
- Apache模塊 mod_proxy_balancer
- Apache模塊 mod_proxy_connect
- Apache模塊 mod_proxy_ftp
- Apache模塊 mod_proxy_http
- Apache模塊 mod_rewrite
- Apache模塊 mod_setenvif
- Apache模塊 mod_so
- Apache模塊 mod_speling
- Apache模塊 mod_ssl
- Apache模塊 mod_status
- Apache模塊 mod_suexec
- Apache模塊 mod_unique_id
- Apache模塊 mod_userdir
- Apache模塊 mod_usertrack
- Apache模塊 mod_version
- Apache模塊 mod_vhost_alias
- Developer Documentation for Apache 2.0
- Apache 1.3 API notes
- Debugging Memory Allocation in APR
- Documenting Apache 2.0
- Apache 2.0 Hook Functions
- Converting Modules from Apache 1.3 to Apache 2.0
- Request Processing in Apache 2.0
- How filters work in Apache 2.0
- Apache 2.0 Thread Safety Issues
- 詞匯和索引
- 詞匯表
- 指令索引
- 指令速查
- 模塊索引
- 站點導航