1.准备好证书和秘钥
如果是云服务器,可以到对应的云服务器上购买ssl证书,我这里是百度云,我以百度云为例子
直达链接:https://console.bce.baidu.com/cas/#/cas/apply/create (这个是免费1年的,可以按需购买不同的证书)
购买完毕后就可以进行在 https://console.bce.baidu.com/cas/#/cas/purchased/common/list 中进行申请,申请成功后就可以用了
点击查看证书就可以看到下载页面,选择 PEM_nginx 进行下载
下载完毕后有2个文件:*.crt
结尾的是ssl证书,*.key
结尾的是秘钥
2.准备检查nginx是否有ssl模块
进入到nginx的sbin目录下 执行 ./nginx -V
如果出现 (configure arguments: --with-http_ssl_module
), 则已安装
没有的话请执行:./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
随后执行make
重新编译一下。此时再查看应该已经安装完毕
nginx的启动和停止命令
# 启动
./nginx -s stop
# 停止
./nginx -c /www/server/nginx/conf/nginx.conf
3.修改nginx配置文件
# 配置ssl
server {
listen 443 ssl;
server_name compass.cn; # localhost修改为您证书绑定的域名。
root html;
index index.html index.htm;
ssl_certificate /www/server/nginx/ssl/compass.cn.crt; #将domain.pem替换成您证书的文件名。
ssl_certificate_key /www/server/nginx/ssl/compass.cn.key; #将domain.key替换成您证书的密钥文件名。
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
location / {
proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
try_files $uri $uri/ /index.html;
root /web/front/dist; #前端项目dist的完整路径(这是一个前端项目)
index index.html;
}
location ^~ /accountBook/ { #account项目后端服务
proxy_pass http://compass.cn:8080;
}
location ^~ /account/ {
# 文件位置: /web/front/static/accountStaticaccount
root /web/front/static/accountStatic;
}
#account项目静态资源
location ^~ /back/ {
# 文件位置: /web/front/static/accountStaticaccount/back
root /web/front/static/backStatic;
}
}
# 在浏览器访问 https://compass.cn 就可以使用https访问到 /web/front/dist下面的前端项目,后端项目也是能访问到的