之前的文章《浅析nginx的安装与使用(收藏)》中,给大家介绍了解了nginx的安装与使用。下面本篇文章给大家介绍怎么给站点开启https和http2,我们一起看看怎么做。
本篇文章主要介绍了如何给站点开启https和http2
本篇文章主要介绍了如何给站点开启https和http2
给站点开启https比较简单以本站Linux + nginx为例
3条命令即可
下载脚本
wget https://dl.eff.org/certbot-autochmod a+x certbot-auto
登录后复制
自动关联到nginx
$sudo ./certbot-auto --nginx```sh## 检查证书有没有过期
登录后复制
$sudo ./certbot-auto renew –dry-run
## 如果过期了就自动续订```sh$sudo ./certbot-auto renew
登录后复制
假如提示如下错误代码
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc3 in position 5: ordinal not in range(128)是因为nginx的配置文件存在中文字符,把中文改成英文即可
或者可以自行查找
grep -r -P '[^-]' /etc/nginx /etc/letsencrypt
登录后复制
好了,现在打开nginx配置看下
listen 443 ssl http2;ssl on;ssl_certificate /etc/letsencrypt/live/chuchur.com/fullchain.pem;ssl_certificate_key /etc/letsencrypt/live/chuchur.com/privkey.pem;include /etc/letsencrypt/options-ssl-nginx.conf;ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;if ($scheme != "https") { return 301 https://$host$request_uri;} # managed by Certbot
登录后复制
注意 nginx 版本太低是无法开启的,首先要升级 nginx 版本,以 centos 为例
修改/etc/yum.repos.d/nginx.repo的nginx仓库地址
[nginx]name=nginx repobaseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/gpgcheck=0enabled=1
登录后复制
如果还没有安装 nginx,直接执行 yum install nginx,如果已经安装了,则可以通过 yum clean all && yum update nginx 升级。
建议先停掉nginx暴力停止killall nginx, 然后卸载掉yum remove nginx之后再重新安装yum install nginx
Ubuntu / Debian升级HTTP/2
首先修改apt仓库源/etc/apt/source.list的地址,增加下面两行仓库。
deb http://nginx.org/packages/mainline/debian/ codename nginxdeb-src http://nginx.org/packages/mainline/debian/ codename nginx
登录后复制
接下来执行
apt-get clean && apt-get install nginx
登录后复制
好了,现在成功了开启了https了,443 ssl 后面那个http2,就是开启了http2
最后重启nginx
nginx -s reload
登录后复制
刷新页面发现网址前面加了一把锁就表示https开启成功了
如何验证开启了http2呢 你打开要验证的网址等加载完成后,新开一个tab输入chrome://net-internals/#http2回车
如果在列表中找到你的网址对应的ip就表面已经开启成功了。
当然是用谷歌浏览器
【完】
推荐学习:http视频教程
以上就是手把手教你使用给站点开启https和http2(附代码)的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2907412.html