전제조건
Elastic Beanstalk
를 통해 배포하고 있음Node.js
환경임Nginx
프록시를 사용하고 있음ACM
에 등록된 인증서가ELB (Classic)
에 연결되어 있음
HTTP to HTTPS
ELB에 ACM 인증서를 붙이는 것으로 https 통신을 쉽게 활성화하긴 했는데, 문제는 http 프로토콜로 여전히 접근이 가능합니다.
http로 요청이 들어오면 https로 redirect시키는 방법입니다.
이렇게 하세요.
프로젝트 루트에 .ebextensions
폴더를 만듭니다. 폴더 안에 https-redirect.config
파일을 만들고 아래 내용을 입력합니다.
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 8080;
server_name ##############;
if ($http_x_forwarded_proto != 'https') {
return 301 https://$server_name$request_uri;
}
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
access_log /var/log/nginx/access.log main;
location / {
proxy_pass http://nodejs;
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;
}
gzip on;
gzip_comp_level 4;
gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location /static {
alias /var/app/current/static;
}
}
/opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash -xe
rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
service nginx stop
service nginx start
container_commands:
removeconfig:
command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"
server_name ##############;
에서 ##을 본인의 도메인으로 변경합니다. 혹은 여러 도메인을 사용하신다면 $host
를 사용해도 됩니다.
이제 앱을 배포하면 http 요청이 https로 redirect됩니다.
삽질한 내용
위 과정으로 배포에 성공하셨나요? 축하합니다. 하지만 여전히 오류가 발생한다면 혹시 놓친 부분이 있는지 제가 삽질한 내용을 훑어보세요.
.ebextensions
폴더 이름이 .
으로 시작합니다. 혹시 ebextentions로 폴더를 만드셨나요? tions
가 아니라 sions
입니다.
뭔가 이상한 포트번호
Elastic Beanstalk
로 Node.js
환경을 생성할 때 프록시로 nginx
가 기본으로 선택되어 있는데요. (물론 바꿀 수 있습니다.) 이 경우 사용되는 포트는 총 세개, 80
, 8080
, 8081
입니다.
- Node.js가 8081 포트로 앱을 시작하고요. 이건
/var/log/nodejs/nodejs.log
를 보면 확인하실 수 있어요. - nginx는 8080포트로 받은 요청을 node.js에 넘겨줘요.
- 그런데 security group에서는 8080포트가 아닌 80포트만 열려있죠.
자세한 내용은 이 링크를 참조하세요.
아무튼, 우리가 수정해야 하는 포트는 nginx가 받을 수 있는 8080
포트입니다. 8080으로 http요청이 오면 https로 redirect시켜야 합니다.
환경마다 다른 설정방법
node.js환경과 java환경의 .ebextensions
설정 방법이 다릅니다. 이 링크를 참고하세요.
nginx 구성 확장으로는 http to https가 되지 않음
./ebextensions/nginx/conf.d/
경로에 *.conf
파일을 생성하면 nginx 구성을 확장할 수 있습니다. 하지만 https redirect의 경우엔 전체 설정을 덮어쓰기해야 합니다.