iT邦幫忙

2021 iThome 鐵人賽

DAY 11
0
DevOps

以 Docker 為始的多種開源服務初探系列 第 11

Day 11 利用 docker 安裝 nginx 並配置 https

現今的瀏覽器如 Firefox, Google Chrome 多以將僅 http 的網站和連結標註為不安全,所以於前一天所安裝的 Mautic 服務若要能正常的於客戶端運行,勢必是得用 https:// 開頭的網址。

安裝 nginx

  1. 先行解決 WARN No swap limit support 問題,需執行以下指令:
sudo vim /etc/default/grub
GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
sudo update-grub
  1. 將 OS 重新開機
sudo systemctl reboot
  1. 先從 docker 官網上拉下最新的 nginx 映像
docker pull nginx:latest
  1. 利用 FileZilla 或是命令列來建立 nginx 的配置文件和 SSL 證書相關的資料夾
# 這些資料夾和檔案,到時會藉由 mapping 的形式映射的 nginx 容器內的對應資料夾中
mkdir /opt/docker/nginx/conf.d -p
mkdir /opt/docker/nginx/cert -p
  1. 將申請下來的 SSL 憑證檔案,上傳到 /opt/docker/nginx/cert 中
  2. 申請下來的 SSL 憑證,在 nginx 上是需要先行 merge 的。請透過命令列執行以下指令
cd /opt/docker/nginx/cert
cat certificate.crt ca_bundle.crt >> ca_bundle_combine.crt
  1. 配置 nginx.conf
# 可以先用 notepad+ 等工具先行編輯好,
# 再透過如 FileZilla 工具上傳
server {
    listen 443 ssl;
    server_name <subdomain / domain name>;
    # 到時是由容器中的 /etc/nginx/ 開始計算相對位置
    ssl_certificate ./cert/ca_bundle_combine.crt;
    ssl_certificate_key cert/private.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;

    client_max_body_size 1024m;

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://<你的 domain 實際ip>:8080/;
    }
}
server {
     listen 80;
     server_name mautic.smlpoints.com;
     return 301 https://$host$request_uri; 
}
  1. 將編寫好的 nginx.conf 上傳至 /opt/docker/nginx/conf.d
  2. 啟動 nginx 服務
docker run -itd --name nginx -p 80:80 -p 443:443 -v /opt/docker/nginx/conf.d/nginx.conf:/etc/nginx/conf.d/nginx.conf -v /opt/docker/nginx/cert:/etc/nginx/cert -m 100m nginx
  1. 使用 docker ps 看是否有成功啟用一個名為 nginx 的容器
  2. 無論成功與否,都可使用 docker logs nginx 來查看 log

參考資料

  1. Installing SSL Certificate on NGINX
  2. Docker 遇到 WARN No swap limit support 修復
  3. docker安装nginx并配置https
  4. SSL: error:0B080074:x509 certificate routines:X509_check_private_key:key values mismatch

上一篇
Day 10 利用 Cloudflare 來管理你的網域和子網域
下一篇
Day 12 docker 安裝 nginx 後的細部設定
系列文
以 Docker 為始的多種開源服務初探30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言