Example code for converting http to https using nginx

Example code for converting http to https using nginx

I am writing a small program recently. Because the official website of the small program must use https, the website used to use http and used Alibaba Cloud service, so I purchased ssl service on Alibaba Cloud. The following is the configuration process.

1. First, go to Alibaba Cloud to purchase SSL. Of course, there is a free version, but it only supports one domain name, and you can only purchase 20 certificates for the same domain name. Each detailed subdomain is counted as one domain name.

2. After purchasing the SSL certificate, go to the certificate console. At this time, you need to complete the information and wait for review. It usually only takes a few minutes to pass.

3. After the review, you need to download the certificate

4. Then you need to configure the key and pem on the server. Of course, we choose to automatically generate the key here. If necessary, you can make the key yourself and follow the steps on Alibaba Cloud. As shown below:

 server {
  listen 443;
  server_name localhost;
  ssl on;
  root html;
  index index.html index.htm;
  ssl_certificate cert/21.pem;
  ssl_certificate_key cert/21.key;
  ssl_session_timeout 5m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  location / {
   root html;
   index index.html index.htm;
  }
 }

Don't rush to restart nginx at this time. First, check whether nginx has added the ssl module. If not, you need to recompile it. First, execute sudo apt-get install openssl libssl-dev to install ssl, and then enter the nginx directory and execute the following statement:

./configure \
 --prefix=/usr/local/nginx \
 --pid-path=/var/run/nginx/nginx.pid \
 --lock-path=/var/lock/nginx.lock \
 --error-log-path=/var/log/nginx/error.log \
 --http-log-path=/var/log/nginx/access.log \
 --with-http_gzip_static_module \
 --http-client-body-temp-path=/var/temp/nginx/client \
 --http-proxy-temp-path=/var/temp/nginx/proxy \
 --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
 --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
 --http-scgi-temp-path=/var/temp/nginx/scgi \
 --add-module=/home/scp/fastdfs-nginx-module/src \
 --with-http_stub_status_module \
 --with-http_ssl_module

After executing the statement, execute make and then make install. At this time, if you restart nginx, the external network may still be inaccessible, because the Alibaba Cloud port needs to be configured by ourselves. You need to go to the security group to add filtering for port 443. I am using the Ubuntu system here, and you need to execute ufw allow 443 in the terminal. Then you can access the call. Then you need to jump the access of port 80 to 443

server {
listen 80;
server_name www.domain.com;
rewrite ^(.*) https://$server_name$1 permanent;
}

This is the end of this article about the sample code for using nginx to convert http to https. For more information about nginx http to https conversion, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example code for using Nginx to implement 301 redirect to https root domain name
  • Detailed explanation of how to enable HSTS in nginx to force the browser to redirect to HTTPS access
  • How to redirect to https through nginx load balancing
  • Detailed explanation of the solution for NGINX to jump from https to http
  • PHP programmers play Linux series Nginx HTTPS detailed explanation
  • How to force nginx to use https access (http jumps to https)

<<:  js to achieve simple drag effect

>>:  Mysql transaction concurrency problem solution

Recommend

How to decompress multiple files using the unzip command in Linux

Solution to the problem that there is no unzip co...

Causes and solutions for MySQL too many connections error

Table of contents Brief summary At noon today, th...

Understanding MySQL precompilation in one article

1. Benefits of precompilation We have all used th...

How to position the header at the top using CSS sticky layout

Application scenarios: One of the new requirement...

Modify the boot time of grub in ubuntu

The online search to modify the grub startup time...

HTML Grammar Encyclopedia_HTML Language Grammar Encyclopedia (Must Read)

Volume Label, Property Name, Description 002 <...

JavaScript to achieve digital clock effects

This article example shares the specific code for...

How to use the WeChat Mini Program lottery component

It is provided in the form of WeChat components. ...

Linux loading vmlinux debugging

Loading kernel symbols using gdb arm-eabi-gdb out...

MySQL users and permissions and examples of how to crack the root password

MySQL Users and Privileges In MySQL, there is a d...

Write a formal blog using XHTML CSS

The full name of Blog should be Web log, which me...

Detailed explanation of MySQL persistent statistics

1. The significance of persistent statistical inf...

Detailed explanation of CSS3 Flex elastic layout example code

1. Basic Concepts //Any container can be specifie...