Nginx server https configuration method example

Nginx server https configuration method example

Linux: Linux version 3.10.0-123.9.3.el7.x86_64

Nginx: nginx/1.6.3

openssl:1.0.1e

Apply for a certificate

Currently, there are many organizations on the Internet that provide free personal SSL certificates, with validity periods ranging from a few months to a few years. Taking StartSSL: https://www.startssl.com as an example, the validity period is 3 years after a successful application, and it can be renewed for free after expiration.

The specific application process is also very simple.

After registering and logging in, select Certificates Wizard >> DV SSL Certificate to apply for a free SSL certificate.

After verifying the domain name via email, generate the CSR of the SSL certificate on your own server. Remember the secret you entered , which you will need later:

openssl req -newkey rsa:2048 -keyout weizhimiao.cn.key -out weizhimiao.cn.csr

Put the generated certificate into the specified directory, such as /data/secret/ . Check the contents of the certificate weizhimiao.csr , copy the contents to the Certificate Signing Request (CSR) section on the page, and submit the page.

Download the generated certificate and select the corresponding web server (Nginx, 1_weizhimiao.cn_bundle.crt), so that we have both the private key and the public key.

  • 1_weizhimiao.cn_bundle.crt (public key)
  • weizhimiao.cn.key (private key)

nginx configuration (add https to the specified domain name)

Current configuration of nginx.conf

...
http {
 ...
 include /etc/nginx/conf.d/*.conf;

 server {
  ...
 }
}

Add to ./conf.d/weizhimiao.cn.conf

server{
 listen 443 ssl;
 server_name weizhimiao.cn;

 ssl_certificate /data/secret/1_weizhimiao.cn_bundle.crt;
 ssl_certificate_key /data/secret/weizhimiao.cn.key;
 ssl_prefer_server_ciphers on;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

 ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED';

 add_header Strict-Transport-Security 'max-age=31536000; preload';
 add_header X-Frame-Options DENY;
 ssl_session_cache shared:SSL:10m;
 ssl_session_timeout 10m;
 keepalive_timeout 70;
 ssl_dhparam /data/secret/dhparam.pem;

 add_header X-Content-Type-Options nosniff;

 add_header X-Xss-Protection 1;

 root /data/www/weizhimiao.cn;
 index index.html;

 location / {

 }
}

Note:

The configuration uses a /data/secret/dhparam.pem file, which is a key file in PEM format and is used in TLS sessions. Used to enhance the security of SSL. Generate this file method,

cd /data/secret/
openssl dhparam 2048 -out dhparam.pem

Redirect the original access to port 80. Add to ./conf.d/weizhimiao.cn.conf

server{
 listen 80;
 server_name weizhimiao.cn;
 return 301 https://weizhimiao.cn$request_uri;
}

test

Check whether there are any syntax errors in the configuration file. You need to enter the password you entered when generating the public key.

nginx -t
Enter PEM pass phrase:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx (remember, reload does not work)

nginx -s stop
Enter PEM pass phrase:
nginx
Enter PEM pass phrase:

Use the browser to access weizhimiao.cn and check whether it is effective.

In addition, after Nginx is configured with a security certificate, you need to enter a password for each reload, stop, and other operations on Nginx.

You can generate a decrypted key file to replace the original key file.

cd /data/secret/
openssl rsa -in weizhimiao.cn.key -out weizhimiao.cn.key.unsecure

Replace the weizhimiao.cn.key file in weizhimiao.cn.conf .

server {
 ...
 ssl_certificate /data/secret/1_weizhimiao.cn_bundle.crt;
 ssl_certificate_key /data/secret/weizhimiao.cn.key.unsecure;
 ...
}

After that, you don't need to enter the password every time you reload.

Finally, use SSLLABS to test it.

result

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:
  • Implementation of HTTP and HTTPS services with Nginx reverse proxy for multiple domain names
  • Two ways to implement nginx https reverse proxy tomcat
  • Detailed explanation of the correct way to redirect nginx server http to https
  • Nginx build https server tutorial
  • Detailed explanation of configuring HTTPS (NGINX) on Alibaba Cloud LINUX server
  • How to build HTTPS server with Nginx and force HTTPS access
  • Problems with configuring https server and reverse proxy with Nginx under Windows

<<:  How to set the number of mysql connections (Too many connections)

>>:  JavaScript to achieve accordion effect

Recommend

Sharing the structure and expression principles of simple web page layout

Introduction to structure and performance HTML st...

mysql add, delete, modify and query basic statements

grammar Here is the generic SQL syntax for INSERT...

HTML form and the use of form internal tags

Copy code The code is as follows: <html> &l...

How webpack implements static resource caching

Table of contents introduction Distinguish betwee...

JS Object constructor Object.freeze

Table of contents Overview Example 1) Freeze Obje...

Example of how to modify styles via CSS variables

question How to modify CSS pseudo-class style wit...

Solve the Docker x509 insecure registry problem

After installing Docker, I encountered the x509 p...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

Research on the value of position attribute in CSS (summary)

The CSS position attribute specifies the element&...

Why do we need Map when we already have Object in JavaScript?

Table of contents 1. Don’t treat objects as Maps ...

How to use React slots

Table of contents need Core Idea Two ways to impl...

An in-depth introduction to React refs

1. What is Refs is called Resilient File System (...