nginx配置http强制跳转https

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

一、采用nginx的rewrite方法

1.下面是将所有的http请求通过rewrite重写到https上。

例如将web.heyonggs.com域名的http访问强制跳转到https。

server
{
listen ;
server_name web.heyonggs.com;
rewrite ^(.*)$ https://$host$1 permanent; //这是nginx早前的写法,现在还可以使用
}

上面的跳转配置rewrite ^(.*)$  https://$host$1 permanent;

也可以改为下面

server
{
listen ;
server_name web.heyonggs.com;
rewrite ^/(.*)$ http://web.heyonggs.com/$1 permanent;
}

或者

server
{
listen ;
server_name web.heyonggs.com;
rewrite ^ http://web.heyonggs.com$request_uri? permanent;
}

2.最新的写法

server
{
listen ;
server_name web.heyonggs.com;
return https://$server_name$request_uri; //最新写法
}

3.这种方式适用于多域名的时候,即访问heyonggs.com的http也会强制跳转到https://web.heyonggs.com上面

server
{
listen ;
server_name heyonggs.com web.heyonggs.com;
if ($host ~* "^heyonggs.com$") {
rewrite ^/(.*)$ https://web.heyonggs.com/ permanent;
}
}

4.下面是最简单的一种配置

server
{
listen ;
server_name web.heyonggs.com;
if ($host = "web.heyonggs.com") {
rewrite ^/(.*)$ http://web.heyonggs.com permanent;
}
}

二、采用nginx的497状态码

497 - normal request was sent to HTTPS

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

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

配置实例:如下访问web.heyonggs.com或者heyonggs.com的http都会被强制跳转到https

server
{
listen ;
server_name web.heyonggs.com;
error_page https://$host$uri?$args;
}

也可以将80和443的配置放在一起:

server
{
listen ;
listen ;
server_name web.heyonggs.com;
error_page https://$host$uri?$args;
}

三、利用meta的刷新作用将http跳转到https

上述的方法均会耗费服务器的资源,可以借鉴百度使用的方法:巧妙的利用meta的刷新作用,将http跳转到https

可以基于http://dev.wangshibo.com的虚拟主机路径下写一个index.html,内容就是http向https的跳转
 
将下面的内容追加到index.html首页文件内
[root@localhost ~]# cat /var/www/html//index.html
<html>
<meta http-equiv="refresh" content="0;url=https://web.heyonggs.com/">
</html>

nginx的配置文件

server
{
listen ;
listen ;
server_name web.heyonggs.com; #将404的页面重定向到https的首页
error_page https://$host$uri?$args;
}

四、通过proxy_redirec方式

解决办法:
# re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;

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

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

    个人真实配置 架构:Nginx 反向代理 + Nginx 前端(LNMP) 在 Nginx 反向代理的 虚拟机主机配置文件中,作如下配置: upstream ilexa_cn { server 192 ...

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

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

  3. windwos下nginx 配置https并http强制跳转https

    windwos下nginx  配置https并http强制跳转https 一.首先配置证书文件 申请证书文件,这里就不做详细过程了,直接看证书文件结果. 这是两个证书的关键文件 打开ngxin下con ...

  4. 解决:ngxin做http强制跳转https,接口的POST请求变成GET

    域名配置了http强制跳转htpps后发现app发起post请求会出现405错误. 所以怀疑是http强制跳转https出现了问题.修改nginx配置如下即可解决: server { listen ; ...

  5. ngxin做http强制跳转https,接口的POST请求变成GET

    http强制跳转https出现了问题.修改nginx配置如下即可解决: server { listen 80; server_name *.snsprj.cn; return 307 https:// ...

  6. 开启HSTS让浏览器强制跳转HTTPS访问

    开启HSTS让浏览器强制跳转HTTPS访问 来源 https://www.cnblogs.com/luckcs/articles/6944535.html 在网站全站HTTPS后,如果用户手动敲入网站 ...

  7. Nginx下HTTP强制重定向至HTTPS

    Nginx下HTTP强制重定向至HTTPS 对于nginx来说,配置http强制重定向至https有多种多样的写法.可以直接rewrite,也可以用301重定向.但是直接拷贝网上的配置往往会出现问题, ...

  8. 服务器 apache配置https,http强制跳转https(搭建http与https共存)

    公司linux服务器上的nginx的已经改成https了,现在还剩下一个windows云服务器没配置. 环境 windows wampserver2.5 64位 1.腾讯云申请的ssl 包含三个文件: ...

  9. nginx安装SSL证书,并强制跳转https访问

    网站最初是nginx代理80端口,实现http访问的.现在要安装SSL证书,使用https访问. 我的nginx根目录是/usr/local/nginx,将申请的SSL证书和key放在/usr/loc ...

随机推荐

  1. Spring中的@Async

    在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在spring 3.x之后, ...

  2. 使用metrics统计接口tps

    metrics的简单介绍 metrics是一种性能指标工具,有很多开源工具使用之来来作为其性能监控,如Hadoop,Kafka,Spark,JStorm等. metrics使用最主要有三个东西: Me ...

  3. IDEA-------破解方法

    ① 到这个地方下载 IntelliJ IDEA 注册码:http://idea.lanyus.com/  就是这个jar包:JetbrainsCrack-2.6.10-release-enc.jar ...

  4. 【C/C++】Rotate Array

    实现数组旋转(循环右移) 如数组 [1, 2, 3, 4, 5, 6, 7],右移 3 位则为 [5, 6, 7, 1, 2, 3, 4] 首先使用泛型函数 void Rotate(void *fro ...

  5. 最近使用Navicat for MySQl访问远程mysql数据库,出现报错,显示“2003- Can't connect MySQL Server on 'localhost'(10038)“。

    优先考虑mysql数据库是否开启 1.先看报错窗口.   通过百度,最终找到的原因是:远程3306端口未对外开放. 于是下面进行远程3306端口开放操作.   首先远程连接服务器,点击“开始”-“管理 ...

  6. Android设备终端监控可能用到的工具

    先记录工具,以后详细分析 add-property-tagcheck-lost+foundcpueatercpustatsdaemonizedexdumpdirectiotestfiokexecloa ...

  7. JavaScript(ES6)学习笔记-Set和Map数据结构(一)

    一.Set 1.ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. , , , , ']); s; // ...

  8. linux学习--目录切换命令 cd

  9. Python—字符串的操作

    字符串的操作 变量: 变量只能是 字母,数字或下划线的任意组合,但首个字符不能为数字,且不能有空格 以下关键字不能声明为变量: and ,as, assert, break ,class ,conti ...

  10. ubuntu 16.04 安装brackets

    1 github 上下载deb包 2 双击deb包但是安装不了(无任何提示,是因为缺少lib文件) 网上给出的解决方案 使用debtool 接压 deb包 把缺少的lib复制进去 配置文件中去掉该 l ...