2011-12-16 193 views
95

我一直在努力这一段时间,我肯定做错了什么。Apache重定向到另一个端口

我在同一台机器上有apache服务器和JBoss服务器。我想重定向mydomain.com的流量到JBoss localhost:8080/example。 DNS当前为mydomain.com设置,并且在进入浏览器时将直接进入端口80。

我的问题是,当某个域名到达Apache(在本例中为“mydomain.com”)时,我该如何重定向到不同的端口?

<VirtualHost ip.addr.is.here> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName mydomain.com 
    ProxyPass http://mydomain.com http://localhost:8080/example 
    ProxyPassReverse http://mydomain.com http://localhost:8080/example 
</VirtualHost> 

修订W /建议 - 还没转发到端口8080

<VirtualHost *:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName mydomain.com 
    ServerAlias www.mydomain.com 
    ProxyPass http://mydomain.com http://localhost:8080/example 
    ProxyPassReverse http://mydomain.com http://localhost:8080/example 
</VirtualHost> 
+0

这看起来不错。你的症状是什么? – 2011-12-16 23:08:04

+0

Sympotms是我把www.mydomain.com放在浏览器中,但它转到apache根目录。相反,我希望它重定向到一个jboss子目录localhost:8080/subdir。我目前正在将各个域直接重定向到80端口,但无法将其转移到另一个端口。 – agentcurry 2011-12-16 23:20:14

+0

我有完全相同的要求:您是否找到解决方案? – Cystack 2012-06-27 12:27:43

回答

138

您应该忽略ProxyPass和ProxyPassReverse中的域http://example.com并将其保留为/。此外,您需要将/保留在example/的末尾以重定向到的位置。此外,我在http://example.comhttp://www.example.com之间遇到了一些问题 - 只有www在我制作了ServerName www.example.com和ServerAlias example.com之前一直工作。给以下一个去。

<VirtualHost *:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName www.example.com 
    ServerAlias example.com 
    ProxyPass/http://localhost:8080/example/ 
    ProxyPassReverse/http://localhost:8080/example/ 
</VirtualHost> 

后进行这些更改,添加需要的模块,并重新启动Apache

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart 
2

如果你没有使用代理服务器JBoss和mydomain.com:8080可以“暴露“世界,然后我会这样做。

<VirtualHost *:80> 
    ServerName mydomain.com 
    Redirect 301/http://mydomain.com:8080/ 
</VirtualHost> 
0

Apache支持基于名称和基于IP的虚拟主机。看起来你正在使用两者,这可能不是你所需要的。

我想你实际上是在设置name-based virtual hosting,因此你不需要指定IP地址。

尝试< VirtualHost *:80>绑定到所有IP地址,除非您真的想要ip based virtual hosting。如果服务器有多个IP地址,并且您想在不同的地址上提供不同的站点,则可能会出现这种情况。最常见的设置是(我猜)基于名称的虚拟主机。

0

您需要两两件事:

  1. 添加ServerAlias www.mydomain.com到你的配置
  2. 改变你的ProxyPass到ProxyPassMatch ^(.*)$ http://localhost:8080/example$1,对可能保持mod_dir和结尾的斜杠从interferring。
23

我解决了这个问题,用下面的代码:

LoadModule proxy_module modules/mod_proxy.so 
LoadModule proxy_http_module modules/mod_proxy_http.so 
<VirtualHost *:80> 
ProxyPreserveHost On 
ProxyRequests Off 
ServerName myhost.com 
ServerAlias ww.myhost.com 
ProxyPass/http://localhost:8080/ 
ProxyPassReverse/http://localhost:8080/ 
</VirtualHost> 

我也用过:

a2enmod proxy_http 
7

我想d正是这个,所以我可以从根域访问Jenkins。

我发现我必须禁用默认网站才能使其工作。这正是我所做的。

$ sudo vi /etc/apache2/sites-available/jenkins 

,并插入到这个文件:

<VirtualHost *:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName mydomain.com 
    ServerAlias mydomain 
    ProxyPass/http://localhost:8080/ 
    ProxyPassReverse/http://localhost:8080/ 
    <Proxy *> 
     Order deny,allow 
     Allow from all 
    </Proxy> 
</VirtualHost> 

接下来,您需要启用/禁用相应的网站:

$ sudo a2ensite jenkins 
$ sudo a2dissite default 
$ sudo service apache2 reload 

希望它可以帮助别人。

2

这可能是一个老问题,但这里是我所做的:

在.conf文件文件由apache加载:

<VirtualHost *:80> 
    ServerName something.com 
    ProxyPass/http://localhost:8080/ 
</VirtualHost> 

说明:监听所有请求都发送到本地机器的端口80.如果我请求“http://something.com/somethingorother”,请将该请求转发到“http://localhost:8080/somethingorother”。这应该适用于外部访问者,因为根据文档,它将远程请求映射到本地服务器的空间。

我正在运行Apache 2.4.6-2ubuntu2.2,所以我不确定“-2ubuntu2.2”如何影响此答案的更广泛适用性。

后进行这些更改,添加需要的模块,并重新启动Apache

sudo a2enmod proxy && sudo a2enmod proxy_http && sudo service apache2 restart 
4

通过反复试验发现了这一点。如果你的配置指定了一个ServerName,那么你的VirtualHost指令将需要做同样的事情。在下面的例子中,awesome.example.com和amazing.example.com都将被转发到一些本地服务运行在端口4567

ServerName example.com:80 

<VirtualHost example.com:80> 
    ProxyPreserveHost On 
    ProxyRequests Off 
    ServerName awesome.example.com 
    ServerAlias amazing.example.com 
    ProxyPass/http://localhost:4567/ 
    ProxyPassReverse/http://localhost:4567/ 
</VirtualHost> 

我知道这并不完全回答这个问题,但我把它放在这里是因为这是Apache端口转发的最高搜索结果。所以我认为这有助于某个人。

3

您必须确保服务器上已启用代理。您可以使用以下命令执行此操作:

a2enmod proxy 
    a2enmod proxy_http 

    service apache2 restart 
0

所有这些都是通过虚拟服务器上的域名访问端口的绝佳见解。但是,不要忘记启用虚拟服务器;这可以被注释掉:

NameVirtualHost *:80 
<Directory "/home/dawba/www/"> 
allow from all 
</Directory> 

我们运行WSGI与域sxxxx.com并在端口6800上运行某些防火墙似乎阻止与端口个域名服务器golang Apache服务器。这是我们的解决方案:

<VirtualHost *:80> 
ProxyPreserveHost On 
ProxyRequests Off 
ServerName wsgi.sxxxx.com 
DocumentRoot "/home/dxxxx/www" 
    <Directory "/home/dxxx/www"> 
    Options Indexes FollowSymLinks 
    AllowOverride None 
    Order allow,deny 
    Allow from all 
    </Directory> 
ScriptAlias /py/ "/home/dxxxx/www/py/" 
WSGIScriptAlias /wsgiprog /home/dxxxx/www/wsgiprog/Form/Start.wsgi 
</VirtualHost> 

<VirtualHost *:80> 
ProxyPreserveHost On 
ProxyRequests Off 
ServerName sxxxx.com 
ServerAlias www.sxxxx.com 
ProxyPass/http://localhost:6800/ 
ProxyPassReverse/http://localhost:6800/ 
</VirtualHost> 
0

我的Apache监听2个不同的端口,

Listen 8080 
Listen 80 

我用的是80级的时候我希望有一个透明的网址,不要把该端口的URL 对谷歌有用的后不允许本地url的服务?

但是我用8080给内部开发,其中我使用的端口作为参考的“开发环境”

0

这在ISPConfig工作压力太大。在网站列表中进入一个域内,点击选项标签,添加这些行:;

ProxyPass/http://localhost:8181/ 
ProxyPassReverse/http://localhost:8181/ 

然后去网站和wolaa :)这也是HTTPS协议。