在某些情况下,虽然Apache已经能满足其大多数通用Web服务需求,但其他Web或应用程序服务器更适合某些任务。 幸运的是,很容易配置Apache将某些请求传递到其他Web服务器进程。 这些辅助(或第三)web服务器可以在相同的服务器或单独的节点(可能通过专用网络)上运行。 我们的示例使用lighttpd作为辅助Web服务器,但它们也可以代理HTTP请求到任何Web服务器或应用程序。
激活Proxy模块
按如下内容编辑/etc/apache2/mods-available/proxy.conf:
- <IfModule mod_proxy.c>
- #turning ProxyRequests on and allowing proxying from all may allow
- #spammers to use your proxy to send email.
- ProxyRequests Off
- <Proxy *>
- AddDefaultCharset off
- Order deny,allow
- Allow from all
- </Proxy>
- # Enable/disable the handling of HTTP/1.1 "Via:" headers.
- # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
- # Set to one of: Off | On | Full | Block
- ProxyVia On
- </IfModule>
这将启用代理支持。
下一步,执行如下命令:
- a2enmod proxy
- a2enmod proxy_http
- service apache2 restart
Apache应该能正常重新启动。如果您遇到任何问题,您可以检查/var/log/apache2/下的日志以获取更多信息。
代理一个域名到Lighttpd
我们已经有一个名为“www.firstsite.org”的网站作为正常的虚拟主机在Apache下运行。 我们将使用Apache将对“www.secondsite.org”网站的请求发送到lighttpd,lighttpd运行在端口8080上。 这里是“www.secondsite.org”的配置文件:
/etc/apache2/sites-available/www.secondsite.org:
- <VirtualHost *:80>
- ServerAdmin [email protected]
- ServerName secondsite.org
- ServerAlias www.secondsite.org
- ProxyPass / http://localhost:8080/
- # Uncomment the line below if your site uses SSL.
- #SSLProxyEngine On
- </VirtualHost>
ProxyPass指令告诉Apache将此域的所有请求转发到在端口8080上运行的Web服务器。如果我们的目标服务器在另一个服务器上运行,我们可以指定地址。 我们将使用以下命令启用该网站:
- a2ensite www.secondsite.org
- service apache2 reload
代理指定URL到Lighttpd
如果我们想把http://www.firstsite.org/myapp/的请求转向 lighttpd,我们只需修改其配置文件,如下所示:
/apache2/sites-available/www.firstsite.org:
- <VirtualHost *:80>
- ServerAdmin [email protected]
- ServerName firstsite.org
- ServerAlias www.firstsite.org
- DocumentRoot /srv/www/firstsite.org/public_html/
- ErrorLog /srv/www/firstsite.org/logs/error.log
- CustomLog /srv/www/firstsite.org/logs/access.log combined
- ProxyPass /myapp http://localhost:8080/
- </VirtualHost>