开发环境: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文件,这都是没有清理缓存造成的。