腾讯云免费SSL证书配置

基于ngnix的https配置

1.证书上传

1)申请证书请参照官方文档,而后把已经颁发下来的证书下载下来。php

2)解压文件,而后把ngnix文件夹下的1_xxx.com_bundle.crt和2_xxx.com.key上传到服务器的nginx配置文件目录(上传到同一目录),如:/usr/local/nginx/conf,我上传的目录是/usr/local/nginx/conf/ssl/www。个人服务器上的nginx安装在/usr/local/nginx目录。html

2.证书安装

修改/usr/local/nginx/conf目录下nginx.conf配置文件。nginx

server {
      listen         443 ssl; #监听443端口
      server_name    xxx.com;#域名,换成本身域名
      ssl_certificate      ssl/www/1_xxx.com_bundle.crt;#证书路径,换成本身证书名
      ssl_certificate_key  ssl/www/2_xxx.com.key;#key路径,换成本身key
                      
      ssl_session_timeout  5m; #会话过时时间                                               

      ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #为创建安全链接,服务器所容许的密码格式列表                                          
      ssl_prefer_server_ciphers  on; #依赖SSLv3和TLSv1协议的服务器密码将优先于客户端密码

      location /     {
              root     /usr/share/nginx/html; #域名访问的根目录,换成本身根目录
              index    index.html index.htm index.php;
      }

      location ~ \.php$ {
          root           /usr/share/nginx/html; #域名访问的根目录,换成本身根目录
          fastcgi_pass   127.0.0.1:9000;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
          include        fastcgi_params;
      }
 }

配置完成后,先用nginx下的./sbin/nginx –t来测试下配置是否有误,正确无误的话,重启nginx(./sbin/nginx –s reload)。就能够使 https://xxx.com (本身域名)来访问了。 注释:浏览器

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

3 使用全站加密,http自动跳转https

对于用户不知道网站能够进行https访问的状况下,让服务器自动把http的请求重定向到https。 在nginx.conf配置文件的http的监听80端口的server中,找到安全

location / {
            #root   html;
            rewrite ^(.*) https://$host$1 permanent; #重定向到https
            index  index.php index.html index.htm;
        }

配置,在里面增长重定向配置 rewrite ^(.*) https://$host$1 permanent; 配置完成后,先用nginx下的./sbin/nginx –t来测试下配置是否有误,正确无误的话,重启nginx(./sbin/nginx –s reload)。就能够使 http://xxx.com (本身域名)来访问了,看看是否在浏览器中自动变成https连接。服务器

4.遇到的问题

server {
        listen 80;
        server_name liuhangs.com;
        #return 301 https://$server_name$request_uri;
        rewrite ^(.*)$ https://$server_name$1 permanent;
}
相关文章
相关标签/搜索