本文是从零搭建-Node-js-线上环境的后续,因 https 比较独立,故单独写了一篇文章记录下。
阿里云现在提供了免费的 SSL 证书,申请过程略过,很简单,审核通过后下载证书文件。
在 /etc/nginx 下新建目录 cert,将证书 xxx.pem 和 xxx.key 上传到 /etc/nginx/cert 下,修改 dev.example.com.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   | server {     listen 443;     server_name dev.example.com;     access_log /var/log/nginx/dev.example.com.access.log;     ssl on; #开启SSL     ssl_certificate     cert/xxx.pem;    #证书文件     ssl_certificate_key cert/xxx.key;    #私钥文件     location / {         proxy_pass   http://127.0.0.1:8888/;     } }
  server {     listen 80;        #监听80端口     server_name  dev.example.com; #监听的域名     return 301 https://dev.example.com$request_uri; }
  | 
重新访问 http://dev.example.com 看看是不是已经跳转到 https://dev.example.com 了