Nginx enforces https and redirects HTTP 301 to HTTPS address

Source: Internet
Author: User
Tags curl web services

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.