歡迎來到當 flask 遇上 docker,上集介紹了如何使用 Dockerfile 運行 flask,這集將會介紹如何用 Dockerfile + nginx 建立一個有 ssl 憑證的頁面。
- Step 0 – 準備 ssl 憑證
- Step 1 – 準備 Dockerfile 和 nginx.conf
- Step 2 – 運行 docker
此篇的完整程式碼放置於 GitHub,歡迎 git clone 使用。
Table
Step 0 – 準備 ssl 憑證
首先使用 openssl 建立開發測試用途的自簽憑證建立 ssl.csr 和 ssl.key
▍Mac & Window 安裝 openssl 方式
1.Window 安裝 openssl:openssl 官方載點
2.Mac 使用 homebrew 安裝 openssl:
1 |
$ brew install openssl |
▍使用 openssl 指令建立 ssl.csr & ssl.key
1 |
$ openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout ssl.key -out ssl.csr |
openssl 參數說明:
- req -x509 : 指定 request 要使用 X.509 憑證驗證
- nodes : 產生的憑證不需要密碼,不然每重新啟動 nginx 就要輸入一次
- days : 有效日期,3650 表示約 10 年
- newkey rsa:2048 : 我們需要同時產生 csr ( certificate singing request )與 key ( private key )檔並使用 2048 bits 的 RSA 加密
- keyout : key 輸出的位置與檔名,這裡測試填 ssl.key ,請自行更換
- out : csr 輸出的位置與檔名,這裡測試填 ssl.csr ,請自行更換
接下來輸入以下回答:
- 國家代碼,台灣就填 TW。
- 州或省,台灣就填 Taiwan。
- 城市,例如台北就填 Taipei。
- 公司名稱。
- 部門名稱。
- 伺服器的 FQDN,這個一定要填寫正確,如果沒有申請網域名稱的話,也可以用 IP 位址替代。(網域可以填寫如下: *. company.com )
- E-mail 信箱。
就會在資料夾中看到 ssl.csr 和 ssl.key囉!
關於更多 openssl 文章可以參考:https://blog.gtwang.org/linux/nginx-create-and-install-ssl-certificate-on-ubuntu-linux/
Step 1 – 準備 Dockerfile 和 nginx.conf
▍資料架構
file
├── Dockerfile
├── index.html
├── ssl.csr
├── ssl.key
└── nginx.conf
nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } server { listen 443 ssl; server_name localhost; # 憑證與金鑰的路徑 ssl_certificate /etc/nginx/ssl.csr; ssl_certificate_key /etc/nginx/ssl.key; location / { root /usr/share/nginx/html; index index.html index.htm; } } |
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Use the Nginx image FROM nginx COPY index.html /usr/share/nginx/html/ # Remove the default nginx.conf RUN rm /etc/nginx/conf.d/default.conf # Replace with our own nginx.conf COPY nginx.conf /etc/nginx/conf.d/ COPY ssl.csr /etc/nginx/ssl.csr COPY ssl.key /etc/nginx/ssl.key EXPOSE 443 |
Index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset=“UTF-8”> <meta name=“viewport” content=“width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <p>Helloworld</p> </body> </html> |
Step 2 – 運行 docker
建立成 image
1 |
$ docker image build -t docker_nginx_ssl . |
Run Container
1 |
$ docker run -p 80:80 -p 443:443 docker_nginx_ssl |
瀏覽 http://localhost/ 如果有看到 helloworld
瀏覽 https://localhost/ 會看到瀏覽器提示不安全 ( 因為是使用自簽憑證 ) 就代表成功囉!
最後~
此篇的完整程式碼放置於 GitHub,歡迎 git clone 使用。
更多 Flask 教學相關閱讀:
▍關於 Flask 教學系列目錄:
▍關於 Flask 使用 Docker 相關文章:
- 第一集:實作 Dockerfile + flask 教學 (附GitHub完整程式)
- 第二集:實作 Dockerfile + nginx + ssl + flask 教學 (附GitHub完整程式)
- 第三集:實作 Docker-compose (Flask+Nginx+PostgreSQL)
▍其他 Flask 相關教學:
- 【Flask教學系列】Flask 為甚麼需要 WSGI 與 Nginx
- 【Flask教學系列】Flask-SQLAlchemy 資料庫連線&設定入門 (一)
- 【Flask教學系列】Flask-JWT-Extended 實作
- 【Flask教學系列】實作 Flask CORS
- 【Flask教學系列】實作 Flask CSRF Protection
有關Max行銷誌的最新文章,都會發佈在Max的Facebook粉絲專頁,如果想看最新更新,還請您按讚或是追蹤唷!