2011-02-16 37 views
29

在將流量轉發到我的後端服務器之前,嘗試使用基本身份驗證配置我的反向代理。任何人都可以給我一個解決方案。具有基本身份驗證的Apache反向代理

這裏舉例:

用戶(互聯網) - >反向代理/虛擬主機服務器(這裏需要補充基本身份驗證) - >後端服務器(非認證)

回答

50

您可以按照指示在這裏: Authentication, AuthorizationAccess Control。爲您的反向代理的主要區別是,你想要把東西AUTH一個位置塊內,即使文檔說他們只允許在目錄塊:

<Location /> 
    AuthType Basic 
    ... 
</Location> 

外Location塊你可以把你的代理命令,如:

ProxyPass/http://localhost:8080/ 
+0

呀,它的作品甚至反向代理。 – lzap 2012-09-16 19:20:08

16

這裏是我用來完成通過https對數據庫的基本認證的配置。我的後端服務器運行Tomcat,並使用AJP連接到它。有趣的端口號(4443)是因爲標準端口(443)已經被使用,並且我不想在同一個端口上配置多個https服務。

<IfModule mod_ssl.c> 
NameVirtualHost *:4443 
<VirtualHost *:4443> 
     ServerAdmin [email protected] 
     ServerName ws.myserver.se 
     ServerAlias ws.myserveralias.se 
     ErrorLog /var/log/apache2/ajpProxy.error.log 

     # Possible values include: debug, info, notice, warn, error, crit, 
     # alert, emerg. 
     LogLevel info 

     CustomLog /var/log/apache2/ajpProxy.log combined 

     DBDriver mysql 
     DBDParams "host=127.0.0.1 port=3306 user=proxyAuthUser pass=yourDbPasswordHere dbname=yourDbName" 
     DBDMin 4 
     DBDKeep 8 
     DBDMax 20 
     DBDExptime 300   

     <Proxy *> 
       # core authentication and mod_auth_basic configuration 
       # for mod_authn_dbd 
       AuthType Basic 
       AuthName "Backend auth name" 
       AuthBasicProvider dbd 

      # core authorization configuration 
       Require valid-user 

       # mod_authn_dbd SQL query to authenticate a user 
       AuthDBDUserPWQuery \ 
       "SELECT password FROM user WHERE emailAddress = %s" 

       AddDefaultCharset Off 
       Order deny,allow 
       Allow from all 
     </Proxy> 

     ProxyPass/ajp://localhost:8009/ 
     ProxyPassReverse/ajp://localhost:8009/ 

     # SSL Engine Switch: 
     # Enable/Disable SSL for this virtual host. 
     SSLEngine on 

     # A self-signed (snakeoil) certificate can be created by installing 
     # the ssl-cert package. See 
     # /usr/share/doc/apache2.2-common/README.Debian.gz for more info. 
     # If both key and certificate are stored in the same file, only the 
     # SSLCertificateFile directive is needed. 
     SSLCertificateFile /etc/apache2/ssl/yourCertificateFile.crt 
     SSLCertificateKeyFile /etc/apache2/ssl/yourPrivateKeyFile.key 
     <FilesMatch "\.(cgi|shtml|phtml|php)$"> 
       SSLOptions +StdEnvVars 
     </FilesMatch> 
     <Directory /usr/lib/cgi-bin> 
       SSLOptions +StdEnvVars 
     </Directory> 

     BrowserMatch "MSIE [2-6]" \ 
       nokeepalive ssl-unclean-shutdown \ 
       downgrade-1.0 force-response-1.0 
     # MSIE 7 and newer should be able to use keepalive 
     BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown 
</VirtualHost> 
</IfModule> 
2

首先,請檢查您的Apache2有utils包

sudo apt-get install apache2-utils 

然後,設置用戶名和密碼。

sudo htpasswd -c /etc/apache2/.htpasswd <username> 

之後,編輯您的反向代理使用的身份驗證

<VirtualHost *:80> 
    ProxyPreserveHost On 

    ProxyPass/http://someaddress:1234/ 
    ProxyPassReverse/http://someaddress:1234/ 

    Timeout 5400 
    ProxyTimeout 5400 

    ServerName dev.mydomain.com 
    ServerAlias *.dev.mydomain.com 

    <Proxy *> 
     Order deny,allow 
     Allow from all 
     Authtype Basic 
     Authname "Password Required" 
     AuthUserFile /etc/apache2/.htpasswd 
     Require valid-user 
    </Proxy> 
</virtualhost> 

至少,更新你的apache

sudo service apache2 reload