我希望Apache以下列方式工作:
- 用户键入 user1.app.com 到地址栏中。
- DNS 有一个通配符,可以将所有内容转发到SERVER1
- SERVER1运行了Apache,它将使用重写映射将user1映射到IP地址 xxx.xxx.xxx。
- Apache提供 xxx.xxx.xxx 中的所有内容,同时保留URL user1.app.com
我尝试了几种方式:
方法1:
RewriteRule ^(.*) http://xxx.xxx.xxx:port/ [P]
结果:重定向循环,远程 IP 被访问几次(可以通过查看远程服务器上的日志来确认(。SERVER1的日志显示以下内容的重复:
proxy: *: found reverse proxy worker for http://xxx.xxx.xxx/
mod_proxy.c(1020): Running scheme http handler (attempt 0)
mod_proxy_http.c(1973): proxy: HTTP: serving URL http://xxx.xxx.xxx/
proxy_util.c(2011): proxy: HTTP: has acquired connection for (*)
proxy_util.c(2067): proxy: connecting http://xxx.xxx.xxx/ to xxx.xxx.xxx:80
proxy_util.c(2193): proxy: connected / to xxx.xxx.xxx:80
proxy_util.c(2444): proxy: HTTP: fam 2 socket created to connect to *
proxy_util.c(2576): proxy: HTTP: connection complete to xxx.xxx.xxx:80 (xxx.xxx.xxx)
mod_proxy_http.c(1743): proxy: start body send
mod_proxy_http.c(1847): proxy: end body send
proxy_util.c(2029): proxy: HTTP: has released connection for (*)
方法2:
<VirtualHost *:801>
ServerName SERVER1
ProxyRequests Off
ProxyPreserveHost On
<Proxy *:801>
Order deny,allow
Allow from localhost
</Proxy>
ProxyPass / http://xxx.xxx.xxx/
ProxyPassReverse / http://xxx.xxx.xxx/
LogLevel debug
</VirtualHost>
和
RewriteRule ^(.*) http://127.0.0.1:801/ [PT]
结果:400 个错误请求使用 method2,我可以在浏览器中转到 SERVER1:801,一切按预期工作。
任何帮助将不胜感激!提前感谢!
解决方案最终是 apache 中几种不同工具的组合:
- 使用文本文件将每个子域映射到相应的 IP
- 在代理重定向中插入环境变量以访问子域(必须在
ProxyPass
和ProxyPassReverse
语句末尾使用ProxyPassInterpolateEnv On
和interpolate
关键字(
阿帕奇:
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from localhost
</Proxy>
RewriteEngine On
ProxyPassInterpolateEnv On
RewriteMap subdomains txt:/directory/clients.txt
RewriteCond %{HTTP_HOST} ^(.*).application.com
RewriteRule ^ - [E=SERVER_NAME:${subdomains:%1}]
RewriteCond %{REQUEST_URI} !(.[^./]+)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R]
ProxyPass / http://${SERVER_NAME}/ interpolate
ProxyPassReverse / http://${SERVER_NAME}/ interpolate