使用AWS ELB将所有http流量重新路由到https

use*_*753 10 apache mod-rewrite amazon-web-services amazon-elb

所以我查看了其他类似的问题,他们提供了解决方案,但似乎没有一个问题出于某种原因.所以,对于初学者来说,我的ELB就是这样设置的

HTTP (incoming) -> HTTP (instance)
HTTPS (incoming) -> HTTP (instance)
Run Code Online (Sandbox Code Playgroud)

因此,两个流量都应该在端口80上进行.这就行了,就像我使用http://mydomain.comhttps://mydomain.com访问我的网站一样,它能够显示,即使我只有一个VirtualHost用于在80号港口.

问题是尝试将所有http流量重写为https.我用它来做基于端口(检查是否!443并重写为https),但现在一切都进入80,这将无法工作.所以我正在运行一个Apache服务器并有这个重写规则

RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [OR,NC]    # Added
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^/?(.*) https://mydomain.com%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

但它似乎永远不会起作用.我还缺少其他线路吗?有没有办法检查它是否达到了这个条件?我试过了两个!httpshttp作为条件,但都没有奏效.

编辑:稍微将我的RewriteRule改为现在的状态,它仍然无法正常工作.我添加了一个额外的条件来重写www,这是有效的.HTTP:X-Forwarded-Proto要么没有,要么没有由负载均衡器设置

编辑:错误真的很愚蠢.我只是简单地连接到错误的实例.谢谢你忍住我的愚蠢

hem*_*mc4 6

要从http重写为https,请使用以下规则.
还要检查你的mod重写是否已启用并正常工作.

RewriteEngine On
# This will enable the Rewrite capabilities

RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS for "normal" conditions

RewriteCond %{HTTP:X-Forwarded-Proto} !https
# This checks the connection is not already HTTPS for AWS conditions

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e.  http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
Run Code Online (Sandbox Code Playgroud)

  • 该解决方案*不能与ELB一起进行https-> http转换,并将创建无限重定向循环.正确的解决方案(当存在负载均衡器时)实际上是要查看X-Forwarded-Proto的HTTP头,并相应地重定向. (7认同)
  • 添加了上面缺少的条件:RewriteCond%{HTTP:X-Forwarded-Proto}!https (3认同)

Knu*_*nut 4

这只是你的RewriteRule无效。请参阅这篇文章了解它的外观。