Nginx配置HTTPS证书网站

news/2024/5/13 5:00:16/文章来源:https://blog.csdn.net/weixin_34038293/article/details/90196115

前提:

    1、主机需要先安装openssl

    2、编译安装nginx时,要加上--with-http_ssl_module  这个ssl模块

现在开始配置:(我当时配置时,主机已安装了openssl,但编译时没有加载http_ssl_module模块,所以后面会报错,这里详解说明下)

1、生成自签字证书

复制代码
 1 [root@localhost /]# openssl req -new -x509 -keyout /root/ca.key -out /root/ca.crt
 2 Generating a 2048 bit RSA private key
 3 .............................+++
 4 .......................................................................................................................+++
 5 writing new private key to '/root/ca.key'
 6 Enter PEM pass phrase:           #输入密钥保护密码
 7 Verifying - Enter PEM pass phrase:       #确认密钥保护密码
 8 -----
 9 You are about to be asked to enter information that will be incorporated
10 into your certificate request.
11 What you are about to enter is what is called a Distinguished Name or a DN.
12 There are quite a few fields but you can leave some blank
13 For some fields there will be a default value,
14 If you enter '.', the field will be left blank.
15 -----
16 Country Name (2 letter code) [XX]:CN
17 State or Province Name (full name) []:xian
18 Locality Name (eg, city) [Default City]:xian
19 Organization Name (eg, company) [Default Company Ltd]:learn
20 Organizational Unit Name (eg, section) []:it
21 Common Name (eg, your name or your server's hostname) []:learner
22 Email Address []:ying@126.com
复制代码

回车结束

2、修改配置文件openssl.cnf (注意:修改前,先备份下)

复制代码
[root@localhost /]# vi /etc/pki/tls/openssl.cnf####################################################################
[ ca ]
default_ca      = CA_default            # The default ca section####################################################################
[ CA_default ]dir             = /etc/pki/CA           # Where everything is kept #证书的根目录,要记住这个目录
certs           = $dir/certs            # Where the issued certs are kept
crl_dir         = $dir/crl              # Where the issued crl are kept
database        = $dir/index.txt        # database index file.
#unique_subject = no                    # Set to 'no' to allow creation of# several ctificates with same subject.
new_certs_dir   = $dir/newcerts         # default place for new certs.certificate     = $dir/ca.crt           # The CA certificate  # 修改这里,表示签名时使用的证书 
serial          = $dir/serial           # The current serial number
crlnumber       = $dir/crlnumber        # the current crl number# must be commented out to leave a V1 CRL
crl             = $dir/crl.pem          # The current CRL
private_key     = $dir/private/cakey.pem# The private key
RANDFILE        = $dir/private/.rand    # private random number file
复制代码

3、复制证书到证书根目录/etc/pki/CA下,并在该目录下创建空文件index.txt和serial,并向serial输入”01“

复制代码
1 [root@localhost ~]# cd /etc/pki/CA/
2 [root@localhost CA]# cp /root/ca.crt .
3 [root@localhost CA]# ls
4 ca.crt  certs  crl  newcerts  private
5 [root@localhost CA]# touch index.txt
6 [root@localhost CA]# touch serial
7 [root@localhost CA]# echo "01" >serial
复制代码

4、生成服务器RSA私钥/root/server.key

复制代码
1 [root@localhost ~]# openssl genrsa -des3 -out /root/server.key 1024
2 Generating RSA private key, 1024 bit long modulus
3 .............++++++
4 .++++++
5 e is 65537 (0x10001)
6 Enter pass phrase for /root/server.key: #设置此密钥的保护密码
7 Verifying - Enter pass phrase for /root/server.key: #确认设置此密钥的保护密码
复制代码

5、为私钥去除口令---公钥

1 [root@localhost ~]# openssl rsa -in /root/server.key -out /root/server_nopwd.key
2 Enter pass phrase for /root/server.key: #输入第4步生成的密钥的保护密码 
3 writing RSA key

6、生成证书请求文件/root/server.csr

复制代码
 1 [root@localhost ~]# openssl req -new -key /root/server.key -out /root/server.csr
 2 Enter pass phrase for /root/server.key:  #输入第4步生成的密钥的保护密码 
 3 You are about to be asked to enter information that will be incorporated
 4 into your certificate request.
 5 What you are about to enter is what is called a Distinguished Name or a DN.
 6 There are quite a few fields but you can leave some blank
 7 For some fields there will be a default value,
 8 If you enter '.', the field will be left blank.
 9 --------下面这部分应该和创建私有证书时填的一样------------------------ 
10 Country Name (2 letter code) [XX]:CN
11 State or Province Name (full name) []:xian
12 Locality Name (eg, city) [Default City]:xian
13 Organization Name (eg, company) [Default Company Ltd]:learn
14 Organizational Unit Name (eg, section) []:it
15 Common Name (eg, your name or your server's hostname) []:learner
16 Email Address []:ying@126.com
17 ---------------------------------------------------------------- 
18 Please enter the following 'extra' attributes
19 to be sent with your certificate request
20 A challenge password []:111111
21 An optional company name []:learn
复制代码

7、用私有证书给证书请求文件/root/server.csr签名

复制代码
 1 [root@localhost ~]# openssl ca -in /root/server.csr -out /root/server.crt -cert /root/ca.crt -keyfile /root/ca.key -config /etc/pki/tls/openssl.cnf
 2 Using configuration from /etc/pki/tls/openssl.cnf
 3 Enter pass phrase for /root/ca.key: #输入第1步生成的密钥的保护密码
 4 Check that the request matches the signature
 5 Signature ok
 6 Certificate Details:
 7         Serial Number: 1 (0x1)
 8         Validity
 9             Not Before: Nov 17 07:47:05 2016 GMT
10             Not After : Nov 17 07:47:05 2017 GMT
11         Subject:
12             countryName               = CN
13             stateOrProvinceName       = xian
14             organizationName          = learn
15             organizationalUnitName    = it
16             commonName                = learner
17             emailAddress              = ying@126.com
18         X509v3 extensions:
19             X509v3 Basic Constraints:
20                 CA:FALSE
21             Netscape Comment:
22                 OpenSSL Generated Certificate
23             X509v3 Subject Key Identifier:
24                 8A:70:77:B0:32:42:49:AF:85:AD:79:C3:36:1F:43:A5:C5:01:15:E2
25             X509v3 Authority Key Identifier:
26                 keyid:83:10:7A:45:18:47:D2:27:F8:A0:81:C8:FE:A8:53:9A:1E:BC:D3:77
27 
28 Certificate is to be certified until Nov 17 07:47:05 2017 GMT (365 days)
29 Sign the certificate? [y/n]:y
30 
31 
32 1 out of 1 certificate requests certified, commit? [y/n]y
33 Write out database with 1 new entries
34 Data Base Updated
复制代码

8、编辑nginx配置文件/usr/local/nginx/conf/nginx.conf

复制代码
 1 server {
 2          listen       8001 ssl;
 3          server_name  x.x.x.x:8001; 
 4 
 5          ssl    on;
 6          ssl_certificate /root/server.crt;
 7          ssl_certificate_key /root/server_nopwd.key;
 8 
 9          location / {
10              root   /var/www/html;
11              index  index.html index.htm;
12          }
13     }
复制代码

9. 重启服务

1 [root@localhost sbin]# ./nginx -s reload

~~~~完成,在客户端上输入https://x.x.x.x:8001/即可访问成功。

当时由于安装nginx时,未编译http_ssl_module模块,导致nginx重启失败------提示:nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/ng.........

所以需要重新编译nginx来添加需要的模块。

 

Nginx重新编译添加模块

1. 找到安装nginx的源码根目录(即安装包存放目录),如果没有的话下载新的源码并解压

1 [root@localhost /]# cd software
2 [root@localhost software]# ls
3 nginx-1.10.2  nginx-1.10.2.tar.gz

 

2. 查看nginx版本极其编译参数

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

 

3. 进入nginx源码目录

1 [root@localhost software]# cd nginx-1.10.2

 

4.重新编译的代码和模块

[root@localhost nginx-1.10.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

 

5.  make下 (注意:千万别make install,否则就覆盖安装了),make完之后在/software/nginx-1.10.2/objs目录下就多了个nginx,这个就是新版本的程序了

6. 备份旧的nginx程序

1 [root@localhost ~]# cd /usr/local/nginx/sbin/
2 [root@localhost sbin]# ls
3 nginx
4 [root@localhost sbin]# cp nginx nginx_back_by_zhang20161117
5 [root@localhost sbin]# ls
6 nginx  nginx_back_by_zhang20161117

7. 删除旧的nginx程序,并把新的nginx程序复制到/usr/local/nginx/sbin/下

1 [root@localhost sbin]# rm nginx
2 rm:是否删除普通文件 "nginx"?y
3 [root@localhost sbin]# cp /software/nginx-1.10.2/objs/nginx /usr/local/nginx/sbin/

8. 测试新的nginx程序是否正确

1 [root@localhost sbin]# /usr/local/nginx/sbin/nginx -t
2 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
3 nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

9. 平滑启动服务 (非必须)

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

10. 查看模块是否已安装 (非必须)

1 [root@localhost sbin]# /usr/local/nginx/sbin/nginx -V
2 nginx version: nginx/1.10.2
3 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)
4 built with OpenSSL 1.0.1e-fips 11 Feb 2013
5 TLS SNI support enabled
6 configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module

11. 重启 

1 [root@localhost sbin]# ./nginx -s quit
2 [root@localhost sbin]# ./nginx

nginx重新加载模块完成!

 

apache配置https 参考: http://ask.apelearn.com/question/1029

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_722610.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

nicetool好工具_上进青年研习社丨这6个工具合集网站,抵过上百个软件!

文丨小渔不知道你们有没有这样的经历:为了一些小的需求,在手机上或是电脑上下了一堆软件。虽然用过觉得也挺好用的,但是平时的使用频率真的不高啊。等你把它们都卸载了,过了一段时间突然又需要用它了。所以,今天小渔要…

快站模板 连接mysql_如何轻松建站?站点一键部署搭建(详细教程)

来越多的人选择个人建站,个人站长虽然门槛很低,但是有些朋友觉得Linux服务器各种复杂的命令脚本让自己没法搭建环境。今天就给大家推荐一款使用方便、功能强大的快速建站工具——云帮手,支持 Linux 与 Windows 系统,可一键配置搭建…

查看 db2 正在 运行的sql_介绍两个刷 SQL 题的网站

今天有朋友在群里问到:在哪可以刷 SQL 题?其实,要刷 SQL 题,对着《SQL COOKBOOK》、《SQL 经典实例》这两本书,把所有实例从头到尾全部实现一遍就很厉害了。不过,我想她的本意是想知道有没有关于 SQL 的 On…

html使用bootstrap无效果,[0失败]手把手教你用Bootstrap做个人网站

作为一个纯小白手残星人,总想自己做一个网站,Wix,Strikingly这一类的可视化建站的工具,自由度不高,付费贼贵,自己做的又不好看又费时间,听说bootstrap这种前端框架可以自己做网页,之…

实验六:MYSQL+PHP的网站搭建

实验目的:掌握MYSQLPHP的网站的搭建过程实验准备:一台Server,一台Client实验前的准备:搭建一台web服务器(过程省略)实验步骤:步骤一1.安装PHP数据包解压php-5.1.4-Win32这个解压包,把…

沙发家具网站源码_2020年(双十二)沙发选购全攻略~~

沙发算是家庭装修最昂贵的家具之一,当然我特别建议选购时亲自试用感受它们,由于这些物品尺寸的特殊性,网购最担心的就是购买后自己如果不喜欢,退货麻烦以及昂贵的运费。现在由于网上购物的便利,以及网上的风格多样化&a…

java项目修改html文件类型,爱了!Guide哥手把手教你搭建一个文档类型的网站!免费且高速!...

这篇文章 Guide 哥就手把手教大家搭建一个像下面这样的文档类型的网站,你可以用来当做项目的说明文档,也还可以当做自己专属的知识小仓库。一.前置条件确保自己电脑下载安装了 NPM 并且使用这个命令: npm i docsify-cli -g安装了 docsify-cli…

LNMP动态网站部署架构 Linux + Nginx 配置Nginx服务

1、解压、编译、生成、安装Nginx服务程序的源码文件Perl语言软件包pcre2、openssl软件包安装/usr/local/openssl/bin目录添加到PATH环境变量中。3、安装zlib软件包4、创建一个用于执行Nginx服务程序的账户。5、在使用命令编译Nginx服务程序时,需要设置特别多的参数&…

网站实时显示人数服务器插件,WordPress网站显示实时在线人数代码分享,显示当前在线浏览人数...

其实Wordpress有类似的插件,比如:WP OnlineCounter、WP-User Online等,不过这些插件都会向数据库中写入数据,并频繁读取数据库,只为了实现这个蛋疼的小功能,不是很划算。随时了解一下当前有多少访客在浏览自…

qt listwidget 关键字颜色_seo关键字优化工具如何收费

如何收费sj67745eo关键字优化工具,企业经常采用SEO优化,以获得更好的排名和更好的流量自己的网站。搜索引擎优化有哪些方法?或者什么更有效?任何一个站点,都是无法脱离内容,它是网站建设的基石,…

安卓封装网站源码_h5封装是什么意思_h5封装的app与原生的有什么区别

什么是h5封装?h5封装APP是指将h5网站或web网站进行打包制作成APP软件,无需编写原生代码即可完成APP开发,同时支持苹果和安卓两个系统。除了网站本身的功能内容之外,鹿客邦还将提供多样的插件,丰富APP的功能。封装后的a…

fiddler如何设置过滤https_传奇网站被劫持怎么办?如何设置网站https防劫持 申请免费https和配置教程...

现在劫持相当严重,很多朋友在开区时网站被各种劫持,所以今天给大家分享一个把网站http变成https的教程,能有效防止网站劫持,注意:现在的各大登陆器还不支持https,所以列表地址你还是得用http,所…

在div中显示html_网站开发为什么使用div+css布局?

DIVCSS是WEB设计标准,它是一种网页的布局方法。与传统中通过表格(table)布局定位的方式不同,它可以实现网页页面内容与表现相分离。使用DIVCSS布局的优势1、符合W3C标准。这保证您的网站不会因为将来网络应用的升级而被淘汰。2、对…

长尾词推广系统推荐乐云seo_吴桥信息如何推广广方法有哪些

吴桥信息如何推广广方法有哪些大词虽然搜索量比较高,但是客户都不是精准客户,很多打电话过来,只是问一下价格,购买的需求度并不高但是精准词就不一样了,精准词一般都是通过二次搜索,客户精准度比较高虽然给…

IIS 部署 Python Django网站流程(受够了野路子)

知道的,百度上搜出来的东西质量令人唏嘘。当你求助的时候多半还得靠自己,或者靠Google 介入正题,详细来一遍流程吧 当然,我是用Visual Studio 2019 来编辑开发Django项目的,如果你也是那么巧了。这可以帮你 如果你Djan…

Linux上安装xampp来搭建WordPress网站(图文教程)

建站准备 建站流程 搭建网站注意事项可以参考以下文章 如何从零开始拥有自己的网站?新手建站十大必知忠告超详细新手建站指南以及预算成本估计注册域名 我是在阿里云万网上注册的域名:注册域名详情地址 。不懂这一步的可以参考建站流程里面的几篇文章。 …

基于springboot2.0的易居二手房网站-房屋管理系统

基于springboot2.0的易居二手房网站-房屋管理系统-java二手房网站java房屋管理系统 1.包含源程序,数据库脚本。代码和数据库脚本都有详细注释。2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善开发环境:Eclipse for J2EE, MYSQL5.1, …

大型网站技术架构(八)网站的安全架构

2019独角兽企业重金招聘Python工程师标准>>> 从互联网诞生起,安全威胁就一直伴随着网站的发展,各种Web攻击和信息泄露也从未停止。常见的攻击手段有XSS攻击、SQL注入、CSRF、Session劫持等。 1、XSS攻击 XSS攻击即跨站点脚本攻击(…

geojson地图_从零开始,构建电子地图网站:0_11_引用geojson

在上篇中,我在MapController类中新建了一个方法JSONObject geometryToJson(Geometry geometry),用以将geometry对象转为json。这个方法有点问题,首先,point是一个点,只有一组坐标,polygon如果有孔洞&#x…

域名被墙检测网站_网站监控优化,网站监控比网站优化重要

为什么说一个网站,最重要的不是优化,而是监控?每个网站开发人员都有这样的困扰,网站已被推行和迭代,并添加了新的功用。交互式界面变得愈加美观和简约,但网站曾经加快了速度。为了找到成绩的本源,我必需从头…