【问题标题】:Apache redirect to another portApache重定向到另一个端口
【发布时间】:2012-01-22 09:27:16
【问题描述】:

我已经为此苦苦挣扎了一段时间,并且肯定做错了什么。

我在同一台机器上有 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> 

已更新,有建议 - 仍然没有转发到端口 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> 

【问题讨论】:

  • 看起来不错。你有什么症状?
  • 症状是我将 www.mydomain.com 放在浏览器中,但它进入了 apache 根目录。相反,我希望它重定向到 jboss 子目录 localhost:8080/subdir。我目前正在使用它来将各种域直接重定向到端口 80,但不能让它去另一个端口。
  • 我也有同样的要求:您找到解决方案了吗?
  • @Cystack - 我无法让它工作。我尝试了如此多的配置变化,这让我发疯了。我放弃了它=/如果您碰巧找到解决方案,请在此处发布,我想知道如何使其工作。祝我的朋友好运!

标签: apache redirect


【解决方案1】:

您应该在 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

【讨论】:

  • 在重启 apache 之前启用 apache 模块 -digitalocean.com/community/tutorials/…
  • 这对于 CentOS 服务器会不会一样? (我不明白为什么不,只是确保)。而且,这将是什么配置文件? (我猜在 /etc/httpd/conf 的某个地方......)
  • 它工作,但我看到在我的应用程序的欢迎页面中,css 没有加载(之前在 example.com:8080/example 上工作)。并且也有一些例外。知道我错过了什么吗?
  • 这导致我的 httpd 上出现“服务不可用”错误,直到我执行 /usr/sbin/setsebool -P httpd_can_network_connect 1
  • @vladkras 非常感谢您的提示,非常有帮助!
【解决方案2】:

我用以下代码解决了这个问题:

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

【讨论】:

    【解决方案3】:

    我想这样做,这样我就可以从根域访问 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
    

    希望对某人有所帮助。

    【讨论】:

    • 感谢@Louth 的贡献。我早就放弃了,不再拥有服务器。仍然困扰着我,我从来没有让它工作,我可能会启动一个新服务器只是为了再次测试。再次感谢,如果/当我再试一次时,我一定会更新。
    • Jenkins wiki 解释了如何在 Apache 后面运行 Jenkins:wiki.jenkins-ci.org/display/JENKINS/…
    【解决方案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 端口转发的热门搜索结果。所以我认为它总有一天会帮助到某人。

    【讨论】:

    • 这是唯一对我有用的。使用*:80 而不是example.com:80 意味着端口不会被重定向。
    【解决方案5】:

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

      a2enmod proxy
      a2enmod proxy_http
    
      service apache2 restart
    

    【讨论】:

      【解决方案6】:

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

      在 apache 加载的 .conf 文件中:

      <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
      

      【讨论】:

        【解决方案7】:

        如果您不必使用 JBoss 的代理并且 mydomain.com:8080 可以“公开”给全世界,那么我会这样做。

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

        【讨论】:

        • 使用 80 以外的端口不仅看起来难看,而且还会导致代理服务器或 ISP 出现问题。在这种情况下,只需删除 Apache 并在端口 80 上运行 JBoss!
        • 在 JBoss 下运行一切是一种选择,我只是想避免将 apache 站点移动到 JBoss,但它不应该过于耗时。感谢您的反馈。
        【解决方案8】:

        只需在您的 apache 配置中使用反向代理(直接):

        ProxyPass /foo http://foo.example.com/bar
        ProxyPassReverse /foo http://foo.example.com/bar
        

        Look here for apache documentation of how to use the mod

        【讨论】:

          【解决方案9】:

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

          Listen 8080
          Listen 80  
          

          当我想要一个透明的 URL 并且不将端口放在 URL 之后时,我使用 80 对不允许本地 url 的谷歌服务有用吗?

          但我使用 8080 进行内部开发,我将端口用作“开发环境”的参考

          【讨论】:

            【解决方案10】:

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

            我认为您实际上是在尝试设置 name-based virtual hosting,为此您无需指定 IP 地址。

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

            【讨论】:

            • 有道理,我只需要基于名称。我改变了它,但它仍然没有路由到另一个端口
            【解决方案11】:

            你需要两件事:

            1. ServerAlias www.mydomain.com 添加到您的配置中
            2. 将您的 proxypass 更改为 ProxyPassMatch ^(.*)$ http://localhost:8080/example$1,以防止 mod_dir 和尾部斜杠干扰。

            【讨论】:

            • 我添加了 ServerAlias 但仍然没有运气。
            【解决方案12】:

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

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

            我们在域 sxxxx.com 上使用 Apache 服务器和在端口 6800 上运行的 golang 服务器运行 WSGI。一些防火墙似乎阻止了带有端口的域名。这是我们的解决方案:

            <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>
            

            【讨论】:

              【解决方案13】:

              这也适用于 ISPConfig。在网站列表中进入域,单击选项选项卡,添加以下行:;

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

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

              【讨论】:

                【解决方案14】:

                试试这个-

                <VirtualHost *:80> 
                  ProxyPreserveHost On
                  ProxyRequests Off
                  ServerName www.adminbackend.example.com
                  ServerAlias adminbackend.example.com
                  ProxyPass / http://localhost:6000/
                  ProxyPassReverse / http://localhost:6000/
                  ErrorLog ${APACHE_LOG_DIR}/error.log
                  CustomLog ${APACHE_LOG_DIR}/access.log combined
                </VirtualHost> 
                

                【讨论】:

                  猜你喜欢
                  • 2018-04-27
                  • 1970-01-01
                  • 2017-05-15
                  • 2021-05-28
                  • 2016-01-26
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多