zl程序教程

您现在的位置是:首页 >  前端

当前栏目

nginx使用OpenSSL自签证书支持https

NginxHTTPS 支持 OpenSSL 使用
2023-09-14 09:15:46 时间

一、下载OpenSSL

OpenSSL下载地址:点击跳转下载页面

1.1、安装openssl

# tar -xzf openssl-1.0.2f.tar.gz
# cd openssl-1.0.2f
# mkdir /usr/local/openssl
# ./config --prefix=/usr/local/openssl
# make
# make install

1.2、创建软连接

# which openssl
/usr/local/openssl/bin/openssl

# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl

1.3、检查版本

# openssl version
/usr/local/openssl/bin/openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory
  • 找不到动态库libssl.so.1.1,执行如下命令:
# vim /etc/ld.so.conf
在最后添加一行:
/usr/local/openssl/lib 

然后再重新执行:

# ldconfig /etc/ld.so.conf
# openssl version
OpenSSL 1.1.0f  25 May 2017

二、自签证书

2.1、创建新目录

mkdir certs

2.2、生成证书

# Generate an RSA key
openssl genrsa -des3 -out Server.key 1024

# Creating Certificate Signing Requests
# 需要输入组织信息,留空输入点(.),而非直接回车(用缺省值)
openssl req -new -key Server.key -out Cert.csr

# 移除口令
cp Server.key Server.key.org
openssl rsa -in Server.key.org -out Server.key

# Signing Your Own Certificates
openssl x509 -req -days 365 -in Cert.csr -signkey Server.key -out Cert.crt

2.3、检查

#ls /usr/local/nginx/conf/certs
Cert.crt  Cert.csr  Server.key  Server.key.org

三、修改nginx配置

确保http_ssl_module已安装。

否则会报错:nginx: [emerg] the “ssl” parameter requires ngx_http_ssl_module

添加SSL配置:

# vim /usr/local/nginx/conf/nginx.conf
在server{}层添加以下内容:
        listen       80 default_server;
        ...
    #支持https
        listen       443 ssl;
        server_name  nginx.test.com
        ssl off;
        ssl_certificate /usr/local/nginx/conf/Cert.crt;
        ssl_certificate_key /usr/local/nginx/conf/Server.key;

 至此全部完成,使用浏览器访问http://nginx.test.com和https://nginx.test.com。