個人網站配置HTTPS
阿新 • • 發佈:2019-01-31
讓NGINX支援SSL
需要編譯時支援ssl,可以sbin/nginx -V 來檢視confiture引數,如果當時沒有支援,那麼需要重新編譯安裝。 編譯引數前面已經有一篇文章了。nginx編譯引數,也不用全加,用--with-http_ssl_module
就可以了。
生成證書
主要參考這個letsencrypty,可以生成免費證書。 生成方式也很簡單,讀上面的文章基本就能明白。ubuntu+nginx.
大致步驟如下:
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto
./path/to /certbot-auto certonly --webroot -w /var/www/example -d example.com -d www.example.com -w /var/www/thing -d thing.is -d m.thing.is
執行完之後系統中會生成這些檔案。
This command will obtain a single cert for example.com, www.example.com, thing.is, and m.thing.is; it will place files below /var/www/example to prove control of the first two domains, and under /var/www/thing for the second pair.
Automating renewal
上面生成的證書,有效期好像是1個月,所以需要到期自己重新renewal一下。方法如下:
./path/to/certbot-auto renew --dry-run
./path/to/certbot-auto renew --quiet --no-self-upgrade
配置NGINX
配置就不多說了.首先需要配置2個server,監聽2個埠。這樣可以強制將80埠的請求重定向至443埠。https本身監聽的是443埠。最主要的是ssl中間那3行。將步驟2中生成的對應key寫在nginx的配置檔案裡。注意改成你的具體路徑。
server {
listen 443 ;
server_name blog.nofile.cc;
ssl on;
ssl_certificate /xxxx/letsencrypt/live/yoursite/fullchain.pem;
ssl_certificate_key /xxxx/letsencrypt/live/yoursite/privkey.pem;
location / {
#這個地方指定被訪問的資料夾位置
root /your/webroot/;
index index.html;
}
}
server {
listen 80;
server_name blog.nofile.cc;
return 301 https://blog.nofile.cc$request_uri;
}
配置好之後,重啟nginx,應該就可以看到綠色的鎖了。
我的個人網站https://blog.nofile.cc,歡迎來踩。