美文网首页
Windows 下 Nginx 配置 SSL 实现 Https

Windows 下 Nginx 配置 SSL 实现 Https

作者: guanguans | 来源:发表于2018-09-20 16:43 被阅读66次

    说明:此教程适合已经配置好 WNMP 环境。

    实现 Https 首先需要向管理机构申请证书,而我们此次由于是练习目的,所以通过 Openssl 自己生成证书。首先我们需要用到生成证书的 Openssl 软件。

    1. 安装 Openssl

    下载地址:http://slproweb.com/products/Win32OpenSSL.html (根据系统选择 32 位或者 64 位版本下载安装)。

    下载完成后,进行安装,我安装在了 C:\wnmp\OpenSSL-Win64 文件夹中。

    2. 安装 ActivePerl (此软件目的为了解析 pl 文件,部分系统不安装也可以实现本教程的功能,安装该软件目的为了学习 perl)。

    下载地址:http://www.activestate.com/activeperl/downloads/ (根据系统选择 win32 或者 win64 版本下载安装)。

    3. 配置环境变量

    在环境变量中添加环境变量
     
    在 path 变量结尾添加如下 :C:\wnmp\OpenSSL-Win64\bin;

    4 生成证书

    (1) 首先在 nginx 安装目录中创建 ssl 文件夹用于存放证书。比如我的文件目录为 C:\wnmp\nginx\ssl 以管理员身份进入命令行模式,进入 ssl 文件夹。 命令为: cd c:/wnmp/nginx/ssl

    (2) 创建私钥

    在命令行中执行命令: openssl genrsa -des3 -out lee.key 1024 (lee 文件名可以自定义),如下图所示:

    image

    输入密码后,再次重复输入确认密码。记住此密码,后面会用到。

    (3)创建 csr 证书

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

    image

    如上图所示,执行上述命令后,需要输入信息。输入的信息中最重要的为 Common Name,这里输入的域名即为我们要使用 https 访问的域名。

    以上步骤完成后,ssl 文件夹内出现两个文件: image

    (4)去除密码。

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

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

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

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

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

    image

    (5)生成 crt 证书

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

    image

    证书生成完毕,ssl 文件夹中一共生成如下 4 个文件,我们需要使用到的是 lee.crt 和 lee.key。

    image

    5. 修改 nginx.conf 文件

    nginx.conf 文件位于:C:\wnmp\nginx\conf

    找到该文件中如下代码的位置进行修改:

        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;
    
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;
    
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    

    修改为:

    # HTTPS server
    #
    #modify by lee 20160907 for https -s 
        server {
            listen       443 ssl;
            server_name    www.lee.com;
    
            ssl_certificate      C:/wnmp/nginx/ssl/lee.crt;
            ssl_certificate_key  C:/wnmp/nginx/ssl/lee.key;
    
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
    
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
    
            location / {
                root   C:/wnmp/lee;
                index  index.html index.htm index.php;
            }
    
                   root           C:/wnmp/lee;
                   fastcgi_pass   127.0.0.1:9001;
                   fastcgi_index  index.php;
                   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                   include        fastcgi_params;
            }
        }
    #modify by lee 20160907 for https -s 
    

    重启 nginx。

    在浏览器中,访问 https://www.lee.com。发现出现证书认证,并能够成功访问。(www.lee.com 为生成证书时,Common Name 输入的域名)

    (执行此步骤时,需要配置好 Virtual Host,并且在 www.lee.com 开放目录中添加了 index.php 默认入口访问文件。)

    image

    上面的 https 被红色划线是因为我们使用的是自己生成的证书,此证书不受浏览器信任,如果想使其变为绿色,则需要向证书管理机构进行申请。

    6. 添加重定向,自动跳转使用 https。

    在 nginx.conf 中 virtual host 中如下代码位置添加一行代码:

    listen       80;                    
        server_name   www.lee.com;                    
    #modify by lee 20160907 for https Redirect -s                        
        rewrite ^(.*) https://$server_name$1 permanent;                    
    #modify by lee 20160907 for https Redirect -e
    

    重启 nginx。

    访问 www.lee.com,会发现浏览器自动跳转到 https://www.lee.com,并能够成功访问。

    至此,https 访问配置成功完成。

    附加配置

    server {
        listen       80;
        # http 自动转发给 https
        rewrite ^(.*) https://$server_name$1 permanent;
        ssl_certificate      D:/phpStudy/PHPTutorial/nginx/ssl/aijia.crt;
        ssl_certificate_key  D:/phpStudy/PHPTutorial/nginx/ssl/aijia.key;
        server_name  aijia.test ;
        root   "D:\phpStudy\PHPTutorial\WWW\aijia";
        location / {
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite  ^/(.*)$ /index.php?s=$1  last;
                break;
            }         
            #autoindex  on;
        }
        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;
        }
    }
    
    server {
        listen       443 ssl;
        ssl_certificate      D:/phpStudy/PHPTutorial/nginx/ssl/aijia.crt;
        ssl_certificate_key  D:/phpStudy/PHPTutorial/nginx/ssl/aijia.key;
        server_name  aijia.test ;
        root   "D:\phpStudy\PHPTutorial\WWW\aijia";
        location / {
            index  index.html index.htm index.php;
            if (!-e $request_filename) {
                rewrite  ^/(.*)$ /index.php?s=$1  last;
                break;
            }         
            #autoindex  on;
        }
        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;
        }
    }
    

    相关文章

      网友评论

          本文标题:Windows 下 Nginx 配置 SSL 实现 Https

          本文链接:https://www.haomeiwen.com/subject/blennftx.html