將正確的 HTTP 頭轉寄給後端伺服器的一些問題

來源:互聯網
上載者:User

標籤:開始   cal   努力   虛擬   event   www   組合   dir   sts   

Apache Software Foundation 的 HTTP 伺服器項目(通常稱為 Apache)是當今互連網上佔據優勢的 Web 服務器,它佔據了 60% 以上的市場份額。Apache 伺服器是日漸流行的 LAMP 軟體配置的一部分。LAMP 是一套免費軟體程式,是在 Linux?、Apache、MySQL 和 PHP 等開放源碼技術之上構建的 Web 平台。在本文中,您將學習一種使用 mod_proxy 模組和多個後端伺服器來改進 LAMP 安全性的方法。我將討論這種方法的優點和缺點,並提供一個配置樣本。

  PHP 和 Apache:安全性難題

  LAMP 管理員面對的一個挑戰是,提供完整 PHP 系統的所有特性,同時確保為系統的所有使用者提供一個安全的環境。使用 PHP 的安全模式是實現這一目標的一種技術,但是它也過度地限制了使用者,而且啟用了這個設施之後,一些 PHP 應用程式就不能發揮作用。

  PHP 安全問題的根源在於大多數 Apache 伺服器的配置方式。因為大多數 Apache 配置運行在特殊的 www-data 使用者識別碼 下,對 Web 網站進行主機託管的所有使用者在預設情況下必須確保這個使用者可以讀取他們的檔案。因此,系統上的所有其他使用者都可能訪問一個使用者的所有 Web 可訪問檔案;所以系統上原本與您無關的安全性漏洞會成為攻擊您的 Web 網站的突破口。如果檔案或目錄必須設定為 www-data 使用者可寫的,那麼這種情況會更加嚴重。

  通過使用 CGI 程式,比如用 Perl 和 Python 等流行語言編寫的程式,可以在使用 suEXEC 機制時消除這個問題的部分影響。簡單地說,suEXEC 使用一個特殊的中間程式以程式所有者的使用者識別碼 執行 CGI 程式。這是一種非常有效機制,已經使用了許多年了。

  但是,在使用 mod_php 模組主機託管時,PHP 頁面作為 Apache 主進程的一部分執行。因此,它們繼承 Apache 進程的所有憑證,而且它們在檔案系統上執行的任何工作必須作為 www-data 使用者執行。

  在多個使用者識別碼 下運行 Apache

  對於上面描述的問題,明顯的解決方案是要求對一個使用者域的所有請求都來自一個只擁有此使用者的憑證的 Apache 執行個體。可以將 Apache 配置為在啟動時擷取任何使用者的憑證。對於給每個使用者指派一個單獨的互連網可見 IP 位址/連接埠組合的簡單設定,這種方法可以解決問題。

  對於更複雜的設定(在其中 IP 位址很寶貴),這種方法是無效的。當單一 Apache 執行個體可以控制一個特定的 IP 位址/連接埠組合時,只能使用虛擬機器主機,這是 Apache 系統中廣泛使用的一種技術。這就排除了讓屬於多個使用者的多個域使用同一個 IP 位址/連接埠組合的可能性。

  Apache 2.0 引入了多處理模組(multiprocessing module,MPM) 的概念。在基本 Apache 2.0 包提供的 MPM 中有一個實驗性的模組 perchild,它可以將一個分布器線程分配給 IP 位址/連接埠組合,並將請求傳遞給在單獨使用者的憑證下啟動並執行子線程,從而實現多個使用者識別碼 下的虛擬機器主機。遺憾的是,perchild 仍然是實驗性的,它不一定能夠發揮作用,而且在 Apache 2.2 發布時從正式 Apache 發行包中刪除了。在此之前,由於認識到仍然需要一個穩定的能夠發揮作用的與 perchild 相似的 MPM,Apache 社區開始研發許多 MPM 來彌補這一欠缺。MetuxMPM 以及面向過程的 peruser 正在朝著這個方向努力。

  一個解決方案:mod_proxy

  儘管還沒有正式的 Apache MPM 能夠直接提供多個使用者識別碼 下的虛擬機器主機,但是仍然可以通過某些配置和管理在 Apache 系統中實現這種行為。這種方法的核心概念是使用 mod_proxy 模組,這個模組(加上其他功能)使 Apache 能夠將頁面請求轉寄給其他伺服器,並將響應傳遞迴原來發出請求的客戶機。

清單 1. 基本請求轉寄的反向 Proxy配置樣本

ProxyRequests Off 
 
ProxyPass /foo http://foo.example.com/bar 
ProxyPassReverse /foo http://foo.example.com/bar 

  清單 1 中的代碼是一個簡單的樣本,它將對一個主機的 /foo 階層下任何頁面的請求轉寄到 http://foo.example.com/bar 的對應頁面。例如,對 /foo/index.htm 頁面的請求會轉寄到 http://foo.example.com/bar/index.htm。可以使用這一原理解決問題。

  樣本情境

  我們來考慮一個情境:Apache 管理員必須為兩個單獨的客戶建立兩個域。一個客戶是線上創業企業,很關注線上安全性。另一個是個人客戶,他在網站安全性方面比較寬鬆,可能將不安全的代碼上傳到這個網站。因此,Apache 管理員必須採取措施將這兩個網站隔離開。

  因此,管理員有兩個域:www.startup.tld,它屬於線上創業企業(使用者識別碼 startup);以及 www.reckless.tld,它屬於個人(使用者識別碼 nimrod)。為瞭解決這個問題,管理員決定使用 mod_proxy 解決方案。管理員給每個使用者一個單獨的 Apache 執行個體,這個執行個體運行在使用者自己的使用者識別碼 下,使用私人的 IP 位址/連接埠組合,並使用 mod_proxy 解決方案通過一個 facade 伺服器提供對這兩個使用者的域的訪問,這個伺服器作為 www-data 運行,使用一個公用的 IP 位址/連接埠組合。圖 1 說明了整個情境。

圖 1. 情境樣本


  推薦的 Apache 版本

  對於應用程式範例配置中的每個元素,Apache 管理員應該使用 表 1 中列出的 Apache 版本。

表 1. 應用程式範例中使用的 Apache 版本

元素 Apache 版本 原因 
facade 伺服器 Apache 2,運行 worker 或 event MPM Apache 2 對 mod_proxy 模組做了重要的改進。worker 和 event MPM 是線程化的,有助於減少 facade 伺服器的記憶體開銷。 
後端伺服器 Apache 1.3,或運行 prefork MPM 的 Apache 2 Apache 管理員必須意識到 PHP 模組不應該運行線上程化環境中。這兩個解決方案為 PHP 模組提供了基於進程的環境。


  後端 Apache 執行個體的配置

  清單 2 和 清單 3 中的程式碼片段說明了與標準 Apache 配置的基本差異。應該根據需要將它們添加到適當的配置中,比如這裡忽略的 PHP 功能配置。

清單 2. 線上創業企業的 Apache 配置

# Stuff every Apache configuration needs 
ServerType standalone 
LockFile /var/lock/apache/accept.startup.lock 
PidFile /var/run/apache.startup.pid 
 
ServerName necessaryevil.startup.tld 
DocumentRoot "/home/startup/web" 
 
# Essential modules 
LoadModule access_module /usr/lib/apache/1.3/mod_access.so 
 
# Which user to run this Apache configuration as 
User startup 
Group startup 
 
# This must be off else the host isn‘t passed correctly 
UseCanonicalName Off 
 
# The IP/port combination to listen on 
Listen 127.0.0.2:10000 
 
# Using name-based virtual hosting allows you to host multiple sites per IP/port combo 
NameVirtualHost 127.0.0.2:10000 
 
<VirtualHost 127.0.0.2:10000> 
    ServerName www.startup.tld 
 
    # You can add aliases so long as the facade server is aware of them! 
    ServerAlias startup.tld 
 
    DocumentRoot "/home/startup/web/www.startup.tld" 
 
    <Directory /home/startup/web/www.startup.tld/> 
      Options Indexes FollowSymLinks MultiViews ExecCGI Includes 
      AllowOverride All 
      Order allow,deny 
      Allow from all 
    </Directory> 
 
</VirtualHost> 

清單 3. 個人客戶的 Apache 配置

# Stuff every Apache configuration needs 
ServerType standalone 
LockFile /var/lock/apache/accept.nimrod.lock 
PidFile /var/run/apache.nimrod.pid 
 
ServerName necessaryevil.nimrod.tld 
DocumentRoot "/home/nimrod/web" 
 
# Essential modules 
LoadModule access_module /usr/lib/apache/1.3/mod_access.so 
 
# Which user to run this Apache configuration as 
User nimrod 
Group nimrod 
 
# This must be off else the host isn‘t passed correctly 
UseCanonicalName Off 
 
# The IP/port combination to listen on 
Listen 127.0.0.2:10001 
 
# Using name-based virtual hosting allows you to host multiple sites per IP/port combo 
NameVirtualHost 127.0.0.2:10001 
 
<VirtualHost 127.0.0.2:10001> 
    ServerName www.reckless.tld 
 
    # You can add aliases so long as the facade server is aware of them! 
    ServerAlias reckless.tld 
 
    DocumentRoot "/home/nimrod/web/www.reckless.tld" 
 
    <Directory /home/nimrod/web/www.reckless.tld/> 
      Options Indexes FollowSymLinks MultiViews ExecCGI Includes 
      AllowOverride All 
      Order allow,deny 
      Allow from all 
    </Directory> 
 
</VirtualHost> 

  清單 4 說明了門面 Apache 執行個體的配置。

清單 4. 門面 Apache 執行個體的 Apache 配置

# Stuff every Apache configuration needs 
LockFile /var/lock/apache/accept.www-data.lock 
PidFile /var/run/apache.www-data.pid 
 
ServerName necessaryevil.facade.server 
DocumentRoot "/home/www-data" 
 
# Essential modules 
LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so 
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so 
 
# Which user to run this Apache configuration as 
User www-data 
Group www-data 
 
# These must be set else the host isn‘t passed correctly 
UseCanonicalName Off 
ProxyVia On 
ProxyRequests Off 
# This must also be set, though it‘s only an option in Apache2 
ProxyPreserveHost On   
 
# The IP/port combination to listen on 
Listen 9.20.1.1:80 
 
# Using name-based virtual hosting allows you to host multiple sites per IP/port combo 
NameVirtualHost 9.20.1.1:80 
 
# Configuration to forward requests for startup.tld 
<VirtualHost 9.20.1.1:80> 2881064151
    ServerName www.startup.tld 
    ServerAlias startup.tld 
 
    ProxyPass / http://127.0.0.2:10000/ 
    ProxyPassReverse / http://127.0.0.2:10000/ 
    ProxyPassReverse / http://www.startup.tld:10000/ 
    ProxyPassReverse / http://startup.tld:10000/ 
</VirtualHost> 
 
# Configuration to forward requests for reckless.tld 
<VirtualHost 9.20.1.1:80> 
    ServerName www.reckless.tld 
    ServerAlias reckless.tld 
 
    ProxyPass / http://127.0.0.2:10001/ 
    ProxyPassReverse / http://127.0.0.2:10001/ 
    ProxyPassReverse / http://www.reckless.tld:10001/ 
    ProxyPassReverse / http://reckless.tld:10001/ 
</VirtualHost> 

  一定要注意這裡的 ProxyPreserveHost 指令。這個指令是 Apache 2 提供的,它解決了將正確的 HTTP 頭轉寄給後端伺服器的一些問題。因此,強烈建議使用 Apache 2 執行個體作為 facade 伺服器。

  運行樣本配置

  根使用者應該運行每個配置。Apache 將取得設定檔中指定的特權,並將其用於所有與主機相關的進程。清單 5 說明運行樣本的方法。

清單 5. 啟動樣本伺服器

  /usr/sbin/apache -f /etc/apache/startup.tld.conf 
/usr/sbin/apache -f /etc/apache/nimrod.tld.conf 
/usr/sbin/apache2 -f /etc/apache2/facade.tld.conf 

  mod_proxy 方法的限制

  一定要注意,本文中描述的方法不適用於需要 SSL 串連的域。這是因為 SSL 協議不允許域的虛擬機器主機。由於這個限制,任何 SSL 主機必須以適當的方式執行,讓每個 SSL 域使用它自己的 IP/連接埠組合。這個限制對所有 Apache 配置都存在,使用這個解決方案的 Apache 也不例外。仍然可以在它們的所有者的使用者識別碼 下運行 SSL 域。

將正確的 HTTP 頭轉寄給後端伺服器的一些問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.