Elastic Beanstalk에서 https를 강제하는 방법은 무엇입니까?
Elastic Beanstalk의 무료 사용 계층에서 https를 강제 할 수없는 것 같습니다.
상태 확인에 실패하지 않고 Amazon Elastic Beanstalk에서 https를 강제하는 방법 에서 다음 제안을 시도했습니다.
이 Apache 재 작성 규칙 사용
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/status$
RewriteCond %{REQUEST_URI} !^/version$
RewriteCond %{REQUEST_URI} !^/_hostmanager/
RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
시도하면 http 요청이 내가 원하는대로 https로 리디렉션되지 않습니다. 대신 http 페이지가 정상적으로로드됩니다. 또한 동일한 결과로 X-Forwarded-Port 헤더를 사용하려고 시도했습니다.
다음 재 작성 규칙도 시도했습니다.
RewriteCond %{SERVER_PORT} 80
RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
그리고이 규칙은 리디렉션 루프를 발생시킵니다. 따라서 아파치 재 작성 규칙이 Elastic Load Balancer 헤더 X-Forwarded-Port 및 X-Forwarded-Proto를 선택하지 않는 것처럼 보이지만 리디렉션 루프도 제가 원하는 것이 아닙니다.
도와주세요. 저는 AWS, Elastic Beanstalk를 처음 사용하며 Apache 규칙에 익숙하지 않습니다. 여기서 어디로 가야할지 모르겠습니다. 감사.
이 답변은로드 밸런서 보안 그룹에서 이미 https를 활성화하고, SSL 인증서를로드 밸런서에 추가하고,로드 밸런서가 전달하는 포트에 443을 추가하고, Route 53을 사용하여 Elastic Beanstalk 환경에서 도메인 이름을 가리켰다 고 가정합니다 (또는 동등한 DNS 서비스).
참고 :이 답변은 Apache를 사용하는 Elastic Beanstalk 환경을위한 것입니다. Docker 기반 배포에서는 작동하지 않을 수도 있습니다.
프로젝트 디렉토리 .config
에있는 파일.ebextensions
중 하나에 다음을 추가 하기 만하면됩니다 .
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>
설명
이것은 Elastic Beanstalk 외부에서 적당히 간단합니다. 일반적으로 다음과 같은 Apache 재 작성 규칙을 추가합니다.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
또는이 경우와 같이로드 밸런서 뒤에있는 경우 :
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
그러나 이러한 구성은 <VirtualHost>
블록 내에서만 작동합니다 . RewriteCond
를 <If>
블록으로 변경하면 블록 외부에서 제대로 작동 <VirtualHost>
하여 독립형 Apache 구성 파일에 넣을 수 있습니다. CentOS의 표준 Apache 설정 (ElasticBeanstalk 설정 포함)에는 /etc/httpd/conf.d/*.conf
이 파일을 저장하는 파일 경로와 일치 하는 모든 파일 이 포함됩니다.
-n '%{HTTP:X-Forwarded-Proto}'
조건 의 일부는로드 밸런서 뒤에 있지 않은 경우 리디렉션을 방지하므로로드 밸런서 및 https가있는 프로덕션 환경과 단일 인스턴스이고 https가없는 스테이징 환경간에 구성을 공유 할 수 있습니다. 모든 환경에서로드 밸런서와 https를 사용하는 경우에는 필요하지 않지만 사용하는 것은 문제가되지 않습니다.
내가 본 나쁜 솔루션
나는이 문제에 대한 많은 나쁜 해결책을 보았고,이 해결책이 왜 필요한지 이해하기 위해 그것들을 살펴볼 가치가 있습니다.
Cloudfront 사용 : 일부 사람들은 Elastic Beanstalk 앞에서 캐시되지 않은 Cloudfront 설정을 사용하여 HTTP에서 HTTPS로 리디렉션 할 것을 제안합니다. 이는 완전히 적절하지 않은 완전히 새로운 서비스 (따라서 복잡성을 추가 함)를 추가합니다 (Cloudfront는 CDN이며 고유 한 동적 콘텐츠에 HTTPS를 강제하는 데 적합한 도구가 아닙니다). Apache 구성은이 문제에 대한 일반적인 솔루션이며 Elastic Beanstalk는 Apache를 사용하므로 이것이 우리가 가야 할 방식입니다.
SSH를 서버에 연결하고 ... : 이것은 Elastic Beanstalk의 관점과 완전히 반대이며 많은 문제가 있습니다. 자동 확장으로 생성 된 모든 새 인스턴스에는 수정 된 구성이 없습니다. 복제 된 환경에는 구성이 없습니다. 적절한 환경 변경 세트의 수에 관계없이 구성이 지워집니다. 이것은 정말 나쁜 생각입니다.
Apache 구성을 새 파일로 덮어 쓰기 : 올바른 솔루션 영역에 들어가지만 Elastic Beanstalk가 서버 설정의 측면을 변경하면 유지 관리에 악몽이 남습니다 (매우 잘 할 수 있음). 또한 다음 항목의 문제를 참조하십시오.
Apache 구성 파일을 동적으로 편집하여 몇 줄을 추가하십시오. 이것은 괜찮은 아이디어입니다. 이 문제는 Elastic Beanstalk가 기본 Apache 구성 파일의 이름을 변경하면 작동하지 않으며 예상치 못한 상황에서이 파일을 덮어 쓸 수 있다는 것입니다. https://forums.aws.amazon.com/thread .jspa? threadID = 163369
편집 :이 답변을 좋아하지만 이제는 매우 오래되었습니다 . AWS는 이 답변의 일부를 쓸모 없게 만드는 새로운 서비스 (예 : Certificate Manager )를 내놓았습니다. 또한
.ebextensions
Apache와 함께 폴더를 사용하면 위에서 설명한 것처럼이 리디렉션을보다 깔끔하게 처리 할 수 있습니다.
S3에서 웹 사이트를 호스팅하는 경우이 답변의 일부가 여전히 유용 할 수 있습니다.
이것은 나를 위해 일했습니다.
aws
콘솔 명령을 사용하여 AWS에 인증서를 업로드합니다 . 명령 구조는 다음과 같습니다.aws iam upload-server-certificate --server-certificate-name CERTIFICATE_NAME --certificate-body "file://PATH_TO_CERTIFICATE.crt" --private-key "file://YOUR_PRIVATE_KEY.pem" --certificate-chain "file://YOUR_CERTIFICATE_CHAIN.ca-bundle" --path /cloudfront/
Elastic Beanstalk 애플리케이션에서 구성 -> 네트워크 계층 -> 로드 밸런싱 으로 이동하고 기어 아이콘을 클릭 합니다 .
보안 리스너 포트 를 443 으로 선택하십시오 . 프로토콜 을 HTTPS 로 선택합니다 . SSL 인증서 ID에 대해 2 단계
CERTIFICATE_NAME
에서 선택합니다 . 구성을 저장하십시오.콘솔로 이동합니다 . EC2 인스턴스를 클릭합니다 . Load Balancers를 클릭합니다 . 부하 분산기를 클릭합니다. 인스턴스를 클릭 하고 아래로 스크롤하여 해당로드 밸런서에 할당 된 EC2 인스턴스를 확인합니다. EC2 인스턴스의 이름이 애플리케이션 URL과 같거나 가까운 경우로드 밸런서 의 DNS 이름 을 기록해 둡니다. 형식이어야합니다.
awseb-e-...
콘솔로 돌아갑니다 . CloudFront를 클릭 합니다. 배포 만들기를 클릭합니다 . 웹 배포를 선택합니다 .
배포를 설정합니다. Origin Domain Name 을 5 단계 에서 찾은로드 밸런서 DNS 이름으로 설정합니다 . HTTP를 HTTPS 로 리디렉션 하도록 뷰어 프로토콜 정책 을 설정합니다 . 설정 앞으로 쿼리 문자열 에 예 . 설정 대체 도메인 이름 (CNAME이) 의 URL (들)은 응용 프로그램에 사용할. 2 단계 에서 업로드 한 SSL 인증서 를로 설정 합니다 . 배포판을 만드십시오.
CERTIFICATE_NAME
CloudFront에서 배포 이름을 클릭합니다. Origins를 클릭 하고 원본을 선택한 다음 Edit를 클릭 합니다. Origin Protocol Policy 가 Match Viewer 인지 확인합니다 . 돌아 가세요. 동작을 클릭 하고 원본을 선택한 다음 편집을 클릭 합니다. Forward Headers 를 Whitelist로 변경 하고 Host를 추가 합니다. 저장.
참고 : 더 긴 가이드도 썼습니다 .
가장 많이 찬성하는 것은 나를 위해 작동하지 않습니다 .. <If> 지시문은 Apache 2.4+에서만 작동하지만 ElasticBeanstalk 에는 버전 2.2.x가 있습니다.
따라서 위와 동일한 조언을 따르십시오. 다음 콘텐츠로 .ebextensions / https_rewrite.config라는 파일을 생성합니다.
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]
이것은 나를 위해 일하는 것 같습니다.
이 파일을 WAR 파일로 빌드하는 방법에 대해서는이 답변을 참조하십시오.
편집 : Zags 솔루션 이 더 일반적이고 정확합니다. 나는 그것을 추천한다 (파이썬 환경에 특정한)
다음은 wsgi.conf를 해킹하거나 CloudFront를 사용하지 않는 깨끗하고 빠른 솔루션입니다.
.ebextensions / some_file.config에서 :
# Redirect HTTP to HTTPS
"/etc/httpd/conf.d/https_redirect.conf":
mode: "000644"
owner: root
group: root
content: |
<Directory /opt/python/current/app/>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</Directory>
너무 쉬운 것 같지만 잘 작동하는 것 같습니다.
또한 "HTTPS가 아님"대신 HTTP를 명시 적으로 리디렉션하고 있습니다.
2018 년에로드 밸런서로 Elastic Beanstalk를 리디렉션하려고합니다. 위의 답변 중 어느 것도 내 환경에서 작동하지 않습니다. 내가 말한 몇 가지 문제 :
가장 많이 투표 한 답변을 시도했지만 내 바람둥이는 버전 2.7입니다. 지원하지 않습니다.
container_commands를 사용하고 00_applications 설정을 복사했습니다. AWS는이를 무시합니다.
그래서 마침내 https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat-proxy.html 을 읽고 작동했습니다.
내가하는 일은 다음과 같습니다.
폴더 구조를 다시 만들었습니다.
.ebextensions
- httpd
-conf.d
-ssl.conf
그리고 이것은 ssl.conf의 내용입니다.
<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>
이것이 도움이되기를 바랍니다.
다음 명령으로 나를 위해 작동합니다.
RewriteCond %{HTTP:X-Forwarded-Port} !=443
https 확인없이 :
RewriteCond %{HTTP:X-Forwarded-Proto} !https
ELB가 X-Forwarded-Proto의 값을 http로 변경하는 것처럼 보입니다 (TCP 프로토콜에서도).
위의 답변 중 어느 것도 나를 위해 일하지 않았지만 일부는 나를 위해 일한 답변을 알아내는 데 도움이되었습니다. 또한 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-tomcat에 도움이 된 아래 URL을 찾았습니다. -platform.html
2 개의 파일 httpd.conf 00_application.conf를 변경하기 위해 위의 URL에서 언급 한 파일 구조를 만들었습니다.
인스턴스에서 전체 httpd.conf를 복사하고 위 링크에 언급 된 폴더 구조 아래의 .ebextention 아래 코드에 넣습니다. 그런 다음 프로젝트의 해당 파일에 아래 줄을 추가하십시오.
LoadModule rewrite_module modules/mod_rewrite.so
00_application.conf에 대해 동일한 작업을 수행하고 인스턴스에서 복사하여 httpd / conf.d / elasticbeanstalk / 00_application.conf 아래의 .ebextention 아래에있는 코드베이스에 배치합니다. 이제이 파일을 편집하고 VirtualHost 사이에 아래를 추가합니다.
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
이제 코드를 배포하면 작동합니다.
Elastic Beantalk에서는 AWS가이를 덮어 쓰도록 on 구성을 추가하기 만하면 웹 서버 구성을 덮어 쓰고 자신의 구성을 제출할 수 있습니다.
경로 아래에 다음 파일을 추가하기 만하면됩니다. .ebextensions \ httpd \ conf.d
파일 내용 :
<VirtualHost *:80>
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
'.ebextensions'는 AWS의 표준 구성 폴더이고 나머지는 덮어 쓰려는 파일과 폴더를 가리 킵니다. 파일이나 폴더가 존재하지 않으면 간단하게 만드십시오.
나는 이것을 알아내는 데 어려움을 겪었으므로 해결책 을 찾은 후 다른 사람을 돕기 위해 해결책 에 대한 자세한 설명을 작성했습니다 . 이는 Tomcat 8, Apache2 및 Spring Boot 앱에만 해당됩니다. AWS 랩 github 에는 정말 유용한 ebextension 예제가 있습니다 .
나를 위해 일한 요약 :
- /src/main/webapp/.ebextensions/httpd/conf.d/elasticbeanstalk.conf에 파일을 생성합니다.
- "LoadModule rewrite_module modules / mod_rewrite.so"를 포함하도록주의하면서 재 작성 조건 / 규칙을 추가합니다.
- AWS EBS에 배포
다음은 Spring Boot 앱 의 예 입니다.
Elastic Beantalk에 대한 다음 구성이 있습니다 (Tomcat 8 Java 8을 실행하는 64 비트 Amazon Linux 2016.09 v2.3.1). .ebextensions 디렉터리를 만들고 다시 쓰기 조건이있는 .config YAML 파일을 추가했습니다.
위에서 설명한 Zagas 솔루션 (매우 복잡함)은 저에게 적합하지 않습니다.
- "If"조건을 알 수 없기 때문에
- Apache 2.2 때문에 httpd.conf 파일에 mod_rewrite.so가 포함되어 있지 않습니다.
이 솔루션 은 나에게 더 의미가 있지만 작동하지 않습니다. 아무 일도 일어나지 않고 "conf.d"디렉토리에서 "ssl_rewrite.conf"파일을 볼 수 없습니다.
세 번째 시도 된 해결책은 ".ebextendsion"디렉토리 아래에 "run.config"및 "ssl_rewrite.conf"파일을 추가하는 것입니다.
run_config 포함
container_commands:
copy-config:
command: "cp .ebextensions/ssl_rewrite.conf /etc/httpd/conf.d"
ssl_rewrite.conf는 다음을 포함합니다.
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
ssl_rewrite.conf는 "conf.d"디렉토리 아래에 생성되지만 http에서 https 로의 리디렉션이 작동하지 않습니다.
저에게 유일한 해결책은 "/etc/httpd/conf.d/elasticbeanstalk/00_application.conf"에 다음 줄을 추가하는 것입니다.
<VirtualHost *:80>
......
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
......
</VirtualHost>
그러나 이것은 임시 해결책이며 시스템이 교체되면 내 https 리디렉션이 사라집니다.
누군가가 여전히 어려움을 겪고있는 경우를 대비하여 :
얼마 동안 어려움을 겪었고 마침내 모든 AWS 구성이 포함 된 GitHub (AWS 팀에서 제공)를 찾았으며 아래 예제는 Apache 2.2 용 HTTP> HTTPS 리디렉션에서 작동합니다. (Apache 2.4 및 Nginx에 대한 구성은 아래 링크를 참조하십시오).
Apache 2.2
앱의 루트 디렉토리에 파일을 생성합니다. YOUR_PROJECT_ROOT / .ebextensions / httpd / conf.d / elasticbeanstalk.conf (IntelliJ / Java를 사용하는 경우 최종 .WAR 아티팩트에 추가되었는지 확인하십시오)
가상 호스트에서 리디렉션을 활성화하려면 다음 줄을 추가하십시오.
<VirtualHost *:80> LoadModule rewrite_module modules/mod_rewrite.so RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} <Proxy *> Order deny,allow Allow from all </Proxy> ProxyPass / http://localhost:8080/ retry=0 ProxyPassReverse / http://localhost:8080/ ProxyPreserveHost on ErrorLog /var/log/httpd/elasticbeanstalk-error_log </VirtualHost>
Apache 2.4 및 Nginx에 대한 더 많은 예제를 보려면 다음 GitHub 저장소를 방문하십시오.
또한 더 많은 유용한 구성과 예제를 사용할 수 있습니다.
문안 인사
환경 변수를 통해 HTTPS 활성화
Elastic Beanstalk에도 있지만로드 밸런서를 사용하지 않는 (따라서 인증서를 직접 할당 할 수없는) 개발 및 스테이징 환경이 아닌 프로덕션 환경에만 HTTPS를 적용해야했습니다.
환경 변수를 사용합니다 USE_HTTPS
. 로 설정된 ssl_rewrite.conf
경우에만 파일을 복사합니다 .USE_HTTPS
true
.ebextensions / files / ssl_rewrite.conf
RewriteEngine On
<If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</If>
.ebextensions / https.config
files:
"/home/ec2-user/https_setup.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
echo "USE_HTTPS env var: ${USE_HTTPS,,}"
outfile=/etc/httpd/conf.d/ssl_rewrite.conf
if [ "${USE_HTTPS,,}" == "true" ]; then
echo "Configure SSL rewrite"
cp .ebextensions/files/ssl_rewrite.conf $outfile
chmod 644 $outfile
chown root:root $outfile
else
[ -f $outfile ] && rm $outfile
echo "Do not use SSL"
exit 0
fi
container_commands:
01_https_setup:
command: "/home/ec2-user/https_setup.sh"
을 변경하는 경우 USE_HTTPS
변경 사항을 적용하려면 애플리케이션을 다시 배포해야합니다. 원하는 경우 파일 에서 echo
명령을 제거 할 수도 있습니다 https.config
.
.htaccess 파일을 루트 폴더에 넣지 않는 이유는 무엇입니까? 이렇게하면 간단히 테스트하고 디버깅 할 수 있습니다. .zip에 포함하면 자동으로 모든 인스턴스에 다시 배포됩니다.
간단히 사용 .htaccess
:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
가장 많이 득표 한 답변은 이제 조금 오래되었습니다. A Paul의 대답은 실제로 정답입니다. 그의 답변에 제공된 링크는 AWS에서 제공 한 것입니다 (따라서 Elastic Beanstalk에서 애플리케이션을 실행할 때 HTTP에서 HTTPS로 리디렉션하도록 Apache 구성을 재정의하는 것이 권장되는 방법).
주목해야 할 매우 중요한 한 가지가 있습니다. 둘 이상의 웹앱을 배포하는 경우 웹앱 중 하나에 .ebextensions 폴더를 추가하면 작동하지 않습니다. 지정한 구성 중 Non가 작성 또는 생성되고 있음을 알 수 있습니다. Elastic Beanstalk 환경에 여러 웹 앱을 배포하는 경우 AWS Java Tomcat 의이 문서를 읽어야 합니다. Elastic Beanstalk에 여러 WAR 파일 배포
일반적으로 WAR 파일을 배포하기 위해 eb 명령을 실행하기 전에 다음과 같은 구조가 필요합니다.
MyApplication.zip
├── .ebextensions
├── foo.war
├── bar.war
└── ROOT.war
.ebextentions 폴더가 각 WAR 파일에있는 경우 완전히 무시되고 구성 변경이 수행되지 않음을 알 수 있습니다.
이것이 다른 사람에게 도움이되기를 바랍니다.
우리는 X-Forwarded-Proto
제대로 처리하여 백엔드에서 문제를 해결했습니다 .
이것은 Grails 구성이지만 아이디어에 도움이 될 것입니다.
grails.plugin.springsecurity.secureChannel.useHeaderCheckChannelSecurity = true
grails.plugin.springsecurity.portMapper.httpPort = 80
grails.plugin.springsecurity.portMapper.httpsPort = 443
grails.plugin.springsecurity.secureChannel.secureHeaderName = 'X-Forwarded-Proto'
grails.plugin.springsecurity.secureChannel.secureHeaderValue = 'http'
grails.plugin.springsecurity.secureChannel.insecureHeaderName = 'X-Forwarded-Proto'
grails.plugin.springsecurity.secureChannel.insecureHeaderValue = 'https'
grails.plugin.springsecurity.secureChannel.definition = [
[pattern: '/**', access: 'REQUIRES_SECURE_CHANNEL']
]
이 질문에 대한 또 다른 두 가지 답변을 확장하려면 https://stackoverflow.com/a/43026082/8775205 , https://stackoverflow.com/a/42035023/8775205 . ELB를 사용하여 AWS에 서비스를 배포하고 단계별 가이드가 필요한 스프링 부트 사용자의 경우 프로젝트의 src / main / webapp / .ebextensions / httpd / conf.d / 아래에 ****. conf 파일을 추가 할 수 있습니다. .
src
--main
----java
----resources
----webapps
------.ebextensions
--------httpd
----------confd
------------****.conf
****.conf looks like the following. Noticed that I have my testing site with a single instance, so I add a condition to exclude it.
<VirtualHost *:80>
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_USER_AGENT} !ELB-HealthChecker
RewriteCond %{HTTP_HOST} !testexample.com #excludes test site
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
After this, remember to add a "resource" under maven-war-plugin in your pom.xml in order to pick up the above configuration.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<!-- some other resource configured by yourself-->
</resource>
<resource>
<directory>src/main/webapps/.ebextensions</directory>
<targetPath>.ebextensions</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
<version>2.1.1</version>
</plugin>
Finally commit and push your code, wait AWS codebuild and codepipeline to pick up your code from your repository and deploy to beanstalk environment, or simply pack your project into a war file and upload it to your AWS beanstalk environment
AWS do not accept unserscores (_) in headders, while we can use (-), So Remove underscores from the header variables, example:- header_var_val = "some value" replace it with headervarval = "some value". It works for me.
With the new Application Load Balancers you can do this fairly trivially now...
Ensure you setup one of these at the time you setup an EB environment (still defaults to classic load balancer I believe). You could not change the type once the environment is created, so recreate it
Once this is done, go to your EC2 settings -> Load Balancers. Click on the load balancer you created for your EB environment. You must ensure that you have setup a HTTPS listener prior to this task so make sure you listen on HTTPS 443 with an SSL cert and forward traffic to your instances with HTTP on 80.
Then add a new listener which listens on HTTP and add a default action of "Redirect to:". Make sure you set HTTPS as the protocol, 443 as the port, "Original host, path, query" as the option and finally 301 as the HTTP response code.
Once this listener is added ensure that you update your EC2 Load Balancer security group to accept both HTTPS and HTTP connections, you will see small warning sign on the listener to remind you!
Chris
If you use a Load Balanced environment you can follow the instructions for Configuring HTTPS for your AWS Elastic Beanstalk Environment and at the end disable the HTTP port.
Note that currently the AWS Free Usage Tier includes the same amount of hours for an Elastic Load Balancing (ELB) as for an EC2 Micro Instance.
this is an easy solution
- ssh into your EC2 instance
- copy the contents of /etc/httpd/conf.d/wsgi.conf into a local file called wsgi.conf which will be placed in the base folder of your application
Edit the local version of wsgi.conf and add the following redirect rules within the < VirtualHost> < /VirtualHost> tags
RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule !/status https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301]
Change the “/status” to whatever page you are using as a health check page.
- Save the file
Edit your < app>.conf file inside your .ebextensions directory to add a container command to copy this version of wsgi.conf over Amazon’s version
container_commands: 01_syncdb: command: "django-admin.py syncdb --noinput" leader_only: true 02_collectstatic: command: "django-admin.py collectstatic --noinput" 03_wsgireplace: command: 'cp wsgi.conf ../wsgi.conf' ...
Deploy the code.
- The deployed version of wsg.conf at /etc/httd/conf.d/wsgi.conf will now include the necessary redirect rules.
It should work and the file will be properly updated for each deployment. The only thing to watch for is if Amazon changes their base wsgi.conf file contents in the future, then your copy may no longer work.
Autor rickchristianson
참고 URL : https://stackoverflow.com/questions/14693852/how-to-force-https-on-elastic-beanstalk
'Development Tip' 카테고리의 다른 글
페이지로드 후 jquery 코드를 수행하는 방법은 무엇입니까? (0) | 2020.11.09 |
---|---|
Mongoid 설치 후 액티브 레코드 생성기를 사용하십니까? (0) | 2020.11.09 |
NA 만 포함하는 열을 삭제하는 방법은 무엇입니까? (0) | 2020.11.09 |
PIL 오류 정보 — IOError : 디코더 zip을 사용할 수 없습니다. (0) | 2020.11.09 |
32/64 비트 또는 CPU 용 C # 컴파일? (0) | 2020.11.09 |