Nginx配置HTTPS访问(SLL证书配置)

一、购买证书

1.登录阿里云账号,进入控制台,找到SSL证书服务

2.点击免费证书

2.1.立即购买

选择数量20,每个账户每年有20个免费证书。同意协议后点击立即购买。

2.2.创建证书并且申请

 2.3.申请证书

2.4.认证并等待审核

2.5.下载证书  

 

 二丶部署服务器

1. 打开服务器进入nginx安装的conf目录

/usr/local/nginx/conf

2.下载的证书文件,完整的复制到config/cert下面。

将文件名改为域名

3. 更改config配置

找到该证书绑定的域名服务器

/usr/local/nginx/conf/vhost

配置如下,其中xxxxx.com根据具体的域名进行调整 

橙色为修改,红色为新加

server

    {

        listen 443 ssl;#SSL协议访问端口号为443。

        server_name xxxxx.com ;

        index index.html index.htm index.php default.html default.htm default.php;

        root  /home/wwwroot/xxxxx.com;#业务域名

        #ssl配置

        ssl_certificate       cert/xxxxx.com.pem;

        ssl_certificate_key   cert/xxxxx.com.key;

        ssl_session_timeout  5m;

        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

        ssl_prefer_server_ciphers  on;

        ssl_session_cache    shared:SSL:1m;

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory

        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

        include proxy-pass-php.conf;

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

        {  

            expires      30d;

        }

        location ~ .*\.(js|css)?$

        {

            expires      12h;

        }

        location ~ /.well-known {

            allow all;

        }

        location ~ /\.

        {

            deny all;

        }

#去掉index.php

     if ( !-e $request_filename){
        rewrite ^(.*)$ /index.php?s=/$1 last;
        break;
    }

        access_log  /home/wwwlogs/xxxxx.com.log;

    }

4.负载均衡,有则必须配置

5.重启lnmp

检查nginx配置

 /etc/init.d/nginx configtest

 重启nginx

 /etc/init.d/nginx reload

三、将http强行改写https

更改config配置

找到该证书绑定的域名服务器

/usr/local/nginx/conf/vhost

文件末端增加以下代码,并且xxxxx.com更替为你所使用的域名

server {
        listen       80;
      server_name xxxxx.com;
      rewrite ^(.*)$    https://$host$1    permanent;  # 强行将 http 转化 https 来访问。

       error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值