【问题标题】:mod_rewrite - Port 80 does not change to 443 when HTTP is explicitly requestedmod_rewrite - 明确请求 HTTP 时,端口 80 不会更改为 443
【发布时间】:2018-06-12 03:42:01
【问题描述】:

我有一个应用程序部署到 Elastic Beanstalk,其 Tomcat 容器使用 Google OpenID Connect 进行身份验证。我想将所有http 请求重定向到https,为此我在.ebextensions 的文件中有以下mod_rewrite 配置-

files:
    "/etc/httpd/conf.d/ssl_rewrite.conf":
        mode: "000644"
        owner: root
        group: root
        content: |
            LoadModule rewrite_module modules/mod_rewrite.so
            RewriteEngine On
            RewriteCond %{HTTP:X-Forwarded-Proto} =http
            RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]

Google OAuth2 凭据控制台将 https://example.com/j_security_check 作为授权重定向 URL。当请求example.comhttps://example.com 时,配置工作正常,因此应用程序被重定向到提到的授权URL。

但是,当明确请求 http - http://example.com - 应用程序被重定向到 httpsport 80 仍在使用中。然后授权的重定向 URL 变为https://example.com:80/j_security_check,我得到Error: redirect_uri_mismatch

如何将明确的http 请求重定向到https,并将端口更改为443?主要目标是匹配提到的授权重定向 URL。如果可能的话,我想用.ebextensions 配置文件或类似的解决方案来实现它。

【问题讨论】:

    标签: apache amazon-web-services mod-rewrite amazon-elastic-beanstalk google-openid


    【解决方案1】:

    你能这样吗。如果成功了,我会给你解释。

    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    

    【讨论】:

    • 这就是我最初所拥有的。它没有用。问题中提到的规则是从AWS' documentationhttp 流量重定向到https
    【解决方案2】:

    问题不在于重写规则。该文件必须放置在.ebextensions 中的特定路径中才能在Tomcat 8 中工作。配置文件也必须进行不同的设置。提供的大多数示例都不适用于 Tomcat,因此我最终将它们放在错误的位置。

    什么有效 -

    /.ebextensions/httpd/conf.d/myconf.conf,放置-

    LoadModule rewrite_module modules/mod_rewrite.so
    

    /.ebextensions/httpd/conf.d/elasticbeanstalk/00_application.conf 中放置-

    <VirtualHost *:80>
      <Proxy *:80>
        Order Allow,Deny
        Allow from all
      </Proxy>
      ProxyPass / http://localhost:8080/ retry=0
      ProxyPassReverse / http://localhost:8080/
      ProxyPreserveHost on
    
      RewriteEngine On
      RewriteCond %{HTTP:X-Forwarded-Proto} =http
      RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
    
      ErrorLog /var/log/httpd/elasticbeanstalk-error_log
    </VirtualHost>
    

    注意使用.conf 文件而不是.config。这很重要!

    另外,我得到的重定向不是真实的。我没有密切注意,因为当我请求example.com 时,浏览器缓存正在为我服务https://example.com。它实际上并没有将http 请求重定向到https

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 2017-05-31
      • 2023-03-24
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多