zoukankan      html  css  js  c++  java
  • Nginx实现同一端口HTTP跳转HTTPS

    转自:https://www.cnblogs.com/pencile/p/Nginx_http_to_https.html

    小目标:在只监听一个端口的情况下,将http访问跳转为https。 

    一般情况下http协议使用80端口,https协议443端口。要实现http强制转https是非常简单的事,随便都可以找到很多方案。使用非默认端口时这就变得有点麻烦了。

    曾经看过一篇文章讲述如何让http 和https 在一个端口上工作。原理大概是以tcp方式监听,检查传入的前几个字节,从而判断出是HTTP还是HTTPS的请求,再将数据转发到相应端口上。这种方式非常强悍,但如果仅仅是让http跳转到https有点杀鸡用牛刀的感觉。

    最近无意中看到一篇有关Nginx的文章,其中提到了一个497的状态码:

    Nginx internal code used for the plain HTTP requests that are sentto HTTPS port to distinguish it from 4XX in a log and an error pageredirection.

    由此可见Nginx内部是可以检测到错误的请求的。因此可以尝试通用error_page去拦截。 配置代码:

    error_page 497 https://$host:$server_port$uri$is_args$args;

    重启Nginx 后,使用浏览器以http访问网站,跳转了!!!

    由此证明用error_page拦截497实现http跳转https是可行的。

    PS:error_page可以作为server节点的局部配置也可以放在http节点作为全局配置。

  • 相关阅读:
    HDU 4619 Warm up 2 (多校)
    深入浅出Node.js (9)
    HDU 1106 排序(排序)
    字符串相关心得
    HDU 2547 无剑无我(数学)
    HDU 2549 壮志难酬(字符串,处理小数点)
    HDU 2551 竹青遍野(循环,水)
    HDU 2552 三足鼎立(数学函数)
    HDU 2555 陷阱(模拟,结构体数组)
    HDU 2561 第二小整数(排序,水)
  • 原文地址:https://www.cnblogs.com/iitrust/p/12825931.html
Copyright © 2011-2022 走看看