参考地址

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

创建所需文件夹及文件

  1. mkdir -p /data/nginx && mkdir -p /data/nginx/conf.d && touch /data/nginx/nginx.conf

编辑 nginx.conf 文件

  1. user nginx;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. #gzip on;
  19. include /etc/nginx/conf.d/*.conf;
  20. }

编辑域名文件

vim /data/nginx/conf.d/test1.example.com.conf

  1. server {
  2. listen 80;
  3. #listen 443 ssl;
  4. server_name test1.example.com;
  5. charset utf-8;
  6. access_log /var/log/nginx/test1.example.com.access.log main;
  7. root /usr/share/nginx/html/test1.example.com;
  8. index index.html index.htm;
  9. # 此处ssl 配置需要在申请证书文件后打开
  10. #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  11. #ssl_prefer_server_ciphers on;
  12. #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";
  13. # config ssl certificate
  14. #ssl_certificate /etc/nginx/ssl/test1.example.com/fullchain.cer;
  15. #ssl_certificate_key /etc/nginx/ssl/test1.example.com/test1.example.com.key;
  16. #申请证书使用
  17. location ^~/.well-known/acme-challenge {
  18. allow all;
  19. }
  20. # 这里配置使用HTTP访问的请求
  21. location / {
  22. proxy_set_header Host $http_addr;
  23. proxy_set_header X-Real-IP $remote_addr;
  24. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  25. proxy_pass http://127.0.0.1:8080;
  26. }
  27. #error_page 404 /404.html;
  28. # redirect server error pages to the static page /50x.html
  29. #
  30. error_log /var/log/nginx/test1.example.com.error.log;
  31. error_page 500 502 503 504 /50x.html;
  32. location = /50x.html {
  33. root /usr/share/nginx/html;
  34. }
  35. }

vim /data/nginx/conf.d/test2.example.com.conf

  1. server {
  2. listen 80;
  3. #listen 443 ssl;
  4. server_name test2.example.com;
  5. charset utf-8;
  6. access_log /var/log/nginx/test2.example.com.access.log main;
  7. root /usr/share/nginx/html/test2.example.com/;
  8. index index.html index.htm;
  9. # 此处ssl 配置需要在申请证书文件后打开
  10. #ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  11. #ssl_prefer_server_ciphers on;
  12. #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";
  13. # config ssl certificate
  14. #ssl_certificate /etc/nginx/ssl/test2.example.com/fullchain.cer;
  15. #ssl_certificate_key /etc/nginx/ssl/test2.example.com/test2.example.com.key;
  16. #申请证书使用
  17. location ~ /.well-known/acme-challenge {
  18. allow all;
  19. # root /usr/share/nginx/html;
  20. }
  21. # 这里配置使用HTTP访问的请求
  22. location / {
  23. root /usr/share/nginx/html/test2.example.com/;
  24. index index.html index.htm;
  25. }
  26. #error_page 404 /404.html;
  27. # redirect server error pages to the static page /50x.html
  28. #
  29. error_log /var/log/nginx/test2.example.com.error.log;
  30. error_page 500 502 503 504 /50x.html;
  31. location = /50x.html {
  32. root /usr/share/nginx/html;
  33. }
  34. }

启动nginx,准备生成ssl证书

  1. 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容器服务

  1. 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
  1. docker exec acme.sh --issue -d test1.example.com --webroot /html/test1.example.com/
  2. 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文件

  1. version: '3.4'
  2. services:
  3. web:
  4. image: nginx
  5. container_name: nginx
  6. ports:
  7. - "80:80"
  8. - "443:443"
  9. privileged: true
  10. volumes:
  11. - "/data/nginx/html:/usr/share/nginx/html:z"
  12. - "/data/nginx/nginx.conf:/etc/nginx/nginx.conf:z"
  13. - "/data/nginx/ssl:/etc/nginx/ssl:z"
  14. - "/data/nginx/logs:/var/log/nginx:z"
  15. - "/data/nginx/conf.d:/etc/nginx/conf.d:z"
  16. environment:
  17. - ENV=production
  18. acme.sh:
  19. image: neilpang/acme.sh
  20. container_name: acme.sh
  21. command: daemon
  22. privileged: true
  23. volumes:
  24. - "/data/nginx/ssl:/acme.sh:z"
  25. - /var/run/docker.sock:/var/run/docker.sock