最近準備搞一個小程序玩玩,到配置服務器域名的時候發(fā)現只支持https協(xié)議,迫于無奈,我不得不再次跳出自己的思維舒適區(qū),發(fā)起了一次http向https的沖擊
在這方面我也了解的不深,寫點文字,給自己做筆記看
一、獲取證書
證書有很好幾種類型,有免費的也有付費的,具體可以參考阮一峰老師的HTTPS 升級指南。于我而言(還不是因為窮~~)當然是選擇最低級的域名認證以及免費的Let's Encrypt
既然如此,那就直奔Let‘s Encrypt這個環(huán)節(jié),我的服務器是Ubuntu,所以又針對性的通過谷歌找到了在Ubuntu上獲取Let’s Encrypt免費證書(感謝作者qakcn)。
首先進行Cerbot的安裝(我的系統(tǒng)版本好像只支持這種方式):
// 先進入你的安裝目錄(我的就是 etc)
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto
證書簽發(fā),可以通過如下命令
// 進入certbot的安裝目錄(我的就是/etc)
./cerbot-auto certonly --webroot -w /var/www -d example.com -d www.example.com -w /var/www/sub -d sub.example.com
關于多個域名生成在同一個證書下的過程有點曲折。我的服務器上部署了多個綁定不同端口的服務,通過nginx反向代理綁定到了不同的二級域名下,起初我將所有一級二級域名都生成在了一個證書當中,然后不管https://www.facemagic888.com 還是 https://h5.facemagic888.com都解析到了https://www.facemagic888.com/index.html這個頁面中
一開始懷疑是,多個域名是不是需要單獨生成,于是重新執(zhí)行命令,給二級域名h5.facemagic888.com單獨生成了一個證書,重新配置之后,情況還是一樣
然后再次懷疑,難道是let's encrypt不支持二級域名么,可是查了半天也沒看到這種說法啊,這恐怕缺乏依據,并且如果真不支持,那還是非常令人沮喪的
我不會告訴你,后面才發(fā)現是nginx配置的問題
補充(2017-09-15):
要增加一個子域名證書,首先要能保證這個域名的可以通過 http://sub.com/.well-known/acme-chanllenge/xxxxxxx能夠訪問到,如果是一個 express,也就是要保證 static 目錄下有 .well-known/acme-chanllenge 目錄
二、nginx配置
起初對照著參考文章來進行配置:
# node-ssl配置
server {
listen 443 ssl;
server_name www.facemagic888.com h5.facemagic888.com;
root /home/WWW/static;
index index.html;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.facemagic888.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.facemagic888.com/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
}
這樣配置之后,沒錯就是上面懷疑的開始?。。?br> 后面發(fā)現,雖然動態(tài)的接口不行,但是靜態(tài)頁面卻無法訪問,這就奇了怪了,那肯定不是不支持二級域名。 那就是代理配置得有問題洛,跟朋友在群里聊了聊發(fā)散了下,80端口既然配置了代理,443端口是不是應該也需要呢?來試一試:
# node-ssl配置
server {
listen 443 ssl;
server_name h5.facemagic888.com;
root /home/WWW/h5-creator;
index index.html;
ssl on;
ssl_certificate /etc/letsencrypt/live/www.facemagic888.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.facemagic888.com/privkey.pem;
location / {
## If you use HTTPS make sure you disable gzip compression
## to be safe against BREACH attack.
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_redirect off ;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
client_max_body_size 20m;
client_body_buffer_size 256k;
proxy_connect_timeout 30;
proxy_send_timeout 30;
proxy_read_timeout 60;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
proxy_http_version 1.1;
#proxy_pass http://NODE_SERVER;
proxy_pass http://localhost:3000;
}
location = /404.html {
root /usr/share/nginx/html;
}
}
還別說,這樣還真就行了,果真還是對nginx還不夠了解啊!!!
補充(2017-09-15):
https 不支持 websocket,這個不要配置 http 的重定向,支持兩種類型 http 和 https
要支持 websocket,需要在 nginx 的 http 配置中的 location / 增加
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
三、補充記憶
過來一段時間沒折騰了,忘記如何重啟nginx,我能說網上給的答案很多都太不直接了么,還是最欣賞這個辦法
service nginx restart
檢查nginx配置
nginx -t
四、證書過期重新簽發(fā)
重新獲取證書
// 我的 cerbot 安裝目錄
cd /etc
// 重新簽發(fā)證書
./certbot-auto renew
// 重啟 nginx
service nginx restart