https nginx步骤

一、生成自签名证书(5 步完成)

步骤 1:安装 OpenSSL(Windows)

https://slproweb.com/products/Win32OpenSSL.html

如果不行就下载这个试试

下载 OpenSSL 轻量版:Win32 OpenSSL 下载(选「Win64 OpenSSL Light」,安装时选 "Copy OpenSSL DLLs to: The OpenSSL binaries (/bin) directory")。

步骤 2:打开命令提示符(管理员)

按下 Win+R → 输入 cmd → 右键选择「以管理员身份运行」。

步骤 3:创建证书存放目录 生成私钥(key 文件)

在 Nginx 目录下建 ssl 文件夹(比如 Nginx 装在 D:\nginx):

进入ssl文件夹 然后cmd

输入以下

openssl genrsa -out localhost.key 2048

D:\nginx\ssl 会出现 localhost.key(这是私钥,别泄露)。

openssl req -new -x509 -key localhost.key -out localhost.crt -days 365

然后按下面的填写

Country Name (2 letter code) [XX]:CN

State or Province Name (full name) []:Beijing

Locality Name (eg, city) [Default City]:Beijing

Organization Name (eg, company) [Default Company Ltd]:LocalTest

Organizational Unit Name (eg, section) []:Test

Common Name (eg, your name or your server's hostname) []:localhost # 必须填 localhost

Email Address []:test@local.com

ssl文件夹会出现两个证书

生成自签名证书(crt 文件)

二、配置 Nginx 启用 HTTPS

步骤 1:打开 Nginx 配置文件

找到 Nginx 目录下的 conf\nginx.conf,用记事本 / VS Code 打开。

步骤 2:添加 HTTPS 配置

在 http { ... } 块内添加以下内容(替换为你的 Nginx 路径):

nginx

server {

listen 443 ssl; # 监听 HTTPS 默认端口 443

server_name localhost; # 对应证书的 Common Name

证书路径(填你生成的文件路径)

ssl_certificate D:/nginx/ssl/localhost.crt;

ssl_certificate_key D:/nginx/ssl/localhost.key;

网站根目录(填你的本地项目路径,比如 Nginx 默认的 html 文件夹)

root D:/nginx/html;

index index.html index.html;

}

可选:HTTP 自动跳转到 HTTPS(访问 http://localhost 会跳 https)

server {

listen 80;

server_name localhost;

return 301 https://hostrequest_uri;

}

三、启动 / 重启 Nginx

步骤 1:检查配置是否正确

Nginx 安装根目录 下(该目录包含nginx.execonf文件夹)cmd,

执行nginx -t

若显示 nginx: configuration file D:\nginx/conf/nginx.conf test is successful,说明配置正确。

步骤 2:启动 / 重启 Nginx

cmd

启动(首次)

start nginx

重启(修改配置后)

nginx -s reload

四、测试效果

打开浏览器,访问 https://localhost → 浏览器提示 "不安全"(自签名证书正常现象)→ 点击 "高级"→"继续访问",就能看到 Nginx 页面,说明 HTTPS 生效!

相关推荐
路溪非溪1 小时前
网络运输层:TCP协议详解(一)
网络·网络协议·tcp/ip
困惑阿三2 小时前
Nginx 域名绑定与 HTTPS 配置指南
运维·nginx·https·ssl
凯勒姆3 小时前
主流网络协议
网络·网络协议
郝学胜-神的一滴3 小时前
Linux 高并发基石:epoll 核心原理 + LT/ET 触发模式深度剖析
linux·运维·服务器·开发语言·c++·网络协议
treesforest4 小时前
Ipdatacloud IP 地址查询方案适合哪些场景?
大数据·网络·数据库·网络协议·tcp/ip·ip
追兮兮4 小时前
基于 GD32 与 LwIP 的 TCP OTA 固件升级实现
网络·网络协议·tcp/ip·tcp·gd32·ota
Hello_Embed4 小时前
嵌入式上位机开发入门(二十八):JSON 与 JsonRPC 入门
网络·笔记·网络协议·tcp/ip·嵌入式
东北甜妹5 小时前
TCP/IP和VLAN
网络协议·tcp/ip·面试
deepdata_cn5 小时前
开放智能体网络协议(Agent Network Protocol,ANP)
网络协议·智能体
星辰_mya6 小时前
RPC 原理:Dubbo为了偷懒而存在的中间商
后端·网络协议·rpc·架构·dubbo