nginx http跳转到https

  1. 修改nginx配置文件"nginx.conf"

  2. 找到"server"结点

  3. 如将80端口http跳转到443端口时使用如下方式:

server {
	listen       80;
	listen       [::]:80;
	server_name  server_name;
	
	return 301 https://$http_host$request_uri; #第一种方式:(推荐)
	rewrite ^(.*) https://$server_name$1 permanent; #第二种方式
	error_page 497 https://$host:8080$request_uri; #第三种方式,重新定义端口

	error_page 404 /404.html;
	location = /404.html {
	}

	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
	}
}
  1. 如https端口为其它端口,需要原端口http跳转为https使用如下方式:
server {
	listen 8888 ssl http2; 
	server_name server_name_ssl;
	ssl_certificate /home/ssl/rsa.pem;
	ssl_certificate_key /home/ssl/rsa.key;
	ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
	ssl_prefer_server_ciphers on;

	error_page 497 301 =307 https://$host:$server_port$request_uri; #如https为特殊端口时可使用此方式客户端访问http时自动为https
    
	root         /usr/share/nginx/html;

	error_page 404 /404.html;
	location = /404.html {
	}

	error_page 500 502 503 504 /50x.html;
	location = /50x.html {
	}
}
posted @ 2022-05-25 16:46  dcrenl  阅读(4114)  评论(0编辑  收藏  举报