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. 电话 SMS 邮件 网页 AppStore

    //调用safar打开网页 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.cnbl ...

  2. HDU 4462

    http://acm.hdu.edu.cn/showproblem.php?pid=4462 一道题意不清的水题 题意:给一个n*n的格子,在上面放草人,每个草人有恐惧范围,问最少选择几个草人可以覆盖 ...

  3. Team Foundation API - 编程访问 WorkItem

    Team Foundation Server (TFS)工具的亮点之一是管理日常工作项, 工作项如Bug, Task,Task Case等. 使用TFS API编程访问TFS服务器中的工作项, 步骤如 ...

  4. Condition的await-signal流程详解(转)

    上一篇文章梳理了condtion,其中侧重流程,网上看到这篇文章文章介绍的很细.值得学习.特意转载过来.   转载请注明出处:http://blog.csdn.net/luonanqin 转载路径:h ...

  5. SpringCloud学习后获取的地址

    关于SpringCloud + Docker 学习地址: (1) https://yq.aliyun.com/articles/57265 (2) https://yq.aliyun.com/team ...

  6. [POJ] 3277 .City Horizon(离散+线段树)

    来自这两篇博客的总结 http://blog.csdn.net/SunnyYoona/article/details/43938355 http://m.blog.csdn.net/blog/mr_z ...

  7. Educational Codeforces Round 15 B

    B. Powers of Two time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  8. Linux软件安装管理概述

    介绍如何在Linux字符界面下安装软件 课程大纲: 一.软件包管理简介 二.rpm命令管理 三.yum在线管理 四.源码包管理 五.脚本安装包

  9. kuangbin_ShortPath N (POJ 1847)

    模板题辣很简单的 只有两种val 0 和1 #include <iostream> #include <string> #include <cstdio> #inc ...

  10. [poj 3261]Milk Patterns

    后缀数组搞一下就可以了喵~ 其实这道题的第一个想法是 SAM ,建完后缀自动机后拓扑排序跑一遍统计下每个子串的出现次数就 O(N) 就妥妥过掉了 后缀树也是 O(N) 的,统计一下每个节点对应的子树中 ...