这一篇记录一下Nginx的SSl配置,各位看到此博客的小伙伴,如有不对的地方请及时通过私信我或者评论此博客的方式指出,以免误人子弟。多谢!

我们知道https相对于http是非常安全的,nginx的ssl可以使nginx支持https,Nginx要想使用SSL,需要添加一个模块--with-http_ssl_module,我们先测试下,访问http://118.190.152.59/ 可以正常访问,但是换成https访问 https://118.190.152.59/ 就会访问失败,下面记录一下Nginx的SSL支持。

最新nginx ssl算法 nginx支持ssl_SSL

目录

添加SSL支持安装ssl_module

生成证书

安装openssl

生成证书

配置https

测试


添加SSL支持安装ssl_module

1、将原有/usr/local/nginx/sbin/nginx和conf中的nginx.conf进行备份。

2、进入nginx的安装源码目录,进行配置指定对应模块。

cd /usr/local/nginx-1.2.0
./configure --with-http_ssl_module

在进行配置前 先使用 nginx -V 命令查看原有nginx是否有已经配置的信息,有的话先复制出来,在执行./configure --with-http_ssl_module 前别忘了带上,比如之前你指安装了 --with-http_ssl_module模块,控制台会展示:

configure arguments: --with-http_ssl_module

此时需要将configure arguments后的参数复制带上,如下:

./configure --with-http_ssl_module --with-stream

3、通过make模板进行编译。

4、make之后会在源码目录中多出一个objs目录,将objs目录下的nginx移动到/usr/local/nginx/sbin下。

cp /objs/nginx /usr/local/nginx/sbin

5、在源码目录下执行 make upgrade。

make upgrade

6、检查是否安装成功,还是使用nginx -V命令,如果安装成功会看到 configure arguments: 后会有--with-http_ssl_module,如下:

最新nginx ssl算法 nginx支持ssl_SSL_02

生成证书

一般我们生成的证书可以通过阿里云购买,或者在阿里云申请一个免费的也可以,免费的有效期只有一年,而且都需要绑定域名,我暂时没有自己的域名,作为测试用,使用openssl自己生成证书就可以了。

安装openssl

在之前安装nginx的时候已经把openssl一起进行了安装,可以查看之前的博客 Linux/Windows中安装Nginx ,通过一下命令检查是否安装了openssl:

openssl version

 

最新nginx ssl算法 nginx支持ssl_Nginx_03

生成证书

依次输入以下命令,按照提示输入需要的密码省市等信息,最终生成的 .crt 文件和 .key 文件就是我们需要的。

mkdir /root/cert
cd /root/cert
openssl genrsa -des3 -out server.key 1024
openssl req -new -key server.key -out server.csr
cp server.key server.key.org
openssl rsa -in server.key.org -out server.key
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

配置https

将配置文件中默认的配置打开,将ssl_certificate 和 ssl_certificate_key 换成上面生成的server.crt 和 server.key的路径即可。

server {
        listen       443;
        server_name  localhost;

        ssl                  on;
        ssl_certificate      /root/cert/server.crt;
        ssl_certificate_key  /root/cert/server.key;

        ssl_session_timeout  5m;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;
	    location / {
	        root   html;
            index  index.html index.htm;
        }
    }

测试

重启nginx后,再次访问 https://118.190.152.59/ 发现就可以访问了,只是浏览器会提示不安全,正式用的时候绑定域名就可以啦,具体操作可以从阿里云上查。

最新nginx ssl算法 nginx支持ssl_最新nginx ssl算法_04