要配置ssl证书,首先要买一下ssl证书,可以在多个平台上购买,阿里云,腾讯云等,还有一些专门卖ssl证书的网站,今天在这里给大家介绍的是一个免费证书的网站。
1.首先获得CA证书,打开网址:https://freessl.org/
FreeSSL.org 是一个提供免费HTTPS证书申请的网站,然后在首页的输入框内输入你的网站的一级域名然后回车,现在默认是 亚洲诚信的ssl证书免费使用1年,然后输入邮箱,之后需要域名做一下TXT解析(这个是为了校验一下你是不是域名拥有者)
然后就生成了ssl证书的两个key,如下
到这一步为止要准备的东西已经准备ok了,这里下面有个下载按钮可以把文件下载下来
接下来就是配置CA证书到nginx了
先把刚才的证书传到服务器上,你可以放在nginx目录或者你自己找一个容易管理的目录,不要随意放,以防不好维护,创建一个目录cert把里面的两个文件都传上去,然后
打开nginx的虚拟主机所在目录 在nginx/conf/vhost/
打开你的网站的配置文件 如 baidu.com.conf,这是未修改之前的conf,rewrite内容和你使用的php框架有关进行不同的rewrite
server {
listen 80;
server_name baidu.com www.baidu.com;
index index.html index.htm index.php;
root /alidata/www/wewe/public;
location / {
index index.php index.html index.htm;
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
rewrite ^(.+)$ /index.php last;
}
#引用PHP CGI
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
}
复制代码
接下来看一下我们需要加什么东西进来,
listen 443 ssl;#这个443端口和80端口可以同时监听,同时监听就是http和https都可用,只监听443就是强制https
server_name wewe.weinvestment.cn;
index index.html index.htm index.php;
root /alidata/www/wewe/public;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
ssl_certificate /alidata/server/cert2/full_chain.pem;#这个地方就是刚才证书上传的路径
ssl_certificate_key /alidata/server/cert2/private.key;#这个地方就是刚才证书上传的路径
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;
#SSL-END
复制代码
我这里再贴一个我开启https和http同时监听后的整体conf文件
server {
listen 80;
listen 443 ssl;
server_name baidu.com www.baidu.com;
index index.html index.htm index.php;
root /alidata/www/wewe/public;
#SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
#error_page 404/404.html;
ssl_certificate /alidata/server/cert2/full_chain.pem;
ssl_certificate_key /alidata/server/cert2/private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
error_page 497 https://$host$request_uri;
#SSL-END
location / {
index index.php index.html index.htm;
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
rewrite ^(.+)$ /index.php last;
}
#引用PHP CGI
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
}
复制代码
配置完成之后,不要急于重启nginx,可以先检测一下配置是否有误,
nginx -t
如果返回successful 就说明配置正确,此时就可以重启nginx了
service nginx restart