nginx配置ssl证书,实现https安全访问
此文只介绍nginx对ssl的配置说明,关于如何获取ssl证书这里不过多说明,大家自行百度。
在你已经获取到ssl证书的前提下,执行以下步骤来完成ssl的证书配置。
1、将解压后的证书中的.crt和.key两个文件上传到nginx的安装目录下的sslkey目录下。(sslkey目录自行创建,放在其他目录也是可以的)
2、在nginx配置目录conf/vhost/下新建 域名.conf配置文件,其中的域名改成你要通过https来访问的网站的域名,如我要让kaky.cn通过https来访问,那你就新建kaky.cn文件
3、在kaky.cn.conf文件中进行如下配置:
server {
listen 80;
server_name kaky.cn www.kaky.cn;
return 301 https://www.kaky.cn$request_uri; (说明:将http访问通过301跳转到https下)
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/kaky.cn; (说明:这里是你网站的根目录)
}
server {
listen 443 ssl;
server_name kaky.cn;
return 301 https://www.kaky.cn$request_uri; (说明:将不带www的https访问即https://kaky.cn 也跳转到https://www.kaky.cn下)
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/kaky.cn;
ssl_certificate /usr/local/nginx/sslkey/kaky.cn-ca-bundle.crt; (说明:.crt文件存放路径,根据你的实际情况修改)
ssl_certificate_key /usr/local/nginx/sslkey/kaky.cn.key; (说明:.key文件存放路径,根据你的实际情况修改)
}
server {
listen 443 ssl;
server_name www.kaky.cn;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/kaky.cn;
ssl_certificate /usr/local/nginx/sslkey/kaky.cn-ca-bundle.crt;
ssl_certificate_key /usr/local/nginx/sslkey/kaky.cn.key;
include none.conf;
#error_page 404 /404.html;
include enable-php.conf; (说明:包含php文件配置,运行php访问)
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 12h;
}
location ~ /\. {
deny all;
}
access_log off;
}
4、保存文件后,重启nginx服务即可通过https来访问你的网站了