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

js dynamically generates tables (node ​​operations)

This article example shares the specific code of ...

Example sharing of anchor tag usage in HTML

Anchor tag usage: Linking to a specific location i...

20 JS abbreviation skills to improve work efficiency

Table of contents When declaring multiple variabl...

Solution for Vue routing this.route.push jump page not refreshing

Vue routing this.route.push jump page does not re...

Implementing CommonJS modularity in browsers without compilation/server

Table of contents introduction 1. What is one-cli...

How to build a multi-node Elastic stack cluster on RHEL8 /CentOS8

Elastic stack, commonly known as ELK stack, is a ...

How to deploy the crownblog project to Alibaba Cloud using docker

Front-end project packaging Find .env.production ...

How to view and modify the time zone in MySQL

Today I found that a program inserted an incorrec...

Data storage implementation method in WeChat applet

Table of contents Global variable globalData Page...

MySQL database deletes duplicate data and only retains one method instance

1. Problem introduction Assume a scenario where a...