• Nginx实现本地http转https请求


    目录

    前言:

    一、安装nginx

     二、安装OpenSSL

             1、下载OpenSSL:

             2、配置环境变量:

                      2.1:配置环境变量,OpenSSL_HOME 

                      2.2:配置path

     三、生成https证书

               1、创建ssl文件夹用于存放证书。创建私钥 (建议使用系统窗口,不要用gitBash 有涉及到选择的地方,gitBash无法选择)

               2、文件夹中生成shidian.key文件 创建csr证书。

               3、复制 shidian.key 并重命名 shidian.key.org 执行命令

               4、生成crt证书

     四、修改nginx.conf配置,使用https请求


    前言:

    项目中有需要做接口回调,测试和dev环境都是https方式请求调用没有问题,但是本地调试不通。因为本地启动后端服务都是http请求,所以使用nginx代理,将http请求转换为https请求,方便调试。

     

    一、安装nginx

    Nginx官网:nginx: download

    安装到非中文目录下:

     启动Nginx:点击nginx.exe文件,启动nginx

    然后访问:

    访问成功,说明安装nginx成功

     二、安装OpenSSL

    OpenSSL官网:Win32/Win64 OpenSSL Installer for Windows - Shining Light Productions

             1、下载OpenSSL:

     放到一个非中文的目录下去:

             2、配置环境变量:

                      2.1:配置环境变量,OpenSSL_HOME 

                      2.2:配置path

     

     三、生成https证书

    首先在Nginx得文件夹中新建一个ssl文件夹:

     

               1、创建ssl文件夹用于存放证书。创建私钥 (建议使用系统窗口,不要用gitBash 有涉及到选择的地方,gitBash无法选择)

    openssl genrsa -des3 -out shidian.key 1024            //shidian 自己取的名字

    可以输入pass作为初始的密码,两次一致。

               2、文件夹中生成shidian.key文件 创建csr证书。

    openssl req -new -key shidian.key -out shidian.csr

    在这里插入图片描述 

     此时应该有两个文件:

     

               3、复制 shidian.key 并重命名 shidian.key.org 执行命令

    openssl rsa -in shidian.key.org -out shidian.key

               4、生成crt证书

    openssl x509 -req -days 365 -in shidian.csr -signkey shidian.key -out shidian.crt

    最终的文件夹里的内容为:

     

     四、修改nginx.conf配置,使用https请求

    整个nginx.conf文件配置:


    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;


    events {
        worker_connections  1024;
    }


    http {
        include       mime.types;
        default_type  application/octet-stream;

        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';

        #access_log  logs/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;


         server {
           listen       80;
           server_name  localhost;

           #charset koi8-r;

           #access_log  logs/host.access.log  main;

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

           #error_page  404              /404.html;

            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }

            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}

            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }


        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

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


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

            # ssl_certificate      cert.pem;
            # ssl_certificate_key  cert.key;
        ssl_certificate      ..//ssl//shidian.crt;
            ssl_certificate_key  ..//ssl//shidian.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;
            }
        }
    }

     

     关于ssl配置

        server {
            listen       443 ssl;
            server_name  localhost;

            # ssl_certificate      cert.pem;
            # ssl_certificate_key  cert.key;
            ssl_certificate      ..//ssl//shidian.crt;     指的是ssl证书
            ssl_certificate_key  ..//ssl//shidian.key; 指的是ssl的密钥

            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请求访问:

    给nginx配置https请求成功!

     

     

  • 相关阅读:
    Docker01基础操作
    Leetcode 2862. Maximum Element-Sum of a Complete Subset of Indices
    修改angular cli 的默认包管理器
    基于SSH开发学生公寓管理系统
    2.2 机器学习流程
    Linux之xdotool工具安装及实践
    Python笔记五之正则表达式
    STC 51单片机47——外部中断控制流水灯
    Docker安装Jenkins
    架火炬市场现状及未来发展趋势分析
  • 原文地址:https://blog.csdn.net/m0_53151031/article/details/125497917