nginx1.14.0版本https加密配置

修改host文件,为最后访问域名准备

C:\Windows\System32\drivers\etc host文件目录
192.168.10.140 www.joyce.com 在最后添加这个自定义域名

https公钥和私钥定义

服务端:公钥、私钥

服务器持有一对公钥和私钥,并且把自己的公钥发给客户端。

当浏览器发起申请时,数据通过浏览器端的私钥加密发送给服务端。服务端拿到加密密文时,通过浏览器的公钥解密得到数据。

服务端再通过自己的私钥加密返回数据到浏览器,浏览器拿到密文后通过服务端的公钥解密得到数据。

 下载nginx和安装

cd /usr/local
wget http://nginx.org/download/nginx-1.14.0.tar.gz           下载
tar -zxvf nginx-1.14.0.tar.gz              解压
/usr/local/nginx/sbin/nginx -V            查看ngixn版本极其编译参数
cd nginx-1.14.0                                 进入nginx源码目录
./configure
#make & make install                        编译和安装

要添加ssl加密模块,需重新编译模块

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
make                                    千万别make & make install,否则就覆盖安装了。make完之后在objs目录下就多了个nginx,这个就是新版本的程序了
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak                              备份旧的nginx程序
rm -rf /usr/local/nginx/sbin/nginx                             先删除旧的nginx程序
cp objs/nginx /usr/local/nginx/sbin/nginx                把新的nginx程序覆盖旧的

生成https crt证书文件

yum -y update         更新yum源

yum -y install openssl         安装ssl

cd /usr/local/nginx         

mkdir ssl             创建ssl文件夹

cd ssl

openssl genrsa -des3 -out server.key 1024          生成server.key私钥文件,长度为1024,需要指定一个密码:123456

-out filename     :将生成的私钥保存至filename文件,若未指定输出文件,则为标准输出。
-numbits            :指定要生成的私钥的长度,默认为1024。该项必须为命令行的最后一项参数。
-des|-des3|-idea:指定加密私钥文件用的算法,这样每次使用私钥文件都将输入密码,太麻烦所以很少使用。
-passout args    :加密私钥文件时,传递密码的格式,如果要加密私钥文件时单未指定该项,则提示输入密码。传递密码的args的格式见openssl密码格式。

openssl req -new -key server.key -out server.csr         生成server.csr公钥文件,需要输入server.key里指定的密码,我这里是:123456


[root@192 ssl]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:
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]:cs
State or Province Name (full name) []:www.joyce.com
Locality Name (eg, city) [Default City]:ShangHai
Organization Name (eg, company) [Default Company Ltd]:joyce
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:joyce
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@192 ssl]#

以上这些后面带[]的都是选填,可以直接回车不填。 

接下来去除私钥的口令验证,也就是去除用户名密码登录校验

cp server.key server.key.org         先复制一份

openssl rsa -in server.key.org -out server.key              去除口令后,覆盖server.key文件。需要输入server.key里指定的密码:123456

 openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt       标记证书使用私钥和csr,使用x509格式,有效期限365天

[root@192 ssl]#  openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=cs/ST=www.joyce.com/L=ShangHai/O=joyce/CN=joyce
Getting Private key

注意!server.crt 就是我们需要的证书!

nginx.conf 配置https配置

vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

     server {
      listen 80;
      server_name www.joyce.com;    
      return 301 https://$server_name$request_uri;       301重定向到https协议端口,这样访问http://www.joyce.com会自动跳转到https://www.joyce.com

          # 可以参考:https://www.cnblogs.com/liuq1991/p/9019900.html  (nginx http转 https)

    }

    server {    
        listen 443 ssl;
        server_name www.joyce.com;
        ssl on;           启用https协议访问
        ssl_certificate /usr/local/nginx/ssl/server.crt;      服务端公钥
        ssl_certificate_key /usr/local/nginx/ssl/server.key;     服务端私钥
        error_log  /usr/local/nginx/logs/error443.log;
        location / {
             proxy_pass http://192.168.10.140:8761;         访问应用
        }
    } 
}

 测试新的nginx.conf是否配置正确

 /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -t

输出如下结果代表nginx.conf配置文件无误:

nginx: theconfiguration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx:configuration file /usr/local/nginx/conf/nginx.conf test issuccessful

平滑重启nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s reload

查看ngixn版本极其编译参数

/usr/local/nginx/sbin/nginx -V 

开启443端口(已关闭防火墙firewalld忽略这一步)

关闭防火墙     systemctl stop firewalld

开启防火墙     systemctl start firewalld 

查看防火墙状态:  systemctl status firewalld

 如果防火墙被开启,则有可能存在443端口没有开启监听的情况

firewall-cmd --zone=public --add-port=443/tcp –permanent
firewall-cmd --reload

 查看防火墙里添加的端口

firewall-cmd --list-ports

 查看是否在防火墙里开启了443端口监听

netstat -antp|grep 443       结果:

 tcp 0 0 0.0.0.0:443 0.0.0.0:*   LISTEN 2037/nginx:master            //代表防火墙开启,并监听了在nginx程序,表示成功。

 浏览器端输入https://www.joyce.com 可以查看公钥

 

注意!最后访问的是https协议!而不是http!
在浏览器里输入https://www.joyce.com/ 即可访问成功!

 

 

posted on 2019-03-20 11:25  梦幻朵颜  阅读(2648)  评论(0编辑  收藏  举报