I. Prerequisites
This method is only applicable to Nginx WEB services. We recommend that you install army brother LNMP or OneinStack. You can also compile and install Nginx by yourself.
II. Modify the host configuration file
In the/usr/local/nginx/conf/vhost/youdomain.com. conf file of LNMP or OneinStack, add the following configurations to the file.
Server
{
Listen 80;
Server_name your domain name. com www. Your domain name. com;
Rewrite ^ (. *) https: // www. Your domain name. com $1 permanent;
}
The above configuration means that when we use HTTP to request your domain name. com or www. your domain name. all 301 redirection to https: // www. your domain name. com. The following is a complete configuration file for your reference:
Server {
Listen 443;
Ssl on;
Ssl_certificate/data/ssl/your domain name. com. crt;
Ssl_certificate_key/data/ssl/your domain name. com. key;
Server_name your domain name. com www. Your domain name. com;
Index index.html index.htm index. php;
...
...
}
Server
{
Listen 80;
Server_name your domain name. com www. Your domain name. com;
Rewrite ^ (. *) https: // www. Your domain name. com $1 permanent;
}
Do not forget to reload Nginx: service nginx reload after configuration modification
III. CURL testing
Finally, we can test whether HTTP access will Redirect 301 to HTTPS. You can use CURL to test: curl-I www. Your domain name. com