nginx https

nginx http 强制跳转到https

方法一

1
2
3
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
  • 列子
1
2
3
4
5
6
7
8
9
server {
listen 80;
listen 443;
server_name xxx.test.com;
index index.html index.php index.htm;
if ($scheme = http ) {
return 301 https://$host$request_uri;
}
}

方法二

1
2
3
if ($server_port = 80 ) {
return 301 https://$host$request_uri;
}
  • 列子
1
2
3
4
5
6
7
8
9
server {
listen 80;
listen 443;
server_name xxx.test.com;
index index.html index.php index.htm;
if ($server_port = 80 ) {
return 301 https://$host$request_uri;
}
}

方法三

1
2
if ($ssl_protocol = "") { return 301 https://$server_name$request_uri; }
if ($host != xxx.test.com) { return 301 $scheme://xxx.test.com$request_uri; }
  • 列子
1
2
3
4
5
6
7
8
server {
listen 80;
listen 443;
server_name xxx.test.com;
index index.html index.php index.htm;
if ($ssl_protocol = "") { return 301 https://$server_name$request_uri; }
if ($host != xxx.test.com) { return 301 $scheme://xxx.test.com$request_uri; }
}

方法四

1
2
3
4
5
server {
listen 80;
server_name test.com www.test.com;
rewrite ^(.*) https://www.test.com$1 permanent;
}
  • 例子
1
2
3
4
5
6
7
8
9
10
11
server {
listen 80;
server_name test.com www.test.com;
rewrite ^(.*) https://www.test.com$1 permanent;
}
server {
listen 443;
server_name test.com www.test.com;
index index.html index.htm index.php;
root ;
}

方法五

1
2
3
4
5
server {
listen 80;
server_name test.com www.test.com;
return 301 https://$server_name$request_uri;
}
坚持原创技术分享,您的支持将鼓励我继续创作!
0%