zoukankan      html  css  js  c++  java
  • nginx强制使用https访问(http跳转到https)

    需求简介

    基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.com,但是很多用户不清楚https和http的区别,会很容易敲成http://test.com,这时会报出404错误,所以我需要做基于test.com域名的http向https的强制跳转

    我总结了三种方式,跟大家共享一下

    nginx的rewrite方法

    思路

    这应该是大家最容易想到的方法,将所有的http请求通过rewrite重写到https上即可

    配置

    server {

    listen192.168.1.111:80;

    server_nametest.com;

    rewrite ^(.*)$https://$host$1permanent;

    }

    搭建此虚拟主机完成后,就可以将http://test.com的请求全部重写到https://test.com上了

    nginx的497状态码

    error code 497

    497 - normal request was sent to HTTPS

    解释:当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码

    思路

    利用error_page命令将497状态码的链接重定向到https://test.com这个域名上

    配置

    server {

    listen       192.168.1.11:443;#ssl端口

    listen       192.168.1.11:80;#用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口

    server_name  test.com;

    #为一个server{......}开启ssl支持

    ssl                  on;

    #指定PEM格式的证书文件 

    ssl_certificate      /etc/nginx/test.pem; 

    #指定PEM格式的私钥文件

    ssl_certificate_key  /etc/nginx/test.key;

    #让http请求重定向到https请求

    error_page 497https://$host$uri?$args;

    }

    index.html刷新网页

    思路

    上述两种方法均会耗费服务器的资源,我们用curl访问baidu.com试一下,看百度的公司是如何实现baidu.com向www.baidu.com的跳转

    可以看到百度很巧妙的利用meta的刷新作用,将baidu.com跳转到www.baidu.com.因此我们可以基于http://test.com的虚拟主机路径下也写一个index.html,内容就是http向https的跳转

    index.html

    <html>

    <meta http-equiv="refresh" content="0;url=https://test.com/">

    </html>

    nginx虚拟主机配置

    server {

    listen 192.168.1.11:80;

    server_nametest.com;

    location / {

                    #index.html放在虚拟主机监听的根目录下

    root /srv/www/http.test.com/;

    }

            #将404的页面重定向到https的首页

    error_page404https://test.com/;

    }

    后记

    (1)上述三种方法均可以实现基于nginx强制将http请求跳转到https请求,大家可以评价一下优劣或者根据实际需求进行选择。

    需求简介

    基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.com,但是很多用户不清楚https和http的区别,会很容易敲成http://test.com,这时会报出404错误,所以我需要做基于test.com域名的http向https的强制跳转

    我总结了三种方式,跟大家共享一下

     

     

    nginx的rewrite方法

     

    思路

    这应该是大家最容易想到的方法,将所有的http请求通过rewrite重写到https上即可
     

    配置

    1. server {  
    2.     listen  192.168.1.111:80;  
    3.     server_name test.com;  
    4.       
    5.     rewrite ^(.*)$  https://$host$1 permanent;  
    6. }  

    搭建此虚拟主机完成后,就可以将http://test.com的请求全部重写到https://test.com上了
     
     

    nginx的497状态码

     

    error code 497

    1. 497 - normal request was sent to HTTPS  

    解释:当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码
     

    思路

    利用error_page命令将497状态码的链接重定向到https://test.com这个域名上
     

    配置

    1. server {  
    2.     listen       192.168.1.11:443;  #ssl端口  
    3.     listen       192.168.1.11:80;   #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口  
    4.     server_name  test.com;  
    5.     #为一个server{......}开启ssl支持  
    6.     ssl                  on;  
    7.     #指定PEM格式的证书文件   
    8.     ssl_certificate      /etc/nginx/test.pem;   
    9.     #指定PEM格式的私钥文件  
    10.     ssl_certificate_key  /etc/nginx/test.key;  
    11.       
    12.     #让http请求重定向到https请求   
    13.     error_page 497  https://$host$uri?$args;  
    14. }  
     

    index.html刷新网页

     

    思路

    上述两种方法均会耗费服务器的资源,我们用curl访问baidu.com试一下,看百度的公司是如何实现baidu.com向www.baidu.com的跳转
     
     
    可以看到百度很巧妙的利用meta的刷新作用,将baidu.com跳转到www.baidu.com.因此我们可以基于http://test.com的虚拟主机路径下也写一个index.html,内容就是http向https的跳转
     

    index.html

    1. <html>  
    2. <meta http-equiv="refresh" content="0;url=https://test.com/">  
    3. </html>  

    nginx虚拟主机配置

    1. server {  
    2.     listen 192.168.1.11:80;  
    3.     server_name test.com;  
    4.       
    5.     location / {  
    6.                 #index.html放在虚拟主机监听的根目录下  
    7.         root /srv/www/http.test.com/;  
    8.     }  
    9.         #将404的页面重定向到https的首页  
    10.     error_page  404 https://test.com/;  
    11. }  
     
     

    后记

    上述三种方法均可以实现基于nginx强制将http请求跳转到https请求,大家可以评价一下优劣或者根据实际需求进行选择。
  • 相关阅读:
    7zip在DOS命令行用法总结
    WinRAR在DOS下压缩/解压缩的使用方法
    Linux关闭防火墙命令
    将java的jar包,打包为rpm 安装包
    spring mvc 基于注解 配置默认 handlermapping
    Spring + Spring MVC + MyBatis 整合
    Android开发环境搭建全程演示(jdk+eclipse+android sdk)
    使用Spring MVC表单标签
    context:component scan配置策略
    Spring AutoWiring Beans with @Autowired annotation
  • 原文地址:https://www.cnblogs.com/shaozhu520/p/15222529.html
Copyright © 2011-2022 走看看