首页 > 解决方案 > 如何在 Elastic Beanstalk 单实例环境中将 HTTP 重定向到 HTTPS

问题描述

我使用 Amazon Linux 2 在 Elastic Beanstalk 单实例环境中部署了一个 Spring Boot Web 应用程序。我已根据文档在 NGNIX 中配置了 SSL,并且所有 HTTPS 请求都工作正常。但是,HTTP 请求不会重定向到 HTTPS。下面是我的conf文件位于\PROJECT_ROOT\.platform\nginx\conf.d\https.conf

# HTTP server
server {
    listen 80;
    return 301 https://example.com$request_uri;
}

# HTTPS server
server {
    listen 443 ssl;
    ssl_certificate      /etc/pki/tls/certs/server.crt;
    ssl_certificate_key  /etc/pki/tls/certs/server.key;
    
    ssl_session_timeout  5m;
    
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers   on;
    
    location / {
        proxy_pass  http://localhost:5000;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto https;
    }
}

我创建了一条A 记录以映射example.com到 EB 环境 URL。

但是,当我尝试点击http://example.com它时,只需通过 HTTP 加载主页,而不是重定向到 HTTPS。

有人可以帮我吗?

标签: amazon-web-servicesspring-bootnginxamazon-elastic-beanstalk

解决方案


推荐阅读