Nginx要开启ssl功能,Windows版本自带http_ssl_module
模块而Linux需要安装,安装此模块要先安装openssl
,整挺步骤如下:
安装环境:
[root@tcloud ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
本次安装的版本为openssl-1.1.1s.tar.gz
,安装脚本opensslInstall.sh
内容如下:
#!/bin/bash
echo "(1/4): 安装依赖 gcc zlib..."
yum -y install gcc zlib
echo "(1/4): 依赖 gcc zlib安装完毕"
echo "(2/4): 解压$1.tar.gz安装文件..."
tar -zxvf $1.tar.gz -C /usr/local
sleep 3
echo "(2/4): 安装文件解压完毕"
echo "(3/4): 检测OpenSSL安装环境..."
cd /usr/local/$1
./config shared zlib --prefix=/usr/local/openssl
echo "(3/4): OpenSSL安装环境检测成功"
echo "(4/4): 编译安装OpenSSL..."
make && make install
echo "(4/4): OpenSSL编译安装完成"
cat <<'EOF' > /etc/profile.d/openssl.sh
export OPENSSL=/usr/local/openssl/bin
export PATH=$OPENSSL:$PATH:$HOME/bin
EOF
sleep 1
source /etc/profile.d/openssl.sh
脚本调用:
# 脚本参数为安装文件的版本信息 也就是解压后的文件夹
./opensslInstall.sh openssl-1.1.1s
这里要特别注意:tar -zxvf $1.tar.gz -C /usr/local
,实际安装完成后OpenSSL的文件存储在/usr/local/openssl-1.1.1s
路径下,后边Nginx安装http_ssl_module会用到这个文件夹。
#!/bin/bash
echo "(1/4): 安装依赖pcre-devel..."
yum -y install pcre-devel
sleep 5
echo "(1/4): 依赖pcre-devel安装完毕"
echo "(2/4): 解压Nginx安装文件..."
tar -zxvf nginx-1.23.1.tar.gz
sleep 3
echo "(2/4): Nginx安装文件解压完毕"
echo "(3/4): 检测Nginx安装环境..."
cd nginx-1.23.1/
./configure --with-http_ssl_module --with-openssl=/usr/local/openssl-1.1.1s --prefix=/usr/local/nginx1.23.1
echo "(3/4): Nginx安装环境检测成功"
echo "(4/4): 编译安装Nginx..."
make & make install
echo "(4/4): Nginx编译安装完成"
这里有个大坑--with-http_ssl_module --with-openssl=/usr/local/openssl-1.1.1s
里的--with-openssl=/usr/local/openssl-1.1.1s
是OpenSSL解压后的源码文件夹而不是安装后的文件夹,需要特别注意。
# 1.生成长度为1024的RSA密钥对并使用Triple DES算法进行加密
openssl genrsa -des3 -out test-en.key 1024
# 输入密码并验证密码【第2和第3步】都会用到这个密码
Enter pass phrase for test-en.key:
Verifying - Enter pass phrase for test-en.key:
# 2.创建新的证数签名
openssl req -new -key test-en.key -out test.csr
# 输入【第1步的密码】
Enter pass phrase for test-en.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.
-----
# 除 Common Name 外都可为空
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HN
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default Company Ltd]:TEST
Organizational Unit Name (eg, section) []:DC
Common Name (eg, your name or your server's hostname) []:192.168.0.1
Email Address []:.
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:.
An optional company name []:.
# 3.去掉密码
openssl rsa -in test-en.key -out test.key
# 输入【第1步的密码】
Enter pass phrase for test-en.key:
writing RSA key
# 4.生成一个自签名的X.509数字证书 有效期365天
openssl x509 -req -days 365 -in test.csr -signkey test.key -out test.crt
# 成功标志
Signature ok
该命令根据提供的证书签名请求文件 test.csr 和私钥文件 test.key,生成一个自签名的 X.509 数字证书,并将生成的证书保存在 test.crt 文件中。自签名证书是由私钥持有者自行签署的证书,不需要经过第三方证书颁发机构(CA)的认证。
生成的自签名证书可以用于各种目的,例如用于测试、开发环境中的安全通信、内部网站等。请注意,自签名证书在公共环境中可能会被浏览器或其他应用程序视为不受信任,因为它们没有受到公共信任的第三方机构的认证。
自动化生成脚本certificateGenerate.sh
内容如下:
#!/bin/bash
echo "(1/5): 生成密钥对开始..."
openssl genrsa -des3 -out test-en.key -passout pass:test 1024
echo "(1/5): 生成密钥对结束。"
echo "(2/5): 创建新的证数签名开始..."
openssl req -new -key test-en.key -out test.csr -passin pass:test -subj "/C=CN/ST=HN/L=ZZ/O=TEST/CN=$1"
sleep 2
echo "(2/5): 创建新的证数签名结束。"
echo "(3/5): 密钥去除密码..."
openssl rsa -in test-en.key -out test.key -passin pass:test
echo "(3/5): 密钥去除密码成功。"
echo "(4/5): 生成自签名证数..."
openssl x509 -req -days 3650 -in test.csr -signkey test.key -out test.crt
echo "(4/5): 生成自签名证数成功。"
echo "(5/5): 删除过程文件..."
rm -rf test-en.key test.csr
echo "(5/5): 删除过程文件成功。"
脚本调用:
# 脚本参数为IP或域名
./certificateGenerate.sh tcloud
这里删除一些注释仅保留核心配置,将https://IP:18080
的请求转发到http://IP:8080
下:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 18080 ssl;
server_name localhost;
ssl_certificate /pathto/test.crt;
ssl_certificate_key /pathto/test.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:8080/;
proxy_redirect off;
proxy_set_header Host $host:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Cookie $http_cookie;
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。