nginx下配置ssl证书,https配置入门

要配置ssl证书,首先要买一下ssl证书,能够在多个平台上购买,阿里云,腾讯云等,还有一些专门卖ssl证书的网站,今天在这里给你们介绍的是一个免费证书的网站。php

1.首先得到CA证书,打开网址:https://freessl.org/

FreeSSL.org 是一个提供免费HTTPS证书申请的网站,而后在首页的输入框内输入你的网站的一级域名而后回车,如今默认是 亚洲诚信的ssl证书无偿使用1年,而后输入邮箱,以后须要域名作一下TXT解析(这个是为了校验一下你是否是域名拥有者)css

而后就生成了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 -thtml

若是返回successful 就说明配置正确,此时就能够重启nginx了

service nginx restartnginx

完成重启以后看看https是否正常运行,若是没有效果,那你就要检测你443端口是否放行,使用阿里云或者腾讯云等等服务器的话,能够在登陆上去以后安全组配置里面设置一下443端口放行,以后就大功告成了!

相关文章
相关标签/搜索