windows下nginx支持https

本文详细介绍如何在Nginx服务器上配置SSL证书,包括下载和安装openssl、生成私钥和CSR文件、去除私钥密码、生成CRT证书,以及修改Nginx配置文件等关键步骤。

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

nginx下载地址:http://nginx.org/en/download.html
openssl下载地址:http://slproweb.com/products/Win32OpenSSL.html
官网地址:https://www.openssl.org/source/

我本地环境使用的phpStudy集成环境,可以方便的切换各种版本及apache和nginx、IIS之间切换,个人感觉比WAMP要好用。

  1. 开启php的openssl支持,这个不细说,不会的上网搜下哈。
  2. 配置环境变量
    我的电脑-》属性-》高级系统设置-》环境变量-》用户变量(如果想要所有用户通用的话可以在系统变量里面配置 )

        变量名: OPENSSL_HOME 变量值:C:\OpenSSL-Win64\bin; (变量值为openssl安装位置,我的 )

        在path变量结尾添加如下 : %OPENSSL_HOME%;
    这里写图片描述

  3. 生成证书

    • 在 nginx安装目录中创建ssl文件夹用于存放证书。我的文件目录是 D:\phpStudy\nginx\ssl,运行命令窗口,cmd,进入D:\phpStudy\nginx\ssl中。

    • 创建私钥

        在命令行中执行命令: openssl genrsa -des3 -out lifes.key 1024 (lifes文件名可以自定义),如下图所示:
       
        这里写图片描述
        
        输入密码后,再次重复输入确认密码。记住此密码,后面会用到。
            

    • 创建csr证书

      在命令行中执行命令: openssl req -new -key lifes.key -out lifes.csr
      (key文件为刚才生成的文件,lifes为自定义文件名

      这里写图片描述

      如上图所示,执行上述命令后,需要输入信息。

      这里写图片描述

      以上步骤完成后,ssl文件夹内出现两个文件:lifes.keylifes.csr

    • 去除密码
      在加载SSL支持的Nginx并使用上述私钥时除去必须的口令,否则会在启动nginx的时候需要输入密码。

      复制lifes.key并重命名为lifes.key.org

      可以使用此命令行,也可以使用鼠标操作 copy lifes.key lifes.key.org

      这里写图片描述

      去除口令,在命令行中执行此命令: openssl rsa -in lifes.key.org -out lifes.key (lifes为自定义文件名)

      如下图所示,此命令需要输入刚才设置的密码。

      这里写图片描述

    • 生成crt证书

      在命令行中执行此命令: openssl x509 -req -days 365 -in lifes.csr -signkey lifes.key -out lifes.crt (lifes为自定义文件名)

      这里写图片描述

      证书生成完毕,ssl文件夹中一共生成如下4个文件:lifes.keylifes.csrlifes.key.orglifes.crt ,我们需要使用到的是lifes.crtlifes.key

  4. 修改nginx.conf文件

    找到该文件中如下代码的位置进行修改: 保证本机的端口不被占用 443 和 80,http默认端口80,https默认端口443。

    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

改为:



    server {

        listen       443;
        server_name  www.lifes.com;

        ssl_certificate      D:/phpStudy/nginx/ssl/lifes.crt;
        ssl_certificate_key  D:/phpStudy/nginx/ssl/lifes.key;

        ssl                  on;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

    }

重启nginx之后访问页面如下:
这里写图片描述

没有到项目的默认页面下,需要加上指向的默认路径


        location / {
            root   D:/WWW/Lifes/public;
            index  index.html index.htm index.php;
        }

然而,这样重启之后访问是直接下载的,问题在于在https server{ }中没有解析php的代码块,需要加上如下代码:


        location ~ .*\.(php|php5)?$ {

            root           D:/WWW/Lifes/public/;

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  HTTPS   on;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;

            #new line
            include fastcgi.conf;

        }

完整的代码如下:

    server {

        listen       443;
        server_name  www.lifes.com;

        ssl_certificate      D:/phpStudy/nginx/ssl/lifes.crt;
        ssl_certificate_key  D:/phpStudy/nginx/ssl/lifes.key;

        ssl                  on;

        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

        location / {
            root   D:/WWW/Lifes/public;
            index  index.html index.htm index.php;
        }

        location ~ .*\.(php|php5)?$ {

            root           D:/WWW/Lifes/public/;

            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  HTTPS   on;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;

            #new line
            include fastcgi.conf;

        }

    }

如果需要http:///www.lifes.com也指向https://www.lifes.com的话需要在虚拟主机配置文件vhosts.conf中加入代码:rewrite ^(.*) https://$server_name$1 permanent;重定向到https
如:


server {
        listen       80;
        server_name  www.lifes.com;

        rewrite ^(.*) https://$server_name$1 permanent;

        root   "D:/WWW/Lifes/public";

        index  index.html index.htm index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

Author:leedaning
本文地址:http://blog.youkuaiyun.com/leedaning/article/details/71125559

### 配置 NginxWindows 环境下支持 HTTPS 要在 Windows 系统上使用 Nginx 部署 HTTPS 网站,需要完成以下几个方面的设置: #### 1. **安装并启动 Nginx** 确保已正确安装 Nginx 并能够正常运行。可以通过进入 `C:\nginx` 目录(假设此为安装路径),双击 `nginx.exe` 文件来启动服务[^3]。验证方法是在浏览器中访问 `http://localhost`,如果显示默认的 Nginx 欢迎页面,则表示安装成功。 --- #### 2. **生成 SSL/TLS 证书** 为了启用 HTTPS 协议,必须准备有效的 SSL/TLS 证书及其私钥文件。这些文件通常由受信任的 CA(Certificate Authority)签发,或者也可以自签名用于测试环境。以下是常见的证书存储路径: - 证书文件路径:`D:/nginx/conf/uat.cer` - 私钥文件路径:`D:/nginx/conf/uat.key` 注意:实际路径应根据具体需求调整[^2]。 --- #### 3. **编辑 Nginx 配置文件 (nginx.conf)** 打开位于 Nginx 安装目录下的 `conf/nginx.conf` 文件,在其中定义一个监听 443 端口的服务块,并指定必要的 SSL 参数。以下是一个典型的配置示例: ```nginx server { listen 443 ssl; server_name yourdomain.com; # 启用 SSL 功能 ssl_certificate D:/nginx/conf/yourdomain.crt; # 替换为您的证书路径 ssl_certificate_key D:/nginx/conf/yourdomain.key; # 替换为您的密钥路径 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; # 反向代理设置 location / { proxy_pass http://127.0.0.1:8080; # 将请求转发至本地应用服务器 } # 错误页处理 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } ``` 上述配置实现了以下功能: - 使用 `listen 443 ssl` 来开启 HTTPS 支持。 - 设置了 `ssl_certificate` 和 `ssl_certificate_key` 指定证书与密钥的位置。 - 添加了一个反向代理规则 (`proxy_pass`),将流量导向后台的应用程序实例。 --- #### 4. **保存更改并重启 Nginx** 完成以上修改之后,需重新加载或重启 Nginx 才能使新配置生效。对于 Windows 环境中的 Nginx,执行如下命令即可实现热更新而无需完全停止服务: ```bash nginx.exe -s reload ``` 该操作会通知守护进程平滑切换到新的配置模式而不中断现有连接[^1]。 --- #### 注意事项 - 如果遇到权限不足的情况,请确认当前用户具有足够的权利读取所指明的 `.crt` 和 `.key` 文件。 - 对于生产环境中使用的正式站点而言,建议从权威机构获取合法认证而非自行创建临时凭证。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值