nginx配置https访问

nginx配置https访问

一、https简介

HTTPS其实是有两部分组成:HTTP + SSL/TLS,也就是在HTTP上又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过TLS进行加密,所以传输的数据都是加密后的数据

二、https协议原理

首先,客户端与服务器建立连接,各自生成私钥和公钥,是不同的。服务器返给客户端一个公钥,然后客户端拿着这个公钥把要搜索的东西加密,称之为密文,并连并自己的公钥一起返回给服务器,服务器拿着自己的私钥解密密文,然后把响应到的数据用客户端的公钥加密,返回给客户端,客户端拿着自己的私钥解密密文,把数据呈现出来。

 

三、SSL证书和私钥的生成

1、创建一个保存私钥和证书的目录并进入

[root@localhost ~]# mkdir /usr/local/nginx-1.12.1/key

[root@localhost ~]# cd /usr/local/nginx-1.12.1/key

2、生成私钥文件

[root@localhost key]# openssl genrsa -out server.key 1024

3、生成csr文件          

[root@localhost key]# openssl req -new -key server.key -out certreq.csr

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [XX]:cn    所在国家的ISO标准代号

State or Province Name (full name) []:beijing   单位所在地省/自治区/直辖市

Locality Name (eg, city) [Default City]:beijing    单位所在地的市//

Organization Name (eg, company) [Default Company Ltd]:lvdian   单位/机构/企业合法的名称

Organizational Unit Name (eg, section) []:yunwei  部门名称

Common Name (eg, your name or your server's hostname) []:www.long.com 

主机名,此项必须与您访问提供SSL服务的服务器时所应用的域名完全匹配

 

Email Address []:[email protected]   邮件地址,不必输入,直接回车跳过

 

Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:            以下信息不必输入,回车跳过直到命令执行完毕

An optional company name []:

 

生成类型为X509的自签名证书。有效期设置3650天,即有效期为10

[root@localhost key]# openssl x509 -req -days 3650 -in server.csr -signkey server.key -out server.crt

四、重新编译nginx添加ssl模块(编译过ssl跳过此步骤)

1、查看nginx版本及编译参数

[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -V

nginx version: nginx/1.12.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module

可以看到,我们上面已经编译了ssl模块,如果没有编译ssl模块,我们需要安装以下方法进行编译安装。

2、编译ssl模块

1)备份原有的nginx执行文件

[root@localhost ~]# cp /usr/local/nginx-1.12.1/sbin/nginx /usr/local/nginx-1.12.1/sbin/nginx.bak

2)进入源码解压的nginx目录编译ssl模块

提示:一定要把之前编译过的参数都加进去重新编译,并加入ssl模块

[root@localhost ~]# cd nginx-1.12.1

[root@localhost nginx-1.12.1]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module

 

3)执行make,千万别执行make install,否则就覆盖安装

[root@localhost nginx-1.12.1]# make

 

3、停止nginx,将新生成的nginx文件覆盖原有的nginx文件

make完之后在nginx-1.12.1/objs目录下就多了个nginx,这个就是新版本的nginx执行文件了,将这个文件复制到/usr/local/nginx-1.12.1/sbin/目录覆盖原来的nginx执行文件。

[root@localhost nginx-1.12.1]# cp objs/nginx /usr/local/nginx-1.12.1/sbin/

cp:是否覆盖"/usr/local/nginx-1.12.1/sbin/nginx" y

 

4、测试新的nginx程序是否正确并查看编译参数

[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -t

nginx: the configuration file /usr/local/nginx-1.12.1/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx-1.12.1/conf/nginx.conf test is successful

 

 

[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -v

nginx version: nginx/1.12.1

[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -V

nginx version: nginx/1.12.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --user=nginx --group=nginx --prefix=/usr/local/nginx-1.12.1 --with-http_stub_status_module --with-http_ssl_module

 

五、nginx配置ssl加密

这里的配置是最重要的,可以根据自己的需求进行配置。想要https就要监听443端口,nginx.conf已经预留出了server,只要我们放开权限,简单修改即可。

[root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf

server {

        listen       443 ssl;

        server_name  localhost;

 

        ssl_certificate      /usr/local/nginx-1.12.1/key/certreq.csr;

        ssl_certificate_key  /usr/local/nginx-1.12.1/key/server.key;

 

        ssl_session_cache    shared:SSL:1m;

        ssl_session_timeout  5m;

 

        ssl_ciphers  HIGH:!aNULL:!MD5;

        ssl_prefer_server_ciphers  on;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

}

 

 

#配置端口转发

server {

        listen 80;

        server_name www.long.com;

        rewrite ^(.*) https://$server_name$1 permanent;

}

[root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload

ssl_certificate证书其实是个公钥,它会被发送到连接服务器的每个客户端,ssl_certificate_key私钥是用来解密的,所以它的权限要得到保护但nginx的主进程能够读取。当然私钥和证书可以放在一个证书文件中,这种方式也只有公钥证书才发送到client

 

ssl_session_timeout 客户端可以重用会话缓存中ssl参数的过期时间,内网系统默认5分钟太短了,可以设成30m30分钟甚至4h

ssl_ciphers选择加密套件,不同的浏览器所支持的套件(和顺序)可能会不同。这里指定的是OpenSSL库能够识别的写法,你可以通过 openssl -v cipher RC4:HIGH:!aNULL:!MD5’(后面是你所指定的套件加密算法) 来看所支持算法。

 

ssl_prefer_server_ciphers on设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。

六、Windows浏览器测试能否跳转https成功

1Windows中添加域名解析

打开windowsC:\Windows\System32\drivers\etc\hosts文件,添加下面的域名解析

192.168.10.10     www.long.com

2、打开浏览器,输入域名测试

image.png

image.png

image.png

可以看到,我们的https已经跳转成功

上面的是自己生成的证书,不受各个浏览器信任的,要让各个浏览器信任就得在相关官方网站申请一个免费证书,一般免费证书有效期是几个月至一年。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章