Nginx实现http自动跳转到https

https是更安全的http,通过http自动跳转https,可以更便于用户使用web。

有几下几个方法可以完成跳转:

1.打开http和https的server,让http跳转到https

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
    listen 80;
    listen [::]:80;
    return 301 https://$host$request_uri;
}
 
server {
    listen 443 ssl;
    listen [::]:443 ssl;
 
    ssl_certificate         certificate_file_path;
    ssl_certificate_key  certificate_key_file_path;
 
    ...
 
}

2.不打开http的server,直接在https的server里完成跳转,以下三种方式都可以

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
 
    if ($server_port = 80 )   
 
    #if ($scheme = http )
 
    #if ($ssl_protocol = "")
 
    {
        return 301 https://$host$request_uri;
    }
 
     
    listen 443 ssl;
    listen [::]:443 ssl;
 
    ssl_certificate         certificate_file_path;
    ssl_certificate_key  certificate_key_file_path;
 
    ...
 
}

到此这篇关于Nginx实现http自动跳转到https的文章就介绍到这了,更多相关Nginx http自动跳转到https内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/server/nginx/7569.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部