LNMP环境给ngnix配置https

开发环境:LNMP
服务器:阿里云服务器、域名

①首先在阿里云SSL证书处购买SSL证书(穷逼自行选择免费的证书DigiCert(Symantec))。
②购买后申请证书,填写相关资料后(如果是阿里云账户上的域名,则会自动生成域名解析直接认证即可)。
③认证后下载证书上传至服务器,放在一个可以找到绝对路径的地方。
④设置服务器安全组,打开443端口(https默认443端口)。
⑤下载ngnix配置文件,对配置文件进行编辑:
在这里插入图片描述
以下是新增的server内容:

server {
        listen       443 ssl;
        server_name  www.domain.com;
        root         /html;

        ssl_certificate "/cert/0000000_www.domain.com.pem";
        ssl_certificate_key "/cert/0000000_www.domain.com.key";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

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

        location ~ .php$ {
            root /html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            include fastcgi_params;
            fastcgi_param HTTPS on;
        }

        error_page 404 /404.html;
        location = /404.html {
        }

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

设置http自动跳转https:
在默认的ngnix 80端口处增加以下这条代码

rewrite ^(.*)$ https://$host$1 permanent;

位置如图:
在这里插入图片描述
⑥完成编辑后保存,上传
⑦重启ngnix服务

systemctl restart nginx 

常见问题:
1.配置完后访问http不会自动跳转https,或者访问时直接下载根目录的php文件,这都是没有清理缓存造成的。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值