个人真实配置

架构:Nginx 反向代理 + Nginx 前端(LNMP)

在 Nginx 反向代理的 虚拟机主机配置文件中,作如下配置:

upstream ilexa_cn {
server 192.168.1.100:80;
keepalive 50;
} server {
listen 80;
server_name ilexa.cn;
return 301 https://$server_name$request_uri;
} server {
listen 443 ssl;
server_name ilexa.cn;
access_log logs/ilexa_cn.log main;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Connection "keep-alive";
ssl on;
ssl_session_timeout 5m; ssl_certificate /etc/nginx/pki/ilexa_cn_bundle.crt;
ssl_certificate_key /etc/nginx/pki/ilexa_cn.key; location / {
proxy_pass http://ilexa_cn;
proxy_set_header Host $host;
# needed for HTTPS
proxy_set_header X_FORWARDED_PROTO https;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} error_page 497 https://$host$uri?$args;
}

【网络资料】Nginx 配置 http 强制跳转到 https

Nginx 的 return 301 跳转

项目的虚拟主机配置文件:

#先监听80端口,为http请求,然后强制转发到https监听的443端口上面
server {
listen 80;
root /var/www/html/ilexa/;
server_name ilexa.cn;
return 301 https://$server_name$request_uri;
} #监听443端口,https请求
server {
listen 443 ssl;
root /var/www/html/ilexa/;
index index.php index.html index.htm;
server_name ilexa.cn; ssl on;
ssl_certificate /etc/nginx/pki/ilexa_cn_bundle.crt;
ssl_certificate_key /etc/nginx/pki/ilexa_cn.key; ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照这个协议配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; #按照这个套件配置
ssl_prefer_server_ciphers on; location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS $https if_not_empty; # 网上资料有的这行也不增加
include fastcgi_params;
}
}

Nginx 的 Rewrite 方法

将所有的 http 请求通过 rewrite 重写到 https 上即可

Nginx前端的配置:

#先监听80端口,为http请求,然后强制转发到https监听的443端口上面
server {
listen 80;
root /var/www/html/ilexa/;
server_name ilexa.cn;
rewrite ^(.*) https://$server_name$1 permanent;
} #监听443端口,https请求
server {
listen 443 ssl;
root /var/www/html/ilexa/;
server_name ilexa.cn;
ssl_certificate /etc/nginx/pki/ilexa_cn_bundle.crt;
ssl_certificate_key /etc/nginx/pki/ilexa_cn.key; location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

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

Nginx 的 497 状态码

497 - normal request was sent to HTTPS  

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

思路:

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

配置:

server {
listen 192.168.1.100:443; #ssl端口
listen 192.168.1.100:80; #用户习惯用http访问,加上80,后面通过497状态码让它自动跳到443端口
server_name ilexa.cn; ssl on; #为一个server{......}开启ssl支持 ssl_certificate /etc/nginx/ilexa_cn.crt; #指定crt格式的证书文件
ssl_certificate_key /etc/nginx/ilexa_cn.key; #指定crt格式的私钥文件 #让http请求重定向到https请求
error_page 497 https://$host$uri?$args;
}

利用 meta 的刷新作用(index.html首页跳转)

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

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

index.html 文件内容:

<html>
<meta http-equiv="refresh" content="0;url=https://ilexa.cn/">
</html>

nginx 虚拟主机配置:

server {
listen 192.168.1.100:80;
server_name ilexa.cn; location / {
root /srv/www/ilexa/; #index.html放在虚拟主机监听的根目录下
} error_page 404 https://ilexa.cn/; #将404的页面重定向到https的首页
}

Aapache 下 http 强制跳转到 https 的配置

根目录建立 .htaccess 文件。内容如下

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

当用户在浏览器直接输入域名 ilexa.cn 的时候,这时候域名直接会转向https://ilexa.cn,而且服务器这样配置的效率也蛮高的。

Nginx 配置 http 强制跳转到 https的更多相关文章

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

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

  2. nginx配置http强制跳转https

    nginx配置http强制跳转https 网站添加了https证书后,当http方式访问网站时就会报404错误,所以需要做http到https的强制跳转设置. 一.采用nginx的rewrite方法 ...

  3. tomcat7.0.55配置HTTP强制跳转到HTTPS

    首先需要配置好HTTPS单向或双向链接 参考: tomcat7.0.55配置单向和双向HTTPS连接(二) 然后编辑tomcat的conf目录下的web.xml 在<welcome-file-l ...

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

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

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

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

  6. 阿里云SLB上http强制跳转到https问题处理

    背景: 最近一客户有一个需求,需要将外网所有http访问请求强制跳转到https,公网出口使用阿里云SLB,证书放在SLB上,SLB后端实例为ECS(webserver)web服务使用nginx, 网 ...

  7. k8s ingress路由强制跳转至https设置

    为ingress配置增加注解(annotations):nginx.ingress.kubernetes.io/ssl-redirect: 'true' 就可以实现http强制跳转至https 不过默 ...

  8. http强制跳转到https

    原文地址:http://m.blog.csdn.net/article/details?id=8549290 需求简介 基于nginx搭建了一个https访问的虚拟主机,监听的域名是test.com, ...

  9. https进行配置以及http跳转到https配置

    https配置: nginx配置 server { listen 443; server_name localhost; ssl on; root html; index index.html ind ...

随机推荐

  1. 深入理解计算机系统 Start && 第一章要点

    对此书已经慕名已久了,抽空看了第1,2,3,5章,其他章节等有空闲继续看吧. 我的许多博客是给自己快速复习使用的,比如此读书后感,你可以根据我下面的建议读完原书几章再回来复习一下(或许那时候就没必要回 ...

  2. Jogl2.0 jogamp-all-platforms 在eclipse 中的配置

    我的电脑在win8 64位系统,搞了好久,网上的方法都快试了个遍,官网的试了,都不行,目前成功了,希望可以帮助其他同学. 1.首先去这里http://jogamp.org/deployment/jog ...

  3. 2020重新出发,NOSQL,MongoDB是什么?

    什么是MongoDB ? MongoDB 是一个开源的文档数据库,它基于 C++ 语言编写,性能高,可用性强,能够自动扩展. MongoDB 是最流行的 NoSQL 数据库之一,原生支持分布式集群架构 ...

  4. 基于jQuery的鼠标悬停时放大图片的效果制作

    这是一个基于jQuery的效果,当鼠标在小图片上悬停时,会弹出一个大图,该大图会跟随鼠标的移动而移动.这个效果最初源于小敏同志的一个想法,刚开始做的时候只能实现弹出的图片是固定的,不能随鼠标移动,最后 ...

  5. 用c语言处理文件

    用c语言处理文件只需要用到几个简单的函数: 1.文件的打开和关闭 fopen()函数用来打开一个文件,该函数原型在头文件stdio.h中,调用的一般形式为 /* FILE 是c语言内置的一个结构体类型 ...

  6. 刀哥之指纹识别biometrics

    指纹识别 - 生物识别 简介 iPhone 5S 开始支持 iOS 8.0 开放了 Touch ID 的接口 代码准备 - (void)touchesBegan:(NSSet *)touches wi ...

  7. 云计算openstack——高可以负载均衡(14)

    一.云平台概要 1.本openstack云平台使用开源软件Openstack Ocata版…… 2.OpenStack 部署环境中,各节点可以分为几类: Cloud Controller Node ( ...

  8. 217。数据中是否有重复元素(哈希表/set简法)

    给定一个整数数组,判断是否存在重复元素. 如果任意一值在数组中出现至少两次,函数返回 true .如果数组中每个元素都不相同,则返回 false . 示例 1: 输入: [1,2,3,1] 输出: t ...

  9. java学习(九) —— java中的File文件操作及IO流概述

    前言 流是干什么的:为了永久性的保存数据. IO流用来处理设备之间的数据传输(上传和下载文件) java对数据的操作是通过流的方式. java用于操作流的对象都在IO包中. java IO系统的学习, ...

  10. 趣图:普通人讲故事 VS 程序员讲故事

      扩展阅读 趣图:我说自己菜 vs 大佬说自己菜 趣图:客户需求VS客户预算 趣图:在外行人眼中的程序员 如何处理前任程序员留下的代码 一个故事讲清楚NIO