其他分享
首页 > 其他分享> > spring – Http to Https AWS Elasticbeanstalk

spring – Http to Https AWS Elasticbeanstalk

作者:互联网

我正在使用AWS Elasticbeanstalk作为我的Spring MVC Web应用程序.我想将所有请求重定向到https.我试着遵循这个How to force https on elastic beanstalk?,但这对我不起作用.此代码重定向到https但我的应用程序无效.它显示“此页面无效”.代码供您参考

<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

Apache 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}/$1 [R,L]

您可以从here检查Elastic Beanstalk上安装的Apache

有关更多信息,请阅读以下答案:https://stackoverflow.com/a/38751749/1772245https://stackoverflow.com/a/40670047/1772245

标签:redirect,https,spring,amazon-web-services,ebextensions
来源: https://codeday.me/bug/20190705/1388939.html