背景介绍

项目前期使用http,后期为了安全方面的考虑,启用了https。

项目架构:前端使用nginx作为多个tomcat实例的反向代理和负载均衡。

实际上只需要在nginx上启用https即可,使客户端与nginx之后使用https方式通信,而nginx与tomcat之间依然以http方式通信。

现在需要将之前客户端所有的http请求全部都自动重定向为https,只需要在nginx上添加相应配置即可。

如下配置实现来源于Nginx HTTP 跳转至 HTTPS,但是我都实践验证过。

另外,也加入了一些自己的理解整理而成。

使用rewrite指令

server {
listen 80;
server_name domain.com;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 443 ssl;
server_name domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.crt;
# other
}

如果此时nginx作为Tomcat的前端反向代理的话,需要将相应配置放在配置ssl的server块中。

使用return指令

server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.crt;
# other
}

如果此时nginx作为Tomcat的前端反向代理的话,需要将相应配置放在配置ssl的server块中。

使用error_page指令

只允许HTTP来访问时,用HTTP访问会让Nginx报497错误,然后利用error_page将链接重定向至HTTPS上。

server {
listen 80;
listen 443 ssl;
server_name domain.com;
ssl on;
ssl_certificate /etc/nginx/ssl/domain.com.crt;
ssl_certificate_key /etc/nginx/ssl/domain.com.crt;
# other
error_page 497 https://$server_name$request_uri;
}

使用error_page指令时,将http和https的监听配置写在同一个server块中,对应的其他配置也需要在该server配置块中完成。

需要注意的是,此时需要将error_page指令语句写在最后,否则不能生效。

【参考】

http://www.netingcn.com/nginx-rewrite-flag.html 关于nginx rewrtie的四种flag

https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite rewrite指令

https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#return return指令

https://nginx.org/en/docs/http/ngx_http_core_module.html#error_page error_page指定

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 关于497状态码在nginx中的扩展应用

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

  1. 使用 Nginx 实现 301 跳转至 https 的根域名

    基于 SEO 和安全性的考量,需要进行 301 跳转,以下使用 Nginx 作通用处理 实现结果 需要将以下地址都统一跳转到 https 的根域名 https://chanvinxiao.com ht ...

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

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

  3. nginx http跳转到https

    server { listen 80; server_name www.888.com; location / { #index.html放在虚拟主机监听的根目录下 root /usr/local/n ...

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

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

  5. Nginx配置http跳转https访问

    Nginx强制http跳转https访问有以下几个方法 nginx的rewrite方法 可以把所有的HTTP请求通过rewrite重写到HTTPS上 配置 方法一 server{ listen ; s ...

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

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

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

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

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

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

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

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

随机推荐

  1. 【CF1097F】Alex and a TV Show(bitset)

    [CF1097F]Alex and a TV Show(bitset) 题面 洛谷 CF 题解 首先模\(2\)意义下用\(bitset\)很明显了. 那么问题在于怎么处理那个\(gcd\)操作. 然 ...

  2. [TJOI2012]桥(最短路+线段树)

    有n个岛屿, m座桥,每座桥连通两座岛屿,桥上会有一些敌人,玩家只有消灭了桥上的敌人才能通过,与此同时桥上的敌人会对玩家造成一定伤害.而且会有一个大Boss镇守一座桥,以玩家目前的能力,是不可能通过的 ...

  3. A1144. The Missing Number

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

  4. 【Asia Yokohama Regional Contest 2018】Arithmetic Progressions

    题目大意:给定 N(1<N<=5000) 个不同元素组成的集合,求从中选出若干数字组成的等差数列最长是多少. 题解:直接暴力有 \(O(n^3)\) 的算法,即:枚举等差数列的前两个值,再 ...

  5. .net 调用 网易云的短信验证

    static string url = "https://api.netease.im/sms/sendcode.action"; static string appKey = & ...

  6. socketv 验证客户端链接的合法性,socketserver

    补充: send()与sendall() 在python socket编程中,有两个发送TCP的函数,send()与sendall(),区别如下: socket.send(string[, flags ...

  7. win10 python3.5 自动补全设置

    https://www.cnblogs.com/lgh344902118/p/8521437.html # python startup file import readline import rlc ...

  8. linux 系统调用之文件操作

    fcntl 文件控制 open 打开文件 creat 创建新文件 close 关闭文件描述字 read 读文件 write 写文件 readv 从文件读入数据到缓冲数组中 writev 将缓冲数组里的 ...

  9. org.apache.poi 读取数字问题

    默认呢,POI读取单元格内容为数字的话,自动搞成Dubbo类型的,比如这样: String value1 = row.getCell(0).getNumericCellValue()+"&q ...

  10. CSS——nth-child()

    nth-child()选择器:CSS3新属性 用法:p:nth-child(2) 选择p标签的父元素 的第二个子元素,并且这个子元素必须是p才起作用 有点绕,有点无厘头,举个栗子: <!DOCT ...