特定子域的 Apache 反向代理
Apache Reverse Proxy For Specific Subdomain
我有一个 Apache HTTP 服务器,它有一个 tomcat 服务器的反向代理。但是,我只希望在客户端使用子域 www 时发生反向代理。这是因为我想使用其他子域指向其他应用程序,例如电子邮件。
例如www.example.com 将显示 apache tomcat webapp。
我想这样做的方法是配置我的 DNS,以便我使用的每个子域都指向我的服务器。现在,除了www,就是server.example.com和posfixadmin.example.com。但是,问题是我所有的子域最终都指向 tomcat.
所以当我尝试访问 postfixadmin.example.com/setup.php 以通过其网络设置设置 postfixadmin 时,它最终将我带到我的 tomcat 网络应用程序404.
这是我的虚拟主机配置:
<VirtualHost www.example.com:80>
ServerName http://www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
<VirtualHost server.example.com:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
<VirtualHost postfixadmin.example.com:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
编辑
看起来代理 conf 文件没有做任何事情(??)。我决定尝试一下并将第一个虚拟主机服务器名更改为以下内容:
<VirtualHost *:80>
ServerName abcd.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
然后,我重新启动并重新加载了 Apache...但是由于某种原因,去 www.example.com 仍然把我带到了 tomcat 网络应用程序!有谁知道是什么驱动了这个?
关于 DNS:我为每个子域设置了特定的 CNAME 条目,包括 www;所有这些都指向我的 example.com 域所在服务器的 public IP(在我的例子中使用 @ - 我认为大多数 DNS 都可以)。在这方面可能有一些不同的策略,但我相信根据您在问题中提出的建议,您走的是正确的道路。
关于 Apache 配置:
我相信 http
协议不需要在 ServerName
指令中指定,并且通常,域不需要出现在 <VirtualHost>
...</VirtualHost>
标签中.
我应该提一下,我对 Tomcat 比较陌生,但我假设它正在本地主机上的 8080 监听,在这种情况下这应该有所帮助。
我不是 100% 确定这就是让你咆哮的全部内容,但请尝试修剪 ServerName
并这样做,包括对 VirtualHost
开放标签的更改:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
您的第二个 <VirtualHost>
可能需要进行类似的更改,但您似乎也在指示它为来自 web/network 的请求提供服务在端口 8080 上——我认为这不是你的意图。
我想你想要的是也从 web/network 监听 端口 80,但要遵循这些指令如果像这样发送给 server.example.com
:
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
最后,对最后一个 <VirtualHost>
标签的类似更改:
<VirtualHost *:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
总而言之,这似乎更像您要找的东西:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
<VirtualHost *:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
我明白了!
事实证明问题出在 ssl 配置文件中 - :443 端口重叠。
感谢您的帮助!
我有一个 Apache HTTP 服务器,它有一个 tomcat 服务器的反向代理。但是,我只希望在客户端使用子域 www 时发生反向代理。这是因为我想使用其他子域指向其他应用程序,例如电子邮件。
例如www.example.com 将显示 apache tomcat webapp。
我想这样做的方法是配置我的 DNS,以便我使用的每个子域都指向我的服务器。现在,除了www,就是server.example.com和posfixadmin.example.com。但是,问题是我所有的子域最终都指向 tomcat.
所以当我尝试访问 postfixadmin.example.com/setup.php 以通过其网络设置设置 postfixadmin 时,它最终将我带到我的 tomcat 网络应用程序404.
这是我的虚拟主机配置:
<VirtualHost www.example.com:80>
ServerName http://www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
<VirtualHost server.example.com:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
<VirtualHost postfixadmin.example.com:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
编辑 看起来代理 conf 文件没有做任何事情(??)。我决定尝试一下并将第一个虚拟主机服务器名更改为以下内容:
<VirtualHost *:80>
ServerName abcd.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
然后,我重新启动并重新加载了 Apache...但是由于某种原因,去 www.example.com 仍然把我带到了 tomcat 网络应用程序!有谁知道是什么驱动了这个?
关于 DNS:我为每个子域设置了特定的 CNAME 条目,包括 www;所有这些都指向我的 example.com 域所在服务器的 public IP(在我的例子中使用 @ - 我认为大多数 DNS 都可以)。在这方面可能有一些不同的策略,但我相信根据您在问题中提出的建议,您走的是正确的道路。
关于 Apache 配置:
我相信 http
协议不需要在 ServerName
指令中指定,并且通常,域不需要出现在 <VirtualHost>
...</VirtualHost>
标签中.
我应该提一下,我对 Tomcat 比较陌生,但我假设它正在本地主机上的 8080 监听,在这种情况下这应该有所帮助。
我不是 100% 确定这就是让你咆哮的全部内容,但请尝试修剪 ServerName
并这样做,包括对 VirtualHost
开放标签的更改:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
您的第二个 <VirtualHost>
可能需要进行类似的更改,但您似乎也在指示它为来自 web/network 的请求提供服务在端口 8080 上——我认为这不是你的意图。
我想你想要的是也从 web/network 监听 端口 80,但要遵循这些指令如果像这样发送给 server.example.com
:
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
最后,对最后一个 <VirtualHost>
标签的类似更改:
<VirtualHost *:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
总而言之,这似乎更像您要找的东西:
<VirtualHost *:80>
ServerName www.example.com
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
</Virtualhost>
<VirtualHost *:80>
ServerName server.example.com
DocumentRoot /var/www/html/
RewriteEngine on
RewriteCond %{SERVER_NAME} =server.example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>
<VirtualHost *:80>
ServerName postfixadmin.example.com
DocumentRoot /var/www/postfixadmin/public
ErrorLog /var/log/httpd/postfixadmin_error.log
CustomLog /var/log/httpd/postfixadmin_access.log combined
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/postfixadmin/public>
Options FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
我明白了!
事实证明问题出在 ssl 配置文件中 - :443 端口重叠。
感谢您的帮助!