pplgin / blogs

分享资料整理

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Nginx + Certbot 搭建免费https站点

pplgin opened this issue · comments

如何配置 http -> https(免费证书)

下载安装 Certbot

具体步骤 https://certbot.eff.org/lets-encrypt/ubuntutrusty-nginx.html

配置nginx

先配置一个nginx(你得有服务器得有域名吧~) 注意替换(yourdomain.com)为自己的域名

vi  /etc/nginx/conf.d/yourdomain.com.conf

张贴以下内容:

server {
  listen 80;
  server_name (www.)?yourdomain.com;

  location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root     /usr/share/nginx/html;
  }

  location = /.well-known/acme-challenge/ {
    return 404;
  }
}

执行下 reload

nginx -s reload

证书生成

使用certbot 命令生成证书

certbot certonly --webroot -w /usr/share/nginx/html/ -d yourdomain
  • -w 表示配置webroot路径 在上面已经配置为了 (/usr/share/nginx/html)
  • -d 表示匹配的域名地址

certbot 生成证书的机制 会先通过http 发起请求像对应的域名 yourdomain/.well-known/acme-challenge/(这个也是为什么要配置nginx的原因)

修改nginx配置http强跳

http:80-> https:443
server {
  ...
  return 301 https://$host$request_uri;
  ...
}

添加https配置 (ssl_certificate 证书地址在上一步生成的时候会在出现)

server {                                                                                                                                                                            
    listen       443 ssl http2;                                                                                                                                                     
    listen       [::]:443 ssl http2;                                                                                                                                                
    server_name  (www.)?yourdomain;                                                                                                                                                 

    ssl_certificate /etc/letsencrypt/live/yourdomain/fullchain.pem;                                                                                                                 
    ssl_certificate_key /etc/letsencrypt/live/yourdomain/privkey.pem;                                                                                                               
    ssl_trusted_certificate /etc/letsencrypt/live/yourdomain/chain.pem;                                                                                                             
    # Load configuration files for the default server block.                                                                                                                        
    location /  {                                                                                                                                                                   
        proxy_pass http://localhost:port;                                                                                                                                                     
    }                                                                                                                                                                               
}

最后定期更新下证书,写一个定时脚本(毕竟免费的是会过期的)

/usr/bin/certbot renew  >> /var/log/le-renew.log