win7 localhost 配置 nginx ssl自签名证书

本文详细介绍如何下载并安装openssl,通过openssl生成自签名SSL证书,并将其配置到Nginx服务器中,包括生成证书脚本、Nginx配置示例及常见错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

步骤

1)下载并安装openssl
下载windows版本的openssl解压缩到本地。
下载地址:http://gnuwin32.sourceforge.net/packages/openssl.htm
这里选择的是:Binaries (*.zip)

2)生成ssl证书

进入目录,运行openssl.exe,输入如下生成脚本,假设文件生成到d:/ssl目录

OpenSSL> req -x509 -out d:/ssl/localhost.crt -keyout d:/ssl/localhost.key -newke
y rsa:2048 -nodes -sha256 -subj '/CN=localhost' -extensions EXT -config d:/ssl/c
onfig.txt -days 2048
Loading 'screen' into random state - done
Generating a 2048 bit RSA private key
.............................................+++
.......................................................................+++
writing new private key to 'd:/ssl/localhost.key'
-----
OpenSSL>

其中 d:/ssl/config.txt 内如如下:

[dn]
CN=localhost

[req]
distinguished_name = dn

[EXT]
subjectAltName=DNS:localhost
keyUsage=digitalSignature
extendedKeyUsage=serverAuth

支持多个域名的配置:
例子1:http://apetec.com/support/generatesan-csr.htm
例子2:https://medium.com/@pubudu538/how-to-create-a-self-signed-ssl-certificate-for-multiple-domains-25284c91142b

3)更新到nginx的配置localhost.conf

server {
	listen 80;
	listen 443;
	server_name localhost;
	root d:/localhost;
	index index.html index.htm index.php;
	autoindex on;
    autoindex_localtime on;
	
	ssl on;
	ssl_certificate     d:/ssl/localhost.crt;
	ssl_certificate_key d:/ssl/localhost.key;
	
	location ~ \.php$ {
	    fastcgi_pass   php_pool;
	    fastcgi_index  index.php;
	    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	    include        fastcgi_params;
	}
}

再重启nginx,访问 https://localhost

报错

localhost 使用了无效的安全证书。 
该证书因为其自签名而不被信任。
错误代码:MOZILLA_PKIX_ERROR_SELF_SIGNED_CERT

解决方法:~ 暂无,网上的都是治标不治本的,修改浏览器设置的后果无法解决如下报错。

D:\>php -r "echo file_get_contents('http://localhost/robots.txt');"
Allow: *
D:\>php -r "echo file_get_contents('https://localhost/robots.txt');"

Warning: file_get_contents(): SSL operation failed with code 1. OpenSSL Error me
ssages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify faile
d in Command line code on line 1

Call Stack:
    0.0020     350368   1. {main}() Command line code:0
    0.0020     350368   2. file_get_contents() Command line code:1


Warning: file_get_contents(): Failed to enable crypto in Command line code on li
ne 1

Call Stack:
    0.0020     350368   1. {main}() Command line code:0
    0.0020     350368   2. file_get_contents() Command line code:1


Warning: file_get_contents(https://localhost/robots.txt): failed to open stream:
 operation failed in Command line code on line 1

Call Stack:
    0.0020     350368   1. {main}() Command line code:0
    0.0020     350368   2. file_get_contents() Command line code:1


D:\>

相关链接

Let’s Encrypt: Certificates for localhost
https://letsencrypt.org/docs/certificates-for-localhost/

How To Create a Self-Signed SSL Certificate for Nginx in Ubuntu 16.04
https://www.digitalocean.com/community/tutorials/how-to-create-a-self-signed-ssl-certificate-for-nginx-in-ubuntu-16-04

### 配置Nginx使用自签名SSL证书 在Nginx配置自签名SSL证书的过程主要包括以下几个方面:生成自签名证书、安装Nginx并确保其支持SSL模块、配置Nginx以加载证书和私钥,以及测试HTTPS连接。以下是详细的说明: #### 1. 确保Nginx支持SSL模块 在配置自签名SSL证书之前,必须确认Nginx编译时启用了`--with-http_ssl_module`模块。可以通过以下命令检查Nginx的编译参数: ```bash nginx -V ``` 如果输出中包含`--with-http_ssl_module`,则表示Nginx支持SSL功能[^5]。 #### 2. 生成自签名SSL证书 可以使用OpenSSL工具生成自签名SSL证书及其对应的私钥。运行以下命令: ```bash openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout server.key -out server.crt ``` 上述命令将生成一个有效期为365天的自签名证书(`server.crt`)和私钥文件(`server.key`)。在执行过程中,需要填写一些信息,如国家代码、组织名称等[^2]。 #### 3. 安装Nginx并启动服务 在CentOS 7上安装Nginx并启动服务,可以运行以下命令: ```bash yum install epel-release -y yum install nginx -y systemctl start nginx ``` 这将安装Nginx并启动服务[^3]。 #### 4. 配置Nginx以加载SSL证书 编辑Nginx的主配置文件(通常位于`/etc/nginx/nginx.conf`或`/etc/nginx/sites-available/default`),添加或修改以下内容: ```nginx server { listen 443 ssl; server_name localhost; ssl_certificate /path/to/server.crt; # 替换为实际的证书路径 ssl_certificate_key /path/to/server.key; # 替换为实际的私钥路径 location / { root /usr/share/nginx/html; index index.html index.htm; } } ``` 确保将`ssl_certificate`和`ssl_certificate_key`的路径替换为实际生成的证书和私钥文件的位置[^4]。 #### 5. 测试配置并重启Nginx 保存配置文件后,测试Nginx配置是否正确: ```bash nginx -t ``` 如果没有错误,重启Nginx服务以应用更改: ```bash systemctl restart nginx ``` #### 6. 访问HTTPS页面 打开浏览器并访问`https://<服务器IP地址>`,应该能够看到由自签名证书保护的HTTPS页面。由于是自签名证书,浏览器可能会显示安全警告,但可以忽略并继续访问[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值