参考地址
https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E
https://github.com/acmesh-official/acme.sh/wiki/deploy-to-docker-containers
创建所需文件夹及文件
mkdir -p /data/nginx && mkdir -p /data/nginx/conf.d && touch /data/nginx/nginx.conf
编辑 nginx.conf 文件
user nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events {worker_connections 1024;}http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf;}
编辑域名文件
vim /data/nginx/conf.d/test1.example.com.conf
server {listen 80;#listen 443 ssl;server_name test1.example.com;charset utf-8;access_log /var/log/nginx/test1.example.com.access.log main;root /usr/share/nginx/html/test1.example.com;index index.html index.htm;# 此处ssl 配置需要在申请证书文件后打开#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#ssl_prefer_server_ciphers on;#ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";# config ssl certificate#ssl_certificate /etc/nginx/ssl/test1.example.com/fullchain.cer;#ssl_certificate_key /etc/nginx/ssl/test1.example.com/test1.example.com.key;#申请证书使用location ^~/.well-known/acme-challenge {allow all;}# 这里配置使用HTTP访问的请求location / {proxy_set_header Host $http_addr;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://127.0.0.1:8080;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_log /var/log/nginx/test1.example.com.error.log;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}
vim /data/nginx/conf.d/test2.example.com.conf
server {listen 80;#listen 443 ssl;server_name test2.example.com;charset utf-8;access_log /var/log/nginx/test2.example.com.access.log main;root /usr/share/nginx/html/test2.example.com/;index index.html index.htm;# 此处ssl 配置需要在申请证书文件后打开#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;#ssl_prefer_server_ciphers on;#ssl_ciphers "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH EDH+aRSA !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !RC4";# config ssl certificate#ssl_certificate /etc/nginx/ssl/test2.example.com/fullchain.cer;#ssl_certificate_key /etc/nginx/ssl/test2.example.com/test2.example.com.key;#申请证书使用location ~ /.well-known/acme-challenge {allow all;# root /usr/share/nginx/html;}# 这里配置使用HTTP访问的请求location / {root /usr/share/nginx/html/test2.example.com/;index index.html index.htm;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_log /var/log/nginx/test2.example.com.error.log;error_page 500 502 503 504 /50x.html;location = /50x.html {root /usr/share/nginx/html;}}
启动nginx,准备生成ssl证书
docker run -d --name nginx --network host --restart always --privileged=true -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/ssl:/etc/nginx/ssl -v /data/nginx/logs:/var/log/nginx -v /data/nginx/conf.d:/etc/nginx/conf.d nginx
使用acme.sh生成证书文件
运行acme容器服务
docker run --rm -itd --privileged=true -v /data/nginx/ssl:/acme.sh -v /data/nginx/html:/html --net=host --name=acme.sh neilpang/acme.sh daemon
docker exec acme.sh --issue -d test1.example.com --webroot /html/test1.example.com/docker exec acme.sh --issue -d test2.example.com --webroot /html/test2.example.com/
成功生成文件后,将 test1.example.com.conf 和 test2.example.com.conf 的ssl配置打开验证。
自动续期(未试验)
acme申请的证90天后会过期,需要定时去更新,acme默认60天更新
删除上面的容器,新建 /data/nginx/nginx_proxy.yml文件
version: '3.4'services:web:image: nginxcontainer_name: nginxports:- "80:80"- "443:443"privileged: truevolumes:- "/data/nginx/html:/usr/share/nginx/html:z"- "/data/nginx/nginx.conf:/etc/nginx/nginx.conf:z"- "/data/nginx/ssl:/etc/nginx/ssl:z"- "/data/nginx/logs:/var/log/nginx:z"- "/data/nginx/conf.d:/etc/nginx/conf.d:z"environment:- ENV=productionacme.sh:image: neilpang/acme.shcontainer_name: acme.shcommand: daemonprivileged: truevolumes:- "/data/nginx/ssl:/acme.sh:z"- /var/run/docker.sock:/var/run/docker.sock
