“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

The introduction

There is a need to do interface callback in the project. There is no problem with both the test and dev environment requesting invocation in HTTPS mode, but the local debugging fails. Because the local backend services are HTTP requests, the Nginx proxy is used to convert HTTP requests into HTTPS requests for easy debugging.

Install nginx

Having installed Nginx in your previous employer, I visited the official website for this article.Nginx.org/en/download…) according to your own computer system. My computer is Windows.After downloading, decompress the file and copy it to a folder that does not contain the Chinese name. I used to put the software installation in the software folder.Through the nginx.exe file or through the commandstart nginxStart the nginx. After the startup, visit localhost to verify that the startup is successful.

Install OpenSSL

Visit our official website (Slproweb.com/products/Wi…Download).Install to the corresponding folder.

Configure environment variablesOPENSSL_HOME.

configurationpath

Generating an HTTPS Certificate

Create an SSL folder to store the certificate. Create private key (recommended to use system window, do not use gitBash there is a selection involved, gitBash cannot select)

Key 1024 // The name specified by the shidian userCopy the code

Enter pass as the password.

Shidian. key file is generated in the folder to create the CSR certificate.

openssl req -new -key shidian.key -out shidian.csr
Copy the code

There are two files at this point

copyshidian.keyAnd renameshidian.key.orgExecute the command

openssl rsa -in shidian.key.org -out shidian.key
Copy the code

Generating a CRT Certificate

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

The final folder files are

Modify the nginx.conf configuration

upstream aglacwy {
        server localfop.hanhang.com:11295;
        keepalive 64;
} 
server {
    listen 443 ssl;
    server_name localfop.chehejia.com; # configure HTTPS domain name
    
    ssl_certificate      D: / / software / / nginx - 1.21.4 / / SSL / / shidian CRT;  This is where the CRT file of the certificate resides
    ssl_certificate_key  D: / / software / / nginx - 1.21.4 / / SSL / / shidian. Key;  This is the directory where the certificate key file resides

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:! aNULL:! MD5;
    ssl_prefer_server_ciphers  on;

    location / {
        proxy_set_header   X-Real-IP            $remote_addr;
        proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header   Host                   $http_host;
        proxy_set_header   X-NginX-Proxy    true;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        if ($request_method ~ ^(POST|DELETE|OPTIONS)$) {
            proxy_pass http://aglacwy;
            break ;
    }
        proxy_pass         http://aglacwy;}}Copy the code

Localfop.hanhang.com Change the host domain name for the local COMPUTER. If necessary, set localhost to the desired domain name in the hosts file

Run the nginx -s reload command to restart nginx. Via HTTPS access by localfop.hanhang.com/test/test/n… , through the breakpoint. Response request result.

At this point, the HTTP request has been successfully converted into an HTTPS request.