23、反向代理功能(httpd目前只能支持一台后端服务器)
启用/反向代理
ProxyPass "/" "http://www.example.com/"
ProxyPassReverse "/" "http://www.example.com/"
特定URL反向代理
ProxyPass "/images" "http://www.example.com/"
ProxyPassReverse "/images" http://www.example.com/
示例:
<VirtualHost *>
ServerName www.magedu.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
proxy_http_module模块代理配置
ProxyRequests: Off关闭正向代理。
ProxyPass: 反向代理指令
ProxyPassReverse: 保留代理的response头不重写(个别除外)
ProxyPreserveHost: On开启。让代理保留原请求的Host首部转发给后端服务器
ProxyVia: On开启。代理的请求响应时提供一个response的via首部
启用反向代理
ProxyPass "/" "http://www.example.com/"
ProxyPassReverse "/" "http://www.example.com/"
特定URL反向代理
ProxyPass "/images" "http://www.example.com/"
ProxyPassReverse "/images" http://www.example.com/
<VirtualHost *:80>
ServerName www.zjol.com
ProxyRequests Off
ProxyVia On
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>
测试ProxyPreserveHost选项:当后端出现多个"主机"
一.是IP访问的主机(没有ServerName),如果ProxyPreserveHost是Off的,转发给tomcat就没有域名首部,只有IP地址,那么将只能访问node1.magedu.com主机,反之为on,访问localhost主机
二.是域名访问主机(有ServerName),如果ProxyPreserveHost是Off的,那么将访问node1.magedu.com主机,反之为on,访问localhost主机
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> </Host>
<Host name="node1.magedu.com" appBase="/data/webapps/" unpackWARs="true" autoDeploy="true">
<Context path="/test" docBase="/data/test" reloadable="false" />
<Context path="/test1" docBase="" reloadable="false" />
</Host>