nginx结合openssl实现https的方法


Posted in Servers onJuly 25, 2021

在未使用SSL证书对服务器数据进行加密认证的情况下,用户的数据将会以明文的形式进行传输,这样一来使用抓包工具是可以获取到用户密码信息的,非常危险。而且也无法验证数据一致性和完整性,不能确保数据在传输过程中没被改变。所以网站如果有涉及用户账户等重要信息的情况下通常要配置使用SSL证书,实现https协议。

在生产环境中的SSL证书都需要通过第三方认证机构购买,分为专业版OV证书(浏览器地址栏上不显示企业名称)和高级版EV(可以显示企业名称)证书,证书所保护的域名数不同也会影响价格(比如只对www认证和通配*认证,价格是不一样的),且不支持三级域名。测试中可以自己作为证书颁发机构来制作证书,浏览器会显示为红色,代表证书过期或者无效,如果是黄色的话代表网站有部分连接使用的仍然是http协议。

不管使用哪种方法,在拿到证书后对Nginx的配置都是一样的,所以这里以搭建OpenSSL并制作证书来进行完整说明

一、准备环境

1)nginx服务

2)ssl模块

[root@ns3 ~]# systemctl stop firewalld
[root@ns3 ~]# iptables -F
[root@ns3 ~]# setenforce 0
[root@ns3 ~]# yum -y install pcre zlib pcre-devel zlib-devel
[root@ns3 ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src/
[root@ns3 ~]#cd /usr/src/nginx-1.16.0
[root@ns3 ~]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module&&make && make install #后续需要的模块一次性安装

3)检测openssl是否安装

[root@ns3 ~]# rpm -qa openssl 2 openssl-1.0.1e-42.el7.x86_64

若没有安装

[root@ns3 ~]# yum -y install openssl openssl-devel

二、创建根证书CA

1、生成CA私钥

[root@ns3 ~]# cd zhengshu/
[root@ns3 zhengshu]# openssl genrsa -out local.key 2048
Generating RSA private key, 2048 bit long modulus
...........................................................................................................................................................................................................................+++
............................................................................................................................................................................................+++
e is 65537 (0x10001)
[root@ns3 zhengshu]# ls
local.key

 2、生成CA证书请求

[root@ns3 zhengshu]# openssl req -new -key local.key -out local.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  #国家
State or Province Name (full name) []:BJ   #省份
Locality Name (eg, city) [Default City]:BJ  #城市
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:test   #部门
Common Name (eg, your name or your server's hostname) []:test   #主机名
Email Address []:test@test.com  #邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:wuminyan  #密码
An optional company name []:wuminyan  #姓名
[root@ns3 zhengshu]# ls
local.csr  local.key
req: 这是一个大命令,提供生成证书请求文件,验证证书,和创建根CA
 -new: 表示新生成一个证书请求
 -x509: 直接输出证书
 -key: 生成证书请求时用到的私钥文件
 -out:输出文件

3、生成CA根证书

这个生成CA证书的命令会让人迷惑
1.通过秘钥 生成证书请求文件
2.通过证书请求文件 生成最终的证书
 -in 使用证书请求文件生成证书,-signkey 指定私钥,这是一个还没搞懂的参数
[root@ns3 zhengshu]# openssl x509 -req -in local.csr -extensions v3_ca -signkey local.key -out local.crt
Signature ok
subject=/C=CN/ST=BJ/L=BJ/O=Default Company Ltd/OU=test/CN=test/emailAddress=test@test.com
Getting Private key

三、根据CA证书创建server端证书

1、生成server私匙

[root@ns3 zhengshu]# openssl genrsa -out my_server.key 2048
Generating RSA private key, 2048 bit long modulus
.................................+++
.........................................+++
e is 65537 (0x10001)
[root@ns3 zhengshu]# ls
local.crt  local.csr  local.key  my_server.key

2、生成server证书请求

[root@ns3 zhengshu]# openssl x509 -req -in local.csr -extensions v3_ca -signkey local.key -out local.crt
Signature ok
subject=/C=CN/ST=BJ/L=BJ/O=Default Company Ltd/OU=test/CN=test/emailAddress=test@test.com
Getting Private key
[root@ns3 zhengshu]# openssl genrsa -out my_server.key 2048
Generating RSA private key, 2048 bit long modulus
.................................+++
.........................................+++
e is 65537 (0x10001)
[root@ns3 zhengshu]# openssl req -new -key my_server.key -out my_server.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
State or Province Name (full name) []:BJ
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:test
Common Name (eg, your name or your server's hostname) []:test
Email Address []:test@test.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:wuminyan
An optional company name []:wuminyan
[root@ns3 zhengshu]# ls
local.crt  local.csr  local.key  my_server.csr  my_server.key

3、生成server证书

[root@ns3 zhengshu]# openssl x509 -days 365 -req -in my_server.csr -extensions v3_req -CAkey local.key -CA local.crt -CAcreateserial -out my_server.crt
 Signature ok
 subject=/C=CN/ST=BJ/L=BJ/O=Default Company Ltd/OU=test/CN=test/emailAddress=test@test.com
 Getting CA Private Key

四、配置nginx支持SSL

[root@ns3 ~]# vim /etc/nginx.cof      #这里设置了一个软连接:lln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
server {
        listen 80;
        listen       443 default  ssl;  #监听433端口
                keepalive_timeout 100;  #开启keepalive 激活keepalive长连接,减少客户端请求次数

                   ssl_certificate      /root/zhengshu/local.crt;   #server端证书位置
                   ssl_certificate_key  /root/zhengshu/local.key;   #server端私钥位置

                        ssl_session_cache    shared:SSL:10m;         #缓存session会话
                        ssl_session_timeout  10m;                    # session会话    10分钟过期

                   ssl_ciphers  HIGH:!aNULL:!MD5;
                   ssl_prefer_server_ciphers  on;

        server_name   test.com;
        charset utf-8;

        location / {
            root   html;
            index  index.html index.htm;
        }

    }
}

五、测试

输入https://192.168.200.115

nginx结合openssl实现https的方法

nginx结合openssl实现https的方法

nginx结合openssl实现https的方法

到此这篇关于nginx结合openssl实现https的文章就介绍到这了,更多相关nginx实现https内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
NGINX 权限控制文件预览和下载的实现原理
Jan 18 Servers
nginx刷新页面出现404解决方案(亲测有效)
Mar 18 Servers
Nginx速查手册及常见问题
Apr 07 Servers
nginx日志格式分析和修改
Apr 28 Servers
阿里云 Windows server 2019 配置FTP
Apr 28 Servers
IIS服务器中设置HTTP重定向访问HTTPS
Apr 29 Servers
KVM基础命令详解
Apr 30 Servers
Apache POI操作批量导入MySQL数据库
Jun 21 Servers
Windows Server 2022 超融合部署(图文教程)
Jun 25 Servers
vscode内网访问服务器的方法
Jun 28 Servers
windows server2016安装oracle 11g的图文教程
Jul 15 Servers
ssh服务器拒绝了密码 请再试一次已解决(亲测有效)
Aug 14 Servers
nginx配置虚拟主机的详细步骤
nginx的zabbix 5.0安装部署的方法步骤
nginx请求限制配置方法
使用goaccess分析nginx日志的详细方法
Jul 09 #Servers
nginx作grpc的反向代理踩坑总结
使用 Apache Superset 可视化 ClickHouse 数据的两种方法
使用nginx配置访问wgcloud的方法
Jun 26 #Servers
You might like
php使用ICQ网关发送手机短信
2013/10/30 PHP
destoon实现公司新闻详细页添加评论功能的方法
2014/07/15 PHP
给WordPress的编辑后台添加提示框的代码实例分享
2015/12/25 PHP
php7 图形用户界面GUI 开发示例
2020/02/22 PHP
JSON 数字排序多字段排序介绍
2013/09/18 Javascript
javascript确认框的三种使用方法
2013/12/17 Javascript
Extjs表单常见验证小结
2014/03/07 Javascript
Bootstrap每天必学之基础排版
2015/11/20 Javascript
Vue.js每天必学之Class与样式绑定
2016/09/05 Javascript
jQuery leonaScroll 1.1 自定义滚动条插件(推荐)
2016/09/17 Javascript
Vue列表页渲染优化详解
2017/07/24 Javascript
vue中提示$index is not defined错误的解决方式
2020/09/02 Javascript
ajax jquery实现页面某一个div的刷新效果
2021/03/04 jQuery
[04:39]显微镜下的DOTA2第十三期—Pis卡尔个人秀
2014/04/04 DOTA
[01:14:34]DOTA2上海特级锦标赛C组资格赛#2 LGD VS Newbee第一局
2016/02/28 DOTA
python3使用PyMysql连接mysql数据库实例
2017/02/07 Python
python正则表达式的使用
2017/06/12 Python
利用aardio给python编写图形界面
2017/08/21 Python
Python 3.6 读取并操作文件内容的实例
2018/04/23 Python
python3实现随机数
2018/06/25 Python
Python动态导入模块的方法实例分析
2018/06/28 Python
python画图系列之个性化显示x轴区段文字的实例
2018/12/13 Python
python中用logging实现日志滚动和过期日志删除功能
2019/08/20 Python
深入浅析python变量加逗号,的含义
2020/02/22 Python
python实现逆滤波与维纳滤波示例
2020/02/26 Python
python入门之井字棋小游戏
2020/03/05 Python
matplotlib quiver箭图绘制案例
2020/04/17 Python
英国Lookfantastic中文网站:护肤品美妆美发购物(英国直邮)
2020/04/27 全球购物
外语专业毕业生个人的自荐信
2013/11/19 职场文书
法务专员岗位职责
2014/01/02 职场文书
酒店员工职业生涯规划
2014/02/25 职场文书
协商一致解除劳动合同协议书
2014/09/14 职场文书
八年级英语教学计划
2015/01/23 职场文书
Python将CSV文件转化为HTML文件的操作方法
2021/06/30 Python
spring cloud 配置中心native配置方式
2021/09/25 Java/Android
鸿蒙3.0体验感怎么样? 鸿蒙3.0系统评测向
2022/08/14 数码科技