Nginx configuration protocol SSL certificate Https and Http redirect automatically jump to Https

Use the cloud server and Ali free certificate

We first need to Ali cloud management console to download the SSL certificate SSL certificate

Here Insert Picture Description

Then extract the certificate there are two documents a key a pem

Here Insert Picture Description

Nginx reached under the conf directory certdirectory (the directory does not exist need to create your own)

Modify the configuration nginx.conf

Configured according to their actual situation

server {
	listen 443 ssl;   #SSL协议访问端口号为443 此处若未添加ssl可能会导致Nginx无法启动
	server_name localhost;  #将localhost修改为证书绑定的域名 www.xxx.com
	root html;
	index index.html index.htm;
	ssl_certificate cert/name.pem;   #将name.pem替换成证书的文件名
	ssl_certificate_key cert/name.key;   #将dname.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;   
	}
}    

Save configuration
Here Insert Picture Description
If you still can not access must open port 443 on the server


Http automatically jump to Https

Nginx.conf or in the configuration file
to add a server:

server
{
	listen 80;
	server_name www.test.com;
	return 301 https://$server_name$request_uri;
}

final effect:

server
{
	listen 80;
	server_name www.test.com;
	return 301 https://$server_name$request_uri;
}

server
{
	listen 443 ssl; #监听端口
	
	server_name www.test.com; #域名
	
	ssl_certificate cert/test.pem;   #将domain name.pem替换成您证书的文件名。
	ssl_certificate_key cert/test.key;   #将domain name.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;
	其余省略...
}

About this redirection check and rewrite a lot of information will be displayed with no return 301 This page contains too many redirects
you can use this


Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/104996099