如果你的tomcat应用需要采用ssl来加强安全性,一种做法是把tomcat配置为支持ssl,另一种做法是用nginx反向代理tomcat,然后把nginx配置为https访问,并且nginx与tomcat之间配置为普通的http协议即可。下面说的是后一种方法,同时假定我们基于spring-boot来开发应用。

一、配置nginx:

server {
listen 80;
listen 443 ssl;
server_name localhost; ssl_certificate server.crt;
ssl_certificate_key server.key; location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}

这里有三点需要说明:

1、nginx允许一个server同时支持http和https两种协议。这里我们分别定义了http:80和https:443两个协议和端口号。如果你不需要http:80则可删除那行。

2、nginx收到请求后将通过http协议转发给tomcat。由于nginx和tomcat在同一台机里因此nginx和tomcat之间无需使用https协议。

3、由于对tomcat而言收到的是普通的http请求,因此当tomcat里的应用发生转向请求时将转向为http而非https,为此我们需要告诉tomcat已被https代理,方法是增加X-Forwared-Proto和X-Forwarded-Port两个HTTP头信息。

二、接着再配置tomcat。基于spring-boot开发时只需在application.properties中进行配置:

server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true

该配置将指示tomcat从HTTP头信息中去获取协议信息(而非从HttpServletRequest中获取),同时,如果你的应用还用到了spring-security则也无需再配置。

此外,由于spring-boot足够自动化,你也可以把上面四行变为两行:

server.tomcat.protocol_header=x-forwarded-proto
server.use-forward-headers=true

下面这样写也可以:

server.tomcat.remote_ip_header=x-forwarded-for
server.use-forward-headers=true

但不能只写一行:

server.use-forward-headers=true

具体请参见http://docs.spring.io/spring-boot/docs/1.3.0.RELEASE/reference/htmlsingle/#howto-enable-https,其中说到:

server.tomcat.remote_ip_header=x-forwarded-for
server.tomcat.protocol_header=x-forwarded-proto
The presence of either of those properties will switch on the valve

此外,虽然我们的tomcat被nginx反向代理了,但仍可访问到其8080端口。为此可在application.properties中增加一行:

server.address=127.0.0.1

这样一来其8080端口就只能被本机访问了,其它机器访问不到。

spring-boot+nginx+tomcat+ssl配置笔记的更多相关文章

  1. CentOS 6.5 nginx+tomcat+ssl配置

    本文档用于指导在CentOS 6.5下使用nginx反向代理tomcat,并在nginx端支持ssl. 安装nginx.参见CentOS 6 nginx安装. SSL证书申请.参见腾讯SSL证书申请和 ...

  2. spring boot启用tomcat ssl

    首先要生成一个keystore证书.参考:Tomcat创建HTTPS访问,java访问https,ssl证书生成:cer&jks文件生成摘录,spring-boot 这里复现一下完整过程: 安 ...

  3. Spring Boot 探索系列 - 自动化配置篇

    26. Logging Prev  Part IV. Spring Boot features  Next 26. Logging Spring Boot uses Commons Logging f ...

  4. Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    我知道大家这段时间看了我写关于 docker 相关的几篇文章,不疼不痒的,仍然没有感受 docker 的便利,是的,我也是这样认为的,I know your felling . 前期了解概念什么的确实 ...

  5. (转)Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    http://www.ityouknow.com/springboot/2018/03/28/dockercompose-springboot-mysql-nginx.html 我知道大家这段时间看了 ...

  6. Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践

    Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...

  7. Docker Compose + Spring Boot + Nginx + Mysql

    Docker Compose + Spring Boot + Nginx + Mysql 实践 我知道大家这段时间看了我写关于 docker 相关的几篇文章,不疼不痒的,仍然没有感受 docker 的 ...

  8. Spring Boot 2.0 教程 | 配置 Undertow 容器

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 资深架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 文章首发于个人网站 https://ww ...

  9. spring boot(10)-tomcat jdbc连接池

    默认连接池 tomcat jdbc是从tomcat7开始推出的一个连接池,相比老的dbcp连接池要优秀很多.spring boot将tomcat jdbc作为默认的连接池,只要在pom.xml中引入了 ...

随机推荐

  1. 打造自己个性的notepad ++

    对coder来说,notepad ++ 是一个很不错的文本编辑器.平时用来看看代码.xml文件,都比系统自带的记事本舒服得多.不过,对于像我这种每天用notepad ++写代码的人,一个原装的note ...

  2. JQuery Ajax 获取数据

    前台页面:   对一张进行查询,删除,添加 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"& ...

  3. codeforces 166C Median - from lanshui_Yang

    C. Median time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  4. 【软件技巧】Sublime Text为不同语法定义不同高亮

    Sublime Text默认的语法高亮已经非常美丽了,可是对于个别语言还是有些不爽. 默认高亮规则叫Monokai,能够从Preferences->Settings - Default中看到: ...

  5. C函数的实现(strcpy,atoi,atof,itoa,reverse)

    在笔试面试中经常会遇到让你实现C语言中的一些函数比如strcpy,atoi等 1. atoi 把字符串s转换成数字 int Atoi( char *s ) { int num = 0, i = 0; ...

  6. Myeclipse安装破解

  7. [Cycle.js] From toy DOM Driver to real DOM Driver

    This lessons shows how we are able to easily swap our toy DOM Driver with the actual Cycle.js DOM Dr ...

  8. 2013年全球ERP市场格局(Gartner)

    Gartner于5月5日公布了全球ERP市场的分析报告,报告称全球ERP软件销售额2013年整体增长了3.8%(从2012年$244亿美元到2013年$258亿美元),全球前五位ERP厂商座次例如以下 ...

  9. vim 快捷键大全

    一.移动光标 1.左移h.右移l.下移j.上移k 2.向下翻页ctrl + f,向上翻页ctrl + b 3.向下翻半页ctrl + d,向上翻半页ctrl + u 4.移动到行尾$,移动到行首0(数 ...

  10. 前端--关于javascript基础

    首先javascript不是浏览器的附属品,只能说它大多数的运行环境是在浏览器中的,但又不仅仅局限于浏览器中.它是一门真正的程序设计语言,在这方面它和java.c.c++.c#是等同的,只不过它不直接 ...