仅代理本地主机的子域

我试图找出如何代理请求到本地主机的子域到Apache上的另一个端口,而不是代理请求只有本地主机(没有子域)。 我无法得到它的工作。 这是我到目前为止所提出的。

<VirtualHost *:80> ServerName subdomain1.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ ServerName subdomain2.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ NoProxy "localhost" </VirtualHost> 

subdomain1和subdomain2到localhost:3000的代理工作,但localhost也代理到localhost:3000。 我如何防止?

编辑

在@Esa和@HBruijn的帮助下,我仍然无法使其工作。 我编辑了虚拟主机http.conf到以下。 别名工作,但现在的子域名不起作用。

 <VirtualHost *:80> ServerName localhost Alias /alias1 "/alias1" Alias /alias2 "/alias2" Alias /alias3 "/alias3" </VirtualHost> <VirtualHost *:80> ServerName subdomain1.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> <VirtualHost *:80> ServerName subdomain2.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> 

最简单的方法是简单地创build单独的VirtualHost条目,每个条目都具有该(子)域的正确设置:

 <VirtualHost *:80> ServerName localhost DocumentRoot /var/www/html ... </VirtualHost> <VirtualHost *:80> ServerName subdomain1.localhost # Possibly a single VirtualHost for all subdomains with a catch-all ServerAlias: # ServerAlias *.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> <VirtualHost *:80> ServerName subdomain2.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> 

这不是ServerName指令的工作方式。 看到:

ServerName指令可能出现在服务器定义的任何地方。 但是,每个外观都会覆盖之前的外观(在该服务器内)。

如果未指定ServerName ,则服务器首先向操作系统请求系统主机名,然后尝试推断客户端可见主机名,如果失败,则对系统上存在的IP地址执行反向查找。

如果ServerName没有指定端口,则服务器将使用传入请求中的端口。 为了获得最佳的可靠性和可预测性,您应该使用ServerName指令指定一个明确的主机名和端口。

你不能在同一个VirtualHost有两个ServerName指令,所以首先你需要分开:

 <VirtualHost *:80> ServerName subdomain1.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> <VirtualHost *:80> ServerName subdomain2.localhost ProxyPass / http://localhost:3000/ ProxyPassReverse / http://localhost:3000/ </VirtualHost> 

然后,为了让localhost:80做一些事情而不是回退到默认configuration(同一个端口内的第一个VirtualHost ,当前是subdomain1.localhost ),你需要拥有自己的<VirtualHost> Section Container 。 所以你可以在上面添加这个:

 <VirtualHost *:80> ServerName localhost DocumentRoot /var/www/html </VirtualHost> 

另外,具有相同的[URL-path]file-path|directory-pathAlias es没有任何意义,但我想在右边有一些真正的文件系统位置。