HTTP重定向到HTTPS,IIS7与APACHE实现及nginx配置

2019-02-1920:18:40服务器及运维Comments4,482 views字数 2272阅读模式
IIS7和Apache上实现访问HTTP跳转到HTTPS访问的方法,网站设计出于安全的考虑需要使用https协议,但不少用户因为输入网址的习惯不喜欢带上https协议,导致访问异常,因此需要一种重定向功能,实现HTTP网站重定向到HTTPS网站的方法,具体操作如下

IIS7文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

从微软的官方网站下载HTTP重写模块,安装完毕之后重启IIS服务,之后打开IIS控制台,发现多了一个组件,双击“URL重写”,在右边窗体中选择“添加规则”,并添加一个空白规则,给规则自定义一个名字(名称自便),比如我这里叫“redirect to HTTPS”,模式为:(.*),添加一个条件,条件输入为 {HTTPS},与模式匹配,模式为 ^OFF$,然后配置操作,操作类型为:重定向,重定向到URL为:https://{HTTP_HOST}/{R:1},重定向类型:永久301。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

设置完毕后点击右侧的“应用”,这个 URL 重写就配置完毕了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

配置后,根目录下的web.config文件的内容如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="redirect to HTTPS" enabled="true" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="^OFF$" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Apache http跳转https配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

修改.htaccess文件,在文件里增加如下几行:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

另一种写法是:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]

nginx配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

nginx的rewrite方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

思路文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

这应该是大家最容易想到的方法,将所有的http请求通过rewrite重写到https上即可文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

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

搭建此虚拟主机完成后,就可以将http://test.com的请求全部重写到https://test.com上了文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

nginx的497状态码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

error code 497文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

497 - normal request was sent to HTTPS文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

解释:当此虚拟站点只允许https访问时,当用http访问时nginx会报出497错误码文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

思路文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

利用error_page命令将497状态码的链接重定向到https://test.com这个域名上文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

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;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

#让http请求重定向到https请求
error_page 497  https://$host$uri?$args;
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

index.html刷新网页文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

思路
上述两种方法均会耗费服务器的资源,我们用curl访问baidu.com试一下,看百度的公司是如何实现baidu.com向www.baidu.com的跳转文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

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

index.html文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

<html>
<meta http-equiv="refresh" content="0;url=https://test.com/">
</html>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

nginx虚拟主机配置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

server {
listen 192.168.1.11:80;
server_name test.com;文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

location / {
#index.html放在虚拟主机监听的根目录下
root /srv/www/http.test.com/;
}
#将404的页面重定向到https的首页
error_page  404 https://test.com/;
}文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

后记
上述三种方法均可以实现基于nginx强制将http请求跳转到https请求,大家可以评价一下优劣或者根据实际需求进行选择。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/9630.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/9630.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定