Nginx enforces HTTPS access (HTTP jumps to HTTPS)

Source: Internet
Author: User
Tags error code root directory
Requirements Introduction

Based on Nginx built an HTTPS access to the virtual host, listening to the domain name is test.com, but many users do not know the difference between HTTPS and HTTP, it will be easy to knock into http://test.com, this time will report 404 errors, so I need to do Forced jump of HTTP to HTTPS based on test.com domain name

I've summed up three ways to share with you



The rewrite method of Nginx


This should be the easiest way for everyone to think about it and rewrite all HTTP requests via rewrite to HTTPS.
Configuration

server {
	listen	192.168.1.111:80;
	server_name	test.com;
	
	Rewrite ^ (. *) $	https://$host $	permanent;
}

Once this virtual host is built, you can rewrite all of the http://test.com's requests to https://test.com.

nginx 497 Status Code


Error code 497

497-normal request was sent to HTTPS

Explanation: When this virtual site allows only HTTPS access, Nginx will report a 497 error code when it is accessed with HTTP
IdeasUse the Error_page command to redirect the link of the 497 status code to the domain name of https://test.com
Configuration
server {
	listen       192.168.1.11:443;	#ssl端口
	Listen       192.168.1.11:80;	#用户习惯用http访问, plus 80, followed by 497 status code to let it automatically jump to 443 port
	server_name  test.com;
	#为一个server {...} Turn on SSL support
	SSL on                  ;
	#指定PEM格式的证书文件 
	ssl_certificate      /etc/nginx/test.pem; 
	#指定PEM格式的私钥文件
	ssl_certificate_key  /etc/nginx/test.key;
	
	#让http请求重定向到https请求	
	error_page 497	https://$host $uri $args;
}


index.html Refresh Web page


ideas above two methods will consume the resources of the server, we use Curl to access baidu.com try to see how Baidu's company is to achieve baidu.com to www.baidu.com jump

Can see Baidu very clever use Meta refresh function, will baidu.com jump to www.baidu.com. So we can also write a index.html based on the http://test.com of the virtual host path, the content is http to https jump
index.html

 

nginx Virtual Host configuration
server {
	listen 192.168.1.11:80;
	server_name	test.com;
	
	Location/{
                #index. html is placed in the root directory of the virtual host listener
		root/srv/www/http.test.com/;
	}
        #将404的页面重定向到https的首页
	error_page	404	https://test.com/;
}


PostScript(1) The above three methods can be implemented based on Nginx forcing HTTP requests to jump to HTTPS requests, you can evaluate the pros or cons or according to the actual needs of the choice. (2) My blog for all of the students have a certain basis, the reference must be combined with their own actual situation, I have a lot of configuration based on project security considerations to do a limitation, no brain copy.

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.