Docker 一分鐘完成自動更新 SSL 証書的 nginx-proxy 設置

一分鐘即可以簡單的完成nginx-proxy + 自動更新 SSL的docker compose設定。

Edmond Y.
HKIT DOG
6 min readApr 9, 2023

--

Docker 一分鐘完成自動更新 SSL 証書的 nginx-proxy 設置

在幾年前的教學中也有教過使用 nginx-proxyletsencrypt-nginx-proxy-companion 來完成 docker-compose 的自動更新 SSL Cert 的設置。

但幾年來我都是 clone 一套來開設新 Server,最近發現原來整套設置的 image 都一段時間沒更新,所以就試一下 Traefik 來取代 Nginx。整個設定很簡單,更新 SSL 要做的設定比 letsencrypt-nginx-proxy-companion 還少,只是效能上 Nginx 還是很有優勢,下次篇再教 Traefik 的設置,今次說說新的 nginx-proxy package。

以前我使用的是 jwilder/nginx-proxyjrcs/letsencrypt-nginx-proxy-companion 來做 proxy server 及 自動更新 SSL Cert,現在他們都合成一個新的 docker user 來一齊發表新的 image,分別是 nginxproxy/nignx-proxynginxproxy/acme-companion ,廢話少說馬上來看看 docker compose 。

services:
# nginx-proxy
nginx-proxy:
image: nginxproxy/nginx-proxy:alpine # 新的nginx-proxy image
container_name: nginx-proxy
restart: always
ports:
- 80:80 # 即使不用http也要有port 80
- 443:443 # https
volumes:
- conf:/etc/nginx/conf.d
- vhost:/etc/nginx/vhost.d
- html:/usr/share/nginx/html
- dhparam:/etc/nginx/dhparam
- certs:/etc/nginx/certs:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
environment:
- ENABLE_IPV6=true # 支援IPv6
networks:
- nginx-proxy

# acme companion
acme-companion:
image: nginxproxy/acme-companion # 新的ssl companion image
container_name: acme-companion
volumes_from:
- nginx-proxy
volumes:
- certs:/etc/nginx/certs:rw
- acme:/etc/acme.sh
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
- DEFAULT_EMAIL=youremail.com # 換成你的 email
networks:
- nginx-proxy

# demo website
website:
image: nginx:alpine
container_name: website
restart: always
volumes:
- ./website:/usr/share/nginx/html:ro
environment:
- VIRTUAL_HOST=www.yourdomain.com,yourdomain.com # 換成你的domain
- LETSENCRYPT_HOST=www.yourdomain.com,yourdomain.com
networks:
- nginx-proxy

volumes:
conf: {}
vhost: {}
html: {}
dhparam: {}
certs: {}
acme: {}

networks:
nginx-proxy: {} # 新的nginx-proxy image container_name: nginx-proxy restart: always ports: - 80:80 # 即使不用http也要有port 80 - 443:443 # https volumes: - conf:/etc/nginx/conf.d - vhost:/etc/nginx/vhost.d - html:/usr/share/nginx/html - dhparam:/etc/nginx/dhparam - certs:/etc/nginx/certs:ro - /var/run/docker.sock:/tmp/docker.sock:ro environment: - ENABLE_IPV6=true # 支援IPv6 networks: - nginx-proxy # acme companion acme-companion: image: nginxproxy/acme-companion # 新的ssl companion image container_name: acme-companion volumes_from: - nginx-proxy volumes: - certs:/etc/nginx/certs:rw - acme:/etc/acme.sh - /var/run/docker.sock:/var/run/docker.sock:ro environment: - DEFAULT_EMAIL=youremail.com # 換成你的 email networks: - nginx-proxy # demo website website: image: nginx:alpine container_name: website restart: always volumes: - ./website:/usr/share/nginx/html:ro environment: - VIRTUAL_HOST=www.yourdomain.com,yourdomain.com # 換成你的domain - LETSENCRYPT_HOST=www.yourdomain.com,yourdomain.com networks: - nginx-proxy volumes: conf: {} vhost: {} html: {} dhparam: {} certs: {} acme: {} networks: nginx-proxy: {}

記得將 demo website 換成你自己的 container,由於有新的 default email 可以預先設定,所以只要設定 VIRTUAL_HOSTLETSENCRYPT 就好了。

設定完成後放置到安裝了docker 的 server 上,使用 docker compose up -d 即可起動及自動完成 let's encrypt 的 SSL 自動更新。

Originally published at https://blog.256pages.com on April 9, 2023.

--

--