Http 到 Https AWS Elasticbeanstalk
Http to Https AWS Elasticbeanstalk
我正在为我的 Spring MVC Web 应用程序使用 AWS Elasticbeanstalk。我想将所有请求重定向到 https。我尝试遵循此 但这对我不起作用。此代码重定向到 https,但我的应用程序无法运行。它显示 "This page isn’t working"。供您参考的代码
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
假设您已经通过 HTTPS 访问您的网站时测试过 HTTPS 工作正常。如果没有,您可以添加此文件 .ebextensions/loadbalancer-terminatehttps.config
,内容如下:
option_settings:
aws:elb:listener:443:
ListenerProtocol: HTTPS
SSLCertificateId: arn:aws:acm:us-west-2:<your-account-id>:certificate/<certificate-arn-on-aws-acm>
InstancePort: 80
InstanceProtocol: HTTP
剩下的就是配置 Apache 配置实例,将使用 HTTP 访问您网站的客户端重定向到 HTTPS,将下面的代码添加到新文件中 .ebextensions/001_ssl_rewrite.config
阿帕奇 2.4+
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
RewriteEngine On
<If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</If>
Apache 2.2.X
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R,L]
您可以从 here
检查您的 Elastic Beanstalk 上安装了哪个 Apache
有关更多信息,请阅读这两个答案: and
我正在为我的 Spring MVC Web 应用程序使用 AWS Elasticbeanstalk。我想将所有请求重定向到 https。我尝试遵循此 但这对我不起作用。此代码重定向到 https,但我的应用程序无法运行。它显示 "This page isn’t working"。供您参考的代码
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
假设您已经通过 HTTPS 访问您的网站时测试过 HTTPS 工作正常。如果没有,您可以添加此文件 .ebextensions/loadbalancer-terminatehttps.config
,内容如下:
option_settings:
aws:elb:listener:443:
ListenerProtocol: HTTPS
SSLCertificateId: arn:aws:acm:us-west-2:<your-account-id>:certificate/<certificate-arn-on-aws-acm>
InstancePort: 80
InstanceProtocol: HTTP
剩下的就是配置 Apache 配置实例,将使用 HTTP 访问您网站的客户端重定向到 HTTPS,将下面的代码添加到新文件中 .ebextensions/001_ssl_rewrite.config
阿帕奇 2.4+
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
RewriteEngine On
<If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</If>
Apache 2.2.X
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [R,L]
您可以从 here
检查您的 Elastic Beanstalk 上安装了哪个 Apache有关更多信息,请阅读这两个答案: and