Sample code for implementing two-way authentication with Nginx+SSL

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory

cd /etc/nginx
mkdir ssl
cd ssl

CA vs Self-signed

Create a CA private key

openssl genrsa -out ca.key 2048

Create a CA root certificate (public key)

openssl req -new -x509 -days 3650 -key ca.key -out ca.crt

Notice:

1. Common Name can be filled in at will
2. To avoid mistakes, please fill in all other required information.

Server Certificate

Make a server-side private key:

openssl genrsa -out server.pem 1024
openssl rsa -in server.pem -out server.key

Generate a signing request:

openssl req -new -key server.pem -out server.csr

Notice:

1. Common Name must be filled in with the domain name when accessing the service. Here we use usb.dev. The following NGINX configuration will use
2. To avoid mistakes, fill in all other required information (to match the CA root certificate)

Issued by CA

openssl x509 -req -sha256 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -days 3650 -out server.crt

Client Certificates

Similar to the server certificate

Notice:

1. Common Name can be filled in at will
2. To avoid mistakes, fill in all other required information (to match the CA root certificate)

Now that the required certificates are in place, we can start configuring NGINX.

Nginx Configuration

server {
    listen 443;
    server_name usb.dev;

    index index.html;

    root /data/test/;

    ssl on;
    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;
    ssl_client_certificate /etc/nginx/ssl/ca.crt;
    ssl_verify_client on;
}

Request Verification

The verification process can be done on another machine or on the local machine. In order to resolve usb.dev, you also need to configure /etc/hosts:

ip address usb.dev

If you use a browser for authentication, you need to export the client certificate into p12 format.

openssl pkcs12 -export -clcerts -in client.crt -inkey client.pem -out client.p12

Download several certificates from the server and install them to the trusted certificate list. Click the p12 file just generated and enter the certificate password to install it to the personal list.
Then close the browser and re-enter the domain name, and a certificate prompt for SSL two-way verification will appear. Just select the certificate.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Example of how to configure nginx to implement SSL
  • Start nginxssl configuration based on docker
  • Solution to Nginx SSL certificate configuration error
  • Nginx domain name SSL certificate configuration (website http upgraded to https)
  • Implementation of Nginx load balancing/SSL configuration
  • How to configure SSL certificate in nginx to implement https service
  • Simple steps to configure Nginx reverse proxy with SSL
  • Detailed explanation of Nginx installation, SSL configuration and common commands under Centos7.x
  • Steps to configure nginx ssl to implement https access (suitable for novices)
  • Create an SSL certificate that can be used in nginx and IIS

<<:  Detailed Introduction to the MySQL Keyword Distinct

>>:  Mysql Sql statement comments

Recommend

Sharing several methods to disable page caching

Today, when developing, I encountered a method wh...

User needs lead to marketing-oriented design

<br />For each of our topics, the team will ...

Introduction to version management tool Rational ClearCase

Rational ClearCase is a software configuration ma...

jquery+springboot realizes file upload function

This article example shares the specific code of ...

Docker container time zone error issue

Table of contents background question Problem ana...

JavaScript to achieve digital clock effect

This article example shares the specific code of ...

Solutions to black screen when installing Ubuntu (3 types)

My computer graphics card is Nvidia graphics card...

ES6 loop and iterable object examples

This article will examine the ES6 for ... of loop...

Detailed explanation of the usage of position attribute in HTML (four types)

The four property values ​​of position are: 1.rel...