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.conf 中 root 目录设置问题
Apr 01 Servers
uwsgi+nginx代理Django无法访问静态资源的解决
May 10 Servers
图文详解nginx日志切割的实现
Jan 18 Servers
了解Kubernetes中的Service和Endpoint
Apr 01 Servers
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
Apr 07 Servers
Linux下使用C语言代码搭建一个简单的HTTP服务器
Apr 13 Servers
centos7安装mysql5.7经验记录
May 02 Servers
windows server2008 开启端口的实现方法
Jun 25 Servers
git中cherry-pick命令的使用教程
Jun 25 Servers
Apache Kafka 分区重分配的实现原理解析
Jul 15 Servers
服务器nginx权限被拒绝解决案例
Sep 23 Servers
Apache SkyWalking 监控 MySQL Server 实战解析
Sep 23 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伪造referer实例代码
2008/09/20 PHP
php HandlerSocket的使用
2011/05/02 PHP
PHP中去掉字符串首尾空格的方法
2012/05/19 PHP
php实现文件下载简单示例(代码实现文件下载)
2014/03/10 PHP
php上传文件并存储到mysql数据库的方法
2015/03/16 PHP
使用PHP进行微信公众平台开发的示例
2015/08/21 PHP
php生成网页桌面快捷方式
2017/05/05 PHP
PHP大文件分块上传功能实例详解
2019/07/22 PHP
安装PHP扩展时解压官方 tgz 文件后没有configure文件无法进行配置编译的问题
2020/08/26 PHP
Using the TextRange Object
2006/10/14 Javascript
Jquery 1.42 checkbox 全选和反选代码
2010/03/27 Javascript
jquery链式操作的正确使用方法
2014/01/06 Javascript
javascript history对象(历史记录)使用方法(实现浏览器前进后退)
2014/01/07 Javascript
js动态往表格的td中添加图片并注册事件
2014/06/12 Javascript
javascript封装 Cookie 应用接口
2015/08/07 Javascript
jquery实现像栅栏一样左右滑出式二级菜单效果代码
2015/08/24 Javascript
jquery实现动画菜单的左右滚动、渐变及图形背景滚动等效果
2015/08/25 Javascript
JavaScript中省略元素对数组长度的影响
2016/10/26 Javascript
详解微信小程序 登录获取unionid
2017/06/27 Javascript
angular2/ionic2 实现搜索结果中的搜索关键字高亮的示例
2018/08/17 Javascript
vue webpack开发访问后台接口全局配置的方法
2018/09/18 Javascript
swiper.js插件实现pc端文本上下滑动功能示例
2018/12/03 Javascript
JavaScript的变量声明与声明提前用法实例分析
2019/11/26 Javascript
微信小程序实现多张图片上传功能
2020/11/18 Javascript
Python3 webservice接口测试代码详解
2020/06/23 Python
canvas绘制圆角头像的实现方法
2019/01/17 HTML / CSS
HTML5 video循环播放多个视频的方法步骤
2020/08/06 HTML / CSS
银行员工职业规划范文
2014/01/21 职场文书
党委班子剖析材料
2014/08/21 职场文书
2014年客服工作总结范文
2014/11/13 职场文书
开除员工通知
2015/04/22 职场文书
2015年小学数学教师个人工作总结
2015/05/25 职场文书
2016父亲节感恩话语
2015/12/09 职场文书
运动会口号霸气押韵
2015/12/24 职场文书
公务员的复习计划书,请收下!
2019/07/15 职场文书
Python Django ORM连表正反操作技巧
2021/06/13 Python