【问题标题】:How to redirect two different ports using Apache httpd?如何使用 Apache httpd 重定向两个不同的端口?
【发布时间】:2018-03-20 17:25:09
【问题描述】:

我为我的 Web 应用程序设置了 SSL。此应用程序的标准端口是 8080,SSL 端口是 8443。我的 Apache httpd 配置有问题。我想重定向下面列出的所有网址:

http://1.2.3.4:8080
https://1.2.3.4:8080
http://1.2.3.4:8443

到这个网址:

https://1.2.3.4:8443

我把 httpd.conf 改成了这个:

Listen 1.2.3.4:8080

<VirtualHost *:8080>
    ServerName 1.2.3.4
    Redirect "/" "https://1.2.3.4:8443"
</VirtualHost>

上面列出的第一个 url 可以正常工作,但不能用于其他 url。我尝试了不同的配置,但它们不起作用。谁能帮我解决这个问题?

【问题讨论】:

    标签: apache .htaccess ssl httpd.conf


    【解决方案1】:
    https://1.2.3.4:8080
    http://1.2.3.4:8443
    

    如果 HTTP 在端口 8080 上提供服务,而 HTTPS (SSL) 在 8443 上,则上述 URL 均不可访问以触发重定向。 (?!)

    在您的配置中,HTTP 端口 8080,HTTPS 端口 8443。

    参考:
    https://serverfault.com/questions/359461/apache-answer-both-http-and-https-on-the-same-port

    【讨论】:

    • 好吧,你说得对,我明白了。但是当httpd监听8080端口时,有没有办法将http://1.2.3.4:8443重定向到https://10.200.200.91:8443
    • HTTP 重定向仅发生在请求在您的服务器上解决之后。如果 HTTP 正在侦听端口 8080,那么像 http://1.2.3.4:8443 这样的请求将永远无法正确解析,并且浏览器将超时。为了满足您的要求,您需要 Apache 在端口 8080 8443(已经与 SSL 共享?)上响应 HTTP - 这对于 Apache AFAIK 是不可能的。我认为这可能通过某种可以拦截和转移此类请求的前端代理来实现?
    【解决方案2】:

    在你的 httpd.conf 中配置 mod_rewrite 来实现这样的重定向

    例如:

    LoadModule rewrite_module  modules/mod_rewrite.so
    RewriteEngine on
    <VirtualHost 1.2.3.4:8080>
      <IfModule mod_rewrite.c>
        RewriteCond %{SERVER_PORT} !^8443$
        RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [L,R]
      </IfModule>
    </VirtualHost>
    

    LoadModule rewrite_module  modules/mod_rewrite.so
    RewriteEngine on
    RewriteCond %{SERVER_PORT} =8080
    RewriteRule ^(.*) https://%{HTTP_HOST}:8443/$1 [L,R]
    

    【讨论】:

    • 第一个示例代码对我不起作用。第二个与我在第一篇文章中的代码相同。
    • 您在这里所做的只是实现标准的 HTTP 到 HTTPS(临时)重定向,这不是 OP 所要求的。
    猜你喜欢
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    相关资源
    最近更新 更多