使用 OpenSSL 和 Nginx 配置自签名证书以启用 HTTPS

原因:服务器做测试http协议无法打开摄像头麦克风
解决:使用自签名证书,升级协议为https

安装 OpenSSL

Ubuntu

sudo apt update
sudo apt install openssl

生成 RSA 私钥

openssl genpkey -algorithm RSA -out /www/wwwroot/ssl/private_key.pem

生成证书签名请求(CSR)

openssl req -new -key /www/wwwroot/ssl/private_key.pem -out /www/wwwroot/ssl/request.csr

按需输入例如:

Country Name (2 letter code) [AU]: CN
State or Province Name (full name) [Some-State]: Beijing
Locality Name (e.g., city) []: Beijing
Organization Name (e.g., company) [Internet Widgits Pty Ltd]: My Company
Organizational Unit Name (e.g., section) []: IT Department
Common Name (e.g., server FQDN or YOUR name) []: example.com
Email Address []: admin@example.com

输入 A challenge password []:自己的密码
确认输入

生成自签名证书

openssl req -x509 -key /www/wwwroot/ssl/private_key.pem -in /www/wwwroot/ssl/request.csr -out /www/wwwroot/ssl/certificate.pem

配置 Nginx 使用 SSL

server {
    listen 443 ssl;
    server_name yourdomain.com; # 替换为你的域名

    ssl_certificate /www/wwwroot/ssl/certificate.pem;
    ssl_certificate_key /www/wwwroot/ssl/private_key.pem;

    # 其他 SSL 配置(可选)
    ssl_protocols TLSv1.2 TLSv1.3; # 使用支持的 TLS 版本
    ssl_ciphers HIGH:!aNULL:!MD5; # 设置加密套件
    ssl_prefer_server_ciphers on;

    # 网站根目录和其他配置
    root /var/www/html;
    index index.html index.htm;

    # 配置虚拟主机的其他部分,例如 location 块
    location / {
        try_files $uri $uri/ =404;
    }
}

# 可选:处理 HTTP 到 HTTPS 的重定向
server {
    listen 80;
    server_name yourdomain.com; # 替换为你的域名

    return 301 https://$host$request_uri;
}

重启

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值