acme证书申请

发布时间 2023-11-17 11:12:46作者: kuozhen

安装条件:
curl https://get.acme.sh | sh
alias acme.sh=~/.acme.sh/acme.sh
准备:
(1)将域名的解析指向服务器,并且服务器开启80端口的http服务

(2)准备acme需要目录

./nginx/html/.well-known/acme-challenge
1 指定初始化
方式一:通过指定dns

acme.sh --issue --server letsencrypt --dns dns_dp -d huisiban.com www.huisiban.com --webroot /usr/local/nginx/html


方式二:通过自定webroot,(推荐)

acme.sh --issue --server letsencrypt -d huisiban.com -d www.huisiban.com --webroot /usr/local/nginx/html


2 安装
执行下面的命令会生成证书文件相关的key和pem,改为你自己的路径和文件名

acme.sh --install-cert -d huisiban.com -d www.huisiban.com --key-file /usr/local/nginx/certs/www.huisiban.com.key --fullchain-file /usr/local/nginx/certs/www.huisiban.com.pem


3 自动更新
这条命令acme将会自动检查更新证书是否过期,自动续期

acme.sh --upgrade --auto-upgrade

 


4 证书申请完成后,就可以配置https 443 服务了

以 www.huisiban.com 为例,配置如下:

server {
    listen 80;
    server_name huisiban.com www.huisiban.com;
    rewrite ^(.*)$ https://www.huisiban.com$1 permanent;
}
server {
    listen       443 ssl;
    server_name  huisiban.com www.huisiban.com;
 
    ssl_certificate      /usr/local/nginx/certs/www.huisiban.com.pem;
    ssl_certificate_key  /usr/local/nginx/certs/www.huisiban.com.key;
 
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
 
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
 
 
    client_max_body_size 20m;
    client_body_buffer_size 128k;
 
    gzip on;
    gzip_buffers 32 4K;
    gzip_comp_level 6;
    gzip_min_length 100;
    gzip_types application/javascript text/css text/xml application/font-woff;
    gzip_disable "MSIE [1-6]\.";
 
    if ( $host = 'huisiban.com') {
        rewrite ^/(.*)$ https://www.huisiban.com/$1 permanent;
    }
    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root /usr/local/nginx/html;
    }
    location ~ ^/upload/(.*)\.(png|jpg|jpeg|gif)$ {
        root /opt/img;
        expires 5d;
        set $img_width -;
        set $img_height -;
        # 获取参数size的值
        if ($arg_size ~* "^(\d+)x(\d+)$") {
          set $img_width $1;
          set $img_height $2;
        }
        # 裁剪图片并且调整大小
        image_filter resize $img_width $img_height;
 
        image_filter_jpeg_quality 25;
        image_filter_buffer 10M;
    }
    location ~ ^/upload/(.*)\.(ico|webp)$ {
        root /opt/img;
    }
    location /fastcms.html {
        alias   /usr/local/nginx/html;
        # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
        try_files $uri $uri/ @router;
        # 请求指向的首页
        index  index.html index.htm;
    }
    location /fastcms {
            alias   /usr/local/nginx/html;
    }
    location /fastcms/api {
        proxy_pass http://127.0.0.1:8085;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    location / {
        proxy_pass http://127.0.0.1:8085;
        proxy_set_header Host      $host;
        proxy_set_header X-Real-IP $remote_addr;
 
    }
 
    # 由于路由的资源不一定是真实的路径,无法找到具体文件
    # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
    location @router {
      rewrite ^.*$ /index.html last;
    }
 
}