OpenSSL 与 Nginx SSL

七月 18, 2025 [tools] #openssl #nginx #ssl #tls #docker #nginx

OpenSSL 与 Nginx SSL

OpenSSL 证书操作

快速生成自签名证书

openssl req -x509 -newkey rsa:2048 -nodes -keyout my.key -out my.crt -days 3650 \
  -subj "/C=CN/ST=shanghai/L=shanghai/O=example/OU=it/CN=domain1"

-subj 字段含义:

字段含义
/C=国家(Country)
/ST=省份(State)
/L=城市(Location)
/O=组织(Organization)
/OU=部门(Organization Unit)
/CN=域名或 IP(Common Name)

查看证书

openssl x509 -text -noout -in domain.crt
openssl verify -verbose -CAfile ca.crt domain.crt

验证证书与私钥是否匹配

openssl rsa  -noout -modulus -in domain.key | openssl md5
openssl x509 -noout -modulus -in domain.crt | openssl md5
openssl req  -noout -modulus -in domain.csr | openssl md5
# 三个 MD5 值一致即匹配

私钥加/解密

# 加密(DES3 密码保护)
openssl rsa -des3 -in unencrypted.key -out encrypted.key

# 解密(取消密码保护)
openssl rsa -in encrypted.key -out decrypted.key

PEM / DER 格式互转

# PEM → DER(二进制)
openssl x509 -in domain.crt -outform der -out domain.der

# DER → PEM
openssl x509 -inform der -in domain.der -out domain.crt

测试 SSL 服务

# 启动测试 SSL 服务端
openssl s_server --cert my.crt --key my.key

# 客户端连接测试
openssl s_client localhost:4433 < /dev/null

CA 证书制作过程

mkdir demoCA && cd demoCA

# 1. 生成 CA 自签名证书
openssl genrsa -out ./private/ca.key 2048
openssl req -new -x509 -days 3650 -key ./private/ca.key -out ./private/ca.crt -config openssl.cnf

# 2. 生成服务端私钥和 CSR
openssl genrsa -out ./private/server.key 2048
openssl req -new -key ./private/server.key -out ./newcerts/server.csr -config openssl.cnf

# 3. CA 签发服务端证书
openssl ca -in ./newcerts/server.csr -cert ./private/ca.crt -keyfile ./private/ca.key \
  -config openssl.cnf -policy policy_anything -out ./certs/server.crt

# 4. 查看签发的证书
openssl x509 -in ./certs/server.crt -noout -text

常见错误

Nginx SSL 配置

server {
    listen 443 ssl;
    server_name yourdomain.com;

    ssl_certificate     /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        proxy_pass http://192.168.2.23:9090;  # 反向代理后端服务
    }
}

Docker Nginx + SSL

方案一:构建自定义镜像

Dockerfile

FROM nginx
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY ssl /etc/nginx/ssl
docker build -t mynginx .
docker run -d -p 443:443 mynginx

方案二:挂载目录

docker run -d --name nginx nginx
docker cp nginx:/etc/nginx /opt/nginx
docker rm -f nginx

# 放入证书
mkdir /opt/nginx/ssl
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
  -keyout /opt/nginx/ssl/nginx.key -out /opt/nginx/ssl/nginx.crt

# 启动
docker run -d -p 80:80 -p 443:443 --name nginx \
  -v /opt/nginx:/etc/nginx nginx

Nginx 访问认证

使用 htpasswd 创建用户名密码文件:

apt install apache2-utils    # 提供 htpasswd
# 或
yum -y install httpd-tools

htpasswd -c /etc/nginx/.passwd username    # 创建文件并添加用户
htpasswd -b /etc/nginx/.passwd username2 password  # 追加用户
htpasswd -D /etc/nginx/.passwd username    # 删除用户

Nginx 配置:

location / {
    auth_basic "Please input password";
    auth_basic_user_file /etc/nginx/.passwd;
}

htpasswd 加密算法选项

选项算法
-mMD5(默认)
-dCRYPT
-sSHA
-p明文(不加密)
-Bbcrypt