2 Star 4 Fork 3

zhangquan / learn-nginx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
https.md 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
xuexb 提交于 2017-06-16 09:45 . 更新文档

配置https

首先配置支持https必须让nginx开启http_ssl_module模块,点击查看nginx编译安装参数 ,可以使用nginx -V查看是否开启TLS SNI support enabled

购买/生成ssl证书,可以使用免费的证书,比如:Let's Encrypt,免费好用的 HTTPS 证书, 签发免费 SSL 泛域名证书

# 配置https

# 配置个http的站点,用来做重定向,当然如果你不需要把http->https可以把这个配置删了
server {
    listen       80;
    # 配置域名
    server_name www.xxoo.com xxoo.com;

    # 添加STS, 并让所有子域支持, 开启需慎重
    add_header strict-transport-security 'max-age=31536000; includeSubDomains; preload';

    # 配置让这些http的访问全部301重定向到https的
    rewrite ^(.*) https://www.xxoo.com$1 permanent;
}

# 配置https
server {
    # 配置域名
    server_name www.xxoo.com xxoo.com;

    # https默认端口
    listen 443;

    # 添加STS, 并让所有子域支持, 开启需慎重
    add_header strict-transport-security 'max-age=31536000; includeSubDomains; preload';

    # https配置
    ssl on;
    ssl_certificate /xxoo/www.xxoo.com.crt;
    ssl_certificate_key /xxoo/www.xxoo.com.key;

    # 其他按正常配置处理即可...
}

注意,这里证书的格式是.crt

配置后的访问规则

输入链接 最终访问链接
http://www.xxoo.com https://www.xxoo.com
http://www.xxoo.com/404/500 https://www.xxoo.com/404/500
http://xxoo.com https://www.xxoo.com
https://www.xxoo.com -(原链接不变)
https://xxoo.com/500 https://www.xxoo.com/500
1
https://gitee.com/zhangquan/learn-nginx.git
git@gitee.com:zhangquan/learn-nginx.git
zhangquan
learn-nginx
learn-nginx
master

搜索帮助