本文档用于指导在CentOS 6.5下使用nginx反向代理tomcat,并在nginx端支持ssl。

安装nginx。参见CentOS 6 nginx安装。

SSL证书申请。参见腾讯SSL证书申请和配置或[使用certbot为站点添加https支持]。

安装tomcat。目前使用的版本为8.x。一般使用以下版本:点击下载8.5.x

nginx 反向代理以及ssl配置

这里仅介绍片段,例子如下,以letsencrypt证书为例(腾讯证书配置参见https://cloud.tencent.com/document/product/400/4143):

upstream tomcat {

server 127.0.0.1:8080 weight=1;

}

server {

server_name xxx.com;

charset utf-8;

   location / {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Connection "";
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
client_max_body_size 100m;
client_body_buffer_size 256k;
proxy_connect_timeout 60;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_buffer_size 8k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass http://tomcat;
} error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

定义后端服务器。这里未举例负载均衡。

upstream tomcat {

server 127.0.0.1:8080 weight=1;

}

定义虚拟服务器

这里主要需要注意ssl证书的配置,以及代理部分的header设置,其影响tomcat端获取真实客户端ip、请求协议等,在未恰当配置的情况下可能造成获取的ip是nginx服务器的ip以及造成资源跨域问题。

tomcat配置

在nginx配置ssl证书的情况下,tomcat配置文件中不需要另行配置ssl证书,但需要获取nginx设置的请求头等信息。以下配置中的端口视具体情况配置。

将原本注释的8080 Connector恢复,并改成如下

<Connector port="8080" protocol="HTTP/1.1"

connectionTimeout="20000"

redirectPort="443"

proxyPort="443"/>

在host标签内添加如下(如果已存在则忽略)

其中%h用于显示客户端ip,但是在代理模式下显示的是nginx的ip,可考虑将 "%h" 修改为 "%{X-Real-IP}i" 即可获取到nginx请求头中已配置的客户端ip。

<Valve className="org.apache.catalina.valves.AccessLogValve"

directory="logs"

prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t %r %s %b" />

获取真实ip,其中httpsServerPort在nginx使用默认443端口时不需添加,否则应当指定。

<Valve className="org.apache.catalina.valves.RemoteIpValve"

remoteIpHeader="x-forwarded-for"

remoteIpProxiesHeader="x-forwarded-by"

protocolHeader="x-forwarded-proto"

protocolHeaderHttpsValue="https" httpsServerPort="7001"

/>

最新文章

  1. Python 2.7 - CentOS 7 - ImportError: No module named Tkinter
  2. Cef 架构
  3. 【C语言】C语言数据类型
  4. python matplotlib 绘图
  5. LINUX如何查看其他用户的操作
  6. MySQL的varchar定义长度到底是字节还是字符
  7. 标准简单SP模板(oracle)
  8. 转:C4项目中验证用户登录一个特性就搞定
  9. android 拍照 onCreate() 调用两次的问题
  10. UITabBarItem
  11. 【转】在Spring中基于JDBC进行数据访问时怎么控制超时
  12. CJOJ 1943 【重庆八中模拟赛】寻找代表元(二分图最大匹配)
  13. Python自建logging模块
  14. java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.classes.views.index_jsp 问题解决方法
  15. linux系统下完全卸载Jenkins
  16. centos7 安装 redis-4.0.9
  17. 第4章 初识STM32
  18. LinuxMint下的Orionode源码安装
  19. python中交换两个值的方法
  20. Rsync启动停止脚本

热门文章

  1. python+opencv-&gt;边缘提取与各函数参数解析
  2. 教妹学Java:Spring 入门篇
  3. TEA5676 + AT24C08 FM收音机 搜台 存台 mmap 实现读写
  4. OpenGL 实现视频编辑中的转场效果
  5. DEV GridControl控件使用(CheckBox全选、操作按钮、事件处理,获取值)
  6. TCP报文首部
  7. RAC修改VIP地址
  8. 大龄IT人的新的一年
  9. OpenCV3入门(十四)图像特效—挤压、哈哈镜、扭曲
  10. C++ 别踩白块小游戏练习