Nginx HTTPS configuration tutorial to protect website data transmission security

PHPz
Release: 2023-07-04 13:43:42
Original
1575 people have browsed it

Nginx HTTPS configuration tutorial to protect website data transmission security

With the rapid development of the Internet, website security issues have received increasing attention. In order to protect the security of website data transmission, the use of HTTPS protocol is a very important measure. This article will introduce how to use Nginx to configure HTTPS to ensure the security of data transmission on the website.

1. Install SSL certificate
Before configuring HTTPS, we need to obtain an SSL certificate to ensure the identity of the website and the security of data transmission. You can purchase a certificate from a third-party certificate authority (CA), or use a free open source certificate generation tool such as Let's Encrypt.

The steps to install the certificate are as follows:

  1. Download the certificate: Download the certificate file (including public key, private key and certificate chain) to the server. Typically, certificate files have the extensions .crt and .key.
  2. Create SSL storage file: Use the openssl command to merge the .crt and .key files into a .pem format file:
    openssl rsa -in privateKey.key -text > privateKey.pem
    openssl x509 -inform PEM -in certificate.crt > certificate.pem
    cat privateKey.pem certificate.pem > ssl.crt

2. Nginx configuration HTTPS

  1. Open the Nginx configuration file: usually located at /etc/nginx/nginx.conf or /usr/local/nginx/conf/nginx.conf.
  2. Add HTTPS service block: Within the http block, add the following configuration:
    server {

    listen 443 ssl;
    server_name yourdomain.com;
    ssl_certificate /path/to/ssl.crt;
    ssl_certificate_key /path/to/privateKey.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ......
    Copy after login

    }

    • listen 443 ssl: listening Default port 443 for HTTPS protocol, with SSL enabled.
    • server_name: Replace with your domain name.
    • ssl_certificate: Specify the path to the SSL certificate.
    • ssl_certificate_key: Specify the path to the SSL private key.
    • ssl_protocols: Specify the supported SSL/TLS protocol version.
    • ssl_ciphers: Specify supported encryption algorithms.
  3. Configure HTTP to HTTPS redirection: Within the http block, add the following configuration:
    server {

    listen 80;
    server_name yourdomain.com;
    return 301 https://$server_name$request_uri;
    Copy after login

    }

    When a user accesses an HTTP URL, Nginx will automatically redirect them to an HTTPS URL.

  4. Save and reload the configuration: Save the configuration file and execute the following command to restart the Nginx service:
    sudo service nginx restart

At this point, you have successfully configured Nginx HTTPS service.

3. Optimize HTTPS configuration
In order to further improve the security and performance of the website, you can take the following optimization measures:

  1. Enable HTTP/2 protocol: use Nginx HTTP /2 module upgrades the HTTPS protocol to HTTP/2 to improve the loading speed and performance of the website.
    Add in the server block:
    listen 443 ssl http2;
  2. Enable OCSP Stapling: OCSP Stapling is a technology that improves the speed and security of SSL verification. Add in the server block:
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;
  3. Configure HTTP Strict Transport Security ( HSTS): HSTS can force all HTTP requests to be redirected to HTTPS and prevent man-in-the-middle attacks.
    Add in the server block:
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";

4. Common problems and solutions in HTTPS configuration
When configuring HTTPS, you may encounter some common problems. The following are some common problems and their solutions:

  1. Configuration file error: Check whether the Nginx configuration file is correct, especially whether the paths to ssl_certificate and ssl_certificate_key are correct.
  2. Certificate Error: Make sure your SSL certificate is valid and matches the domain name. The validity of the certificate can be verified in the browser.
  3. Firewall issues: If you use a firewall, make sure port 443 (HTTPS protocol) is open.
  4. SSL/TLS protocol issue: Some clients may not support older versions of the SSL/TLS protocol. Keeping only TLSv1.2 in ssl_protocols can solve this problem.

Conclusion
By configuring the HTTPS protocol with Nginx, we can provide a more secure data transmission channel for the website. This article introduces how to install an SSL certificate and configure Nginx's HTTPS service, and provides some optimization configurations and solutions to common problems. I hope this article will be helpful to you and make your website data transmission more secure and reliable.

The above is the detailed content of Nginx HTTPS configuration tutorial to protect website data transmission security. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template