美文网首页
如何为个人网站开启HTTPS访问(支持多域名)

如何为个人网站开启HTTPS访问(支持多域名)

作者: 老Q在折腾 | 来源:发表于2018-11-26 11:45 被阅读0次

    我们都知道,现在已经进入了HTTPS时代,连搜索引擎都会专门为HTTPS网站的索引进行加权。HTTPS的主要特点是更安全,虽然有可能会让我们的访问变得稍慢一些,但是保证安全以及与时俱进听起来很有吸引力。

    下面我们就以从腾讯云申请的免费SSL证书为例,演示下如何通过nginx为我们的网站开启HTTPS访问。


    欢迎大家关注我的个人博客【数洞】 【备用站】

    一、获取证书

    BAT等大型云服务提供商都有免费的SSL证书申请通道,鉴于我的域名、服务器都是从腾讯云购买,因此我们以腾讯云为例:【腾讯云SSL证书免费申请】

    按要求填写信息,基本上二十分钟内会完成审核。通过审核后,将我们的证书下载下来,然后从Nginx文件夹内获得SSL证书文件1_www.domain.com_bundle.crt和私钥文件2_www.domain.com.key

    二、证书安装

    将域名www.domain.com的证书文件1_www.domain.com_bundle.crt、私钥文件2_www.domain.com.key保存到nginx目录下,例如我的nginx的目录在/etc/nginx目录下。
    更新Nginx根目录下/etc/nginx/nginx.conf文件如下:

     server {
            listen 443;
            server_name www.domain.com; #填写绑定证书的域名
            ssl on;
            ssl_certificate 1_www.domain.com_bundle.crt;
            ssl_certificate_key 2_www.domain.com.key;
            ssl_session_timeout 5m;
            ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
            ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照这个套件配置
            ssl_prefer_server_ciphers on;
            location / {
                root   html; #站点目录
                index  index.html index.htm;
            }
        }
    

    配置完成后,先用nginx –t来测试下配置是否有误,正确无误的话,重启nginx(nginx -s reload)。就可以使用https://www.domain.com来访问。

    注:

    配置文件参数 说明
    listen 443 SSL 访问端口号为 443
    ssl on 启用 SSL 功能
    ssl_certificate 证书文件
    ssl_certificate_key 私钥文件
    ssl_protocols 使用的协议
    ssl_ciphers 配置加密套件,写法遵循 openssl 标准

    三、使用全站加密,HTTP自动跳转HTTPS(可选)

    对于用户不知道网站可以进行HTTPS访问的情况下,让服务器自动把HTTP的请求重定向到HTTPS。
    在服务器这边的话配置的话,可以在页面里加js脚本,也可以在后端程序里写重定向,当然也可以在WEB服务器来实现跳转。Nginx是支持rewrite的(只要在编译的时候没有去掉pcre),在HTTP的server配置里增加rewrite ^(.*) https://$host$1 permanent;,这样就可以实现从端口80进来的请求,重定向为HTTPS了。

    四、nginx.conf文件示例

    由于我的个人博客有两个域名,所以有四个server配置在里边,具体如下:

    server {
        listen       443;
        server_name  www.data-insights.cn;
        ssl on;
        ssl_certificate 1_www.data-insights.cn_bundle.crt;
        ssl_certificate_key 2_www.data-insights.cn.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        root         /data/www/hexo;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    
    server {
        listen       443;
        server_name  www.data-insight.cn;
        ssl on;
        ssl_certificate 1_www.data-insight.cn_bundle.crt;
        ssl_certificate_key 2_www.data-insight.cn.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers on;
        root         /data/www/hexo;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    
    server {
        listen       80;
        server_name  www.data-insights.cn;
        root         /data/www/hexo;
        rewrite ^(.*) https://$host$1 permanent;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    
    server {
        listen       80;
        server_name  www.data-insight.cn;
        root         /data/www/hexo;
        rewrite ^(.*) https://$host$1 permanent;
    
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    
        location / {
        }
    
        error_page 404 /404.html;
            location = /40x.html {
        }
    
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    

    相关文章

      网友评论

          本文标题:如何为个人网站开启HTTPS访问(支持多域名)

          本文链接:https://www.haomeiwen.com/subject/kqzvqqtx.html