리버스 프록시의 역할과 정적파일을 처리하는 웹 서버.
리버스 프록시란?
외부 클라이언트에서 서버로 접근 시, 내부 서버로 접근할 수 있게 해주는 중개자 역할.
VM에 백엔드 서버가 돌아가고 있다. 백엔드 서버를 SSL화 하기 위하여 Nginx와 Letsencrypt를 사용하였다. nginx와 Letsencrypt를 VM에 설치하고 실행하였지만 포트 충돌이 발생하였다. 결국 nginx를 VM에 직접 설치하는 게 아닌 Nginx가 도커화 된 이미지를 VM에 직접 실행시키기로 하였다. Nginx 이미지가 Letsencrypt의 SSL 인증서를 인식하여 SSL 설정을 하기까지 제목과 같은 오류가 떴고 삽질과정과 해결방법은 다음과 같다:
우선, VM의 네트워크 방화벽 설정에는 문제가 없다는 걸 확인 후 'nginx -t'로 syntax오류를 점검하였지만 정상이었다. 'error.log'를 출력하고자 하였지만 아무것도 뜨지 않았다. SSL 점검해주는 온라인 사이트에 주소를 입력한 결과, SSL 인증서를 nginx가 전혀 인식하지 못하는 걸 발견하였다. nginx 설정 코드와 SSL인증서 인식의 문제로 보인다.
SSL Test tool
sudo cp /etc/letsencrypt/archive/your_domain_name/cert1.pem /etc/docker_certs/
sudo cp /etc/letsencrypt/archive/your_domain_name/privkey1.pem /etc/docker_certs/
sudo cp /etc/letsencrypt/archive/your_domain_name/chain1.pem /etc/docker_certs/
sudo cp /etc/letsencrypt/archive/your_domain_name/fullchain1.pem /etc/docker_certs/
/etc/docker_certs:/etc/nginx/ssl
docker exec -it <nginx-container-name> ls -l /etc/docker_certs
docker exec -it <nginx-container-name> cat /etc/nginx/conf.d/default.conf
include /etc/nginx/conf.d/*.conf;
...
services:
nginx:
volumes:
./nginx.conf:/etc/nginx/nginx.conf
tryon.conf
server {
# Redirect all HTTP traffic to HTTPS
listen 80;
server_name <your_domain_name_or_IP>;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name <your_domain_name_or_IP>;
ssl_certificate /etc/nginx/ssl/fullchain1.pem;
ssl_certificate_key /etc/nginx/ssl/privkey1.pem;
# Strong SSL Settings
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384';
ssl_prefer_server_ciphers off;
ssl_session_timeout 1d;
ssl_session_cache shared:MozSSL:10m; # about 40000 sessions
ssl_session_tickets off;
# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=63072000" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-Frame-Options "DENY" always;
add_header X-XSS-Protection "1; mode=block" always;
# Forward all requests to Django application running in a Docker container named 'django' on port 8000.
location / {
# add_header 'Access-Control-Allow-Origin'.
proxy_pass http://<"service name in docker-compose.yml">:8000; //One of services in docker-compose.yml
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 $scheme;
}
# The path inside the Nginx container to the static files.
location /static/ {
alias /usr/share/nginx/html/static/;
}
# The path inside the Nginx container to the media files.
location /media/ {
alias /usr/share/nginx/html/media/;
}
}
...
nginx:
volumes:
- ./tryon.conf:/etc/nginx/conf.d/tryon.conf
이로서, Nginx를 설치할 때 기본으로 깔려있는 'nginx.conf'에 'include /etc/nginx/conf.d/*.conf'가 존재하기에 nginx 컨테이너를 실행할 시 '/etc/nginx/conf.d/tryon.conf'가 적용된다.
이로서 SSL인증 문제가 해결되었다..
도커 컨테이너 실행 시 볼륨 마운트는 컨테이너 내부구조 구축과 그에 따른 SSL인증을 위해서도 아주 중요하다.