nginx引入ssl证书是将http升级为https访问
首先需要在nginx的根目录下的nginx.conf文件夹中配置ssl证书和key文件夹
例如
server {
listen 3000 ssl;
server_name localhost; //要配置证书的域名
ssl_certificate /web_cert/your_certificate.crt; //ssl证书在本地的文件地址
ssl_certificate_key /web_cert/your_private_key.key; //ssl证书在本地的文件地址
<!-- 如果是http请求那么就跳转到https页面 -->
if ($scheme = http) {
return 301 https://$host$request_uri;
}
location / {
root /home/root1/17/dist;
index index.html index.htm;
}
location /live {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/root1/17/video_collect/video_PullFlow;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
}
修改了nginx.conf后需要重启nginx
sudo nginx -t # 检查配置文件语法是否有错
sudo nginx # 启动nginx
sudo nginx -s reload # 重新加载
sudo nginx -s stop # 关闭nginx
配置完nginx后需要在服务中也配置ssl证书(保证服务也是在https上运行)
例如fastapi是在入口文件中配置
config = Config(app=app, loop=loop, host='0.0.0.0', port=443, log_level="info",ssl_keyfile=ssl_keyfile,ssl_certfile=ssl_certfile)
ser = Server(config=config)
这样就可以实现http升级为https访问了