server {
listen 80;
server_name www.888.com; location / {
#index.html放在虚拟主机监听的根目录下
root /usr/local/nginx/html/http.888.com/;
rewrite ^/(.*) https://www.888.com/$1 last;
}
#将404的页面重定向到https的首页
error_page 404 https://www.888.com/;
}

  


 chown -R www:www /usr/local/nginx/html

/usr/local/nginx/html/http.88888.com/index.html
<html>
<meta http-equiv="refresh" content="0;url=https://www.88888.com/">
</html>

  

需求简介

基于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. rewrite ^(.*)$  https://$host$1 permanent;
  5. }

搭建此虚拟主机完成后,就可以将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. #让http请求重定向到https请求
  12. error_page 497  https://$host$uri?$args;
  13. }
 

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. location / {
  5. #index.html放在虚拟主机监听的根目录下
  6. root /srv/www/http.test.com/;
  7. }
  8. #将404的页面重定向到https的首页
  9. error_page  404 https://test.com/;
  10. }
 
 

后记

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

nginx http跳转到https的更多相关文章

  1. nginx从http跳转到https

    场景 项目前期使用http,后期为了安全方面的考虑,启用了https. 项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡. 实际上只需要在nginx上启用https即可,使客户 ...

  2. Nginx环境中如何将HTTP跳转至HTTPS设置

    如果我们VPS服务器的WEB环境采用的是NGINX架构,那如果我们将安装SSL证书的网站希望强制跳转至HTTPS网站URL的时候那需要如何设置呢?这里个人建议是这样的,我们必须要强制一个地址,这样网站 ...

  3. Nginx配置http强制跳转到https

    目的:访问http://sdk.open.test.com/时强制自动跳转到https://sdk.open.test.com/ 修改nginx站点配置文件sdk.open.test.com.conf ...

  4. CentOS 7 配置nginx并默认强制使用https对http进行跳转

    1.安装nginx yum install nginx 2.启动nginx服务 service nginx start 3.开启防火墙80端口,云服务器和本地虚拟服务器各有不同,不再赘述. 4.访问你 ...

  5. nginx强制使用https访问(http跳转到https)

    Nginx 的 Location 从零开始配置 - 市民 - SegmentFault 思否https://segmentfault.com/a/1190000009651161 nginx配置loc ...

  6. Nginx的https配置记录以及http强制跳转到https的方法梳理

    一.Nginx安装(略)安装的时候需要注意加上 --with-http_ssl_module,因为http_ssl_module不属于Nginx的基本模块.Nginx安装方法: 1 2 # ./con ...

  7. nginx配置http访问自动跳转到https

    1.按照如下格式修改nginx.conf 配置文件,80端口会自动转给443端口,这样就强制使用SSL证书加密了.访问http的时候会自动跳转到https上面 server { listen ; se ...

  8. nginx证书制作以及配置https并设置访问http自动跳转https(反向代理转发jboss)

    nginx证书制作以及配置https并设置访问http自动跳转https 默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖 ...

  9. (转)Nginx的https配置记录以及http强制跳转到https的方法梳理

    Nginx的https配置记录以及http强制跳转到https的方法梳理 原文:http://www.cnblogs.com/kevingrace/p/6187072.html 一.Nginx安装(略 ...

随机推荐

  1. DB Create and Insert

    <?php $servername = "localhost"; $username = "username"; $password = "pa ...

  2. 6、网页制作Dreamweaver(HTML结构--dom操作)

    一.基本语法:数据类型(字符串,小数,整数,布尔,时间) var, var s = "3.14"; var n = parseFloat(s); ; s += 5; var d = ...

  3. Cryptography加密和解密

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Se ...

  4. 重学STM32---(八)----SDIO

    1. SDIO(SD/SDIO MMC卡主机模块)在AHB外设总线和多媒体卡(MMC).SD存储卡.SDIO卡和CE-ATA设备间提供了操作接口.(SDIO没有SPI兼容的通信模式 ) 1.1.什么是 ...

  5. 利用range() 控制循环

    s = ['a','b','c','d','e'] for i in range(len(s)):...     if i < len(s)-1:...         print s[i] a ...

  6. JS初学之-循环生成坐标

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  7. 工作中遇到的问题--eclipse没有方法提示

    一.eclipse自身配置问题步骤:1. 打开Eclipse ,然后“window”→“Preferences”2. 选择“java”,展开,“Editor”,选择“Content Assist”.3 ...

  8. Codeforces378 D Kostya the Sculptor(贪心)(逻辑)

    Kostya the Sculptor time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  9. 课堂所讲整理:super和转型(修改版)

    创建父类: package org.hanqi.pn0120; public class Father { private String name; private int age; public S ...

  10. UVALive-4670 Dominating Patterns(AC自动机)

    题目大意:找出出现次数最多的模式串. 题目分析:AC自动机裸题. 代码如下: # include<iostream> # include<cstdio> # include&l ...