基于openresty的https配置实践

摘要:
错误:lib(40):UI_set_result:您必须键入4到8191个字符服务器的企业密码。key:验证服务器的企业密钥。key:

最近机器人项目的子项目,由于和BAT中的一家进行合作,人家要求用HTTPS连接,于是乎,我们要改造我们的nginx的配置,加添HTTPS的支持。

当然了,HTTPS需要的证书,必须是认证机构颁发的,这里的配置实践,也是从技术路线上的一次操作,证书是基于openssl生成的。没有谁颁发,自建得之!

不多说,开始实践!!!!

1. openssl的版本信息

[root@localhost conf]# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013

2. openresty的版本信息

[root@localhost sbin]# ./nginx -V
nginx version: openresty/1.11.2.2
built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) 
built with OpenSSL 1.0.1u  22 Sep 2016
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt=-O2 --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.60 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.7 --add-module=../ngx_lua_upstream-0.06 --add-module=../headers-more-nginx-module-0.32 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.17 --add-module=../redis2-nginx-module-0.13 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/usr/local/openresty/luajit/lib --with-pcre=/opt/pcre-8.40 --with-openssl=/opt/openssl-1.0.1u --with-http_ssl_module
[root@localhost sbin]#

3. 创建服务器私钥,命令会提醒输入一个密码,必须输入(在nginx的conf所在的路径下进行操作,当然也可以在其他路径,需要配合后续的nginx的配置一起改变

[root@localhost conf]# openssl genrsa -des3 -out server.key 4096
Generating RSA private key, 4096 bit long modulus
..............................................................++
........................++
e is 65537 (0x10001)
Enter pass phrase for server.key:
140180344625056:error:28069065:lib(40):UI_set_result:result too small:ui_lib.c:869:You must type in 4 to 8191 characters
Enter pass phrase for server.key:
140180344625056:error:28069065:lib(40):UI_set_result:result too small:ui_lib.c:869:You must type in 4 to 8191 characters
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
[root@localhost conf]# ll
总用量 64
-rw-r--r--. 1 root root 1077 3月   8 12:08 fastcgi.conf
-rw-r--r--. 1 root root 1077 3月   8 13:20 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 3月   8 12:08 fastcgi_params
-rw-r--r--. 1 root root 1007 3月   8 13:20 fastcgi_params.default
-rw-r--r--. 1 root root 2837 3月   8 13:20 koi-utf
-rw-r--r--. 1 root root 2223 3月   8 13:20 koi-win
-rw-r--r--. 1 root root 3957 3月   8 12:08 mime.types
-rw-r--r--. 1 root root 3957 3月   8 13:20 mime.types.default
-rw-r--r--. 1 root root 3012 3月  14 16:41 nginx.conf
-rw-r--r--. 1 root root 2656 3月   8 13:20 nginx.conf.default
-rw-r--r--. 1 root root  636 3月   8 12:08 scgi_params
-rw-r--r--. 1 root root  636 3月   8 13:20 scgi_params.default
-rw-r--r--  1 root root 3311 7月  11 14:15 server.key
-rw-r--r--. 1 root root  664 3月   8 12:08 uwsgi_params
-rw-r--r--. 1 root root  664 3月   8 13:20 uwsgi_params.default
-rw-r--r--. 1 root root 3610 3月   8 13:20 win-utf

4. 创建签名请求的证书(CSR)

[root@localhost conf]# 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]:cn
State or Province Name (full name) []:hubei
Locality Name (eg, city) [Default City]:wuhan
Organization Name (eg, company) [Default Company Ltd]:tk
Organizational Unit Name (eg, section) []:iflab
Common Name (eg, your name or your server's hostname) []:root
Email Address []:shihuc@163.com

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

5. 在加载SSL支持的Nginx服务器上,使用上述私钥时除去必须的口令(注意,所谓除去,其实就是将必须的私钥密码写入到了私钥文件里面了,更新了原来的私钥文件)

[root@localhost conf]# cp server.key server.key.org
[root@localhost conf]# 
[root@localhost conf]# openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org:
writing RSA key
[root@localhost conf]# 

6. 通过openssl的x509指令生产证书文件

[root@localhost conf]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=cn/ST=hubei/L=wuhan/O=tk/OU=iflab/CN=root/emailAddress=shihuc@163.com
Getting Private key

7. nginx的配置

# HTTPS server
#
server {
    listen       443 ssl;
    server_name  localhost;

    ssl_certificate      server.crt;
    ssl_certificate_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/SSLROOT;
        index  index.html index.htm;
    }
}

在nginx的html目录下,创建SSLROOT目录,并在下面创建一个index.html的页面,用于测试。

<!DOCTYPE html>
<html>
<head>
<title>SHIHUC</title>
<style>
    body {
         50em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>欢迎来到SHIHUC的博客</h1>
<p>你好,你看到的这个页面是用来测试HTTPS的配置过程效果的。</p>

<a href="http://www.cnblogs.com/shihuc/">shihuc</a>.<br/>

<p><em>感谢关注SHIHUC的博客,欢迎交流.</em></p>
</body>
</html>

重启nginx。

在浏览器地址栏输入nginx的服务器地址

https://10.90.7.10

得到下面的效果:

基于openresty的https配置实践第1张

注意,上面图中,地址栏出现红色的不安全提醒。

需要注意的是,注意:

A.  若没有第5步,直接进入第6步,则会在咨询x509指令时提醒用户输入私钥密码:

[root@localhost conf]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Signature ok
subject=/C=cn/ST=hubei/L=wuhan/O=tk/OU=iflab/CN=root/emailAddress=shihuc@163.com
Getting Private key
Enter pass phrase for server.key:
[root@localhost conf]#

B. 另外,在nginx启动的时候,也会提醒用户输入ssl的私钥密码

[root@localhost conf]# ./../sbin/nginx -s reload
Enter PEM pass phrase:

到此,基于openresty的nginx做https的配置,到此一个实践完成。其实很简单。重点是要熟悉下openssl的指令应用!

免责声明:文章转载自《基于openresty的https配置实践》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇微博内容中的短地址 分析Mybatis3源码笔记(一)环境搭建下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

libcurl+openssl实现https爬虫

https图解: 1、客户端向服务端发送SSL协议版本号、加密算法种类、随机数等信息。 2、服务端给客户端返回SSL协议版本号、加密算法种类、随机数等信息,同时也返回服务器端的证书,即公钥证书 3、客户端使用服务端返回的信息验证服务器的合法性,包括: 证书是否过期 发型服务器证书的CA是否可靠 返回的公钥是否能正确解开返回证书中的数字签名 服务器证书上的...

linux/windows 双平台csv文件生成方法

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。 1、linux/windows 可移植 #include <stdio.h> int main() { FILE *fp; char const *fileTit...

linux中查看nginx、apache、php、mysql配置文件路径

linux高效、稳定,但是也带来维护上的一些问题。配置文件究竟在哪里?????       如何在linux中查看nginx、apache、php、mysql配置文件路径了,如果你接收一个别人配置过的环境,但没留下相关文档。这时该怎么判断找到正确的加载文件路径了。可以通过以下来判断1、判断apache 首先执行命令找到httpd路径 ps aux | gr...

wpf 窗体翻页效果

点击设置翻页,取消翻回来 1.xaml 1 <Window x:Class="_3D翻页动画.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.m...

开启Nginx的目录文件列表功能

ngx_http_autoindex_module  此模块用于自动生成目录列表,ngx_http_autoindex_module只在 ngx_http_index_module模块未找到索引文件时发出请求. nginx默认是不允许列出整个目录的。 开启目录列表: 打开nginx.conf文件,在location server 或 http段中加入 au...

开源加密解密库比较

第一个:OpenSSLC语言实现,整个软件包大概可以分成三个主要的功能部分:密码算法库、SSL协议库以及应用程序。OpenSSL的目录结构自然也是围绕这三个功能部分进行规划的。   作为一个基于密码学的安全开发包,OpenSSL提供的功能相当强大和全面,囊括了主要的密码算法、常用的密钥和证书封装管理功能以及SSL协议,并提供了丰富的应用程序供测试或其它目的...