当前位置: 首页 > news >正文

Nginx反向代理Tomcat实战指南

在生产环境中,直接将Tomcat服务器暴露给外部网络并不是最佳实践。通过使用反向代理服务器(如Nginx或Apache HTTP Server),可以提高安全性、性能和灵活性。本文将介绍如何结合Nginx作为反向代理来部署Tomcat应用。

结合反向代理实现 Tomcat 部署

可以利用iptables策略实现端口重定向或DNAT,解决非标准端口的问题

iptables -t nat -A PREROUTING -p tcp --dport 80 --j REDIRECT --to-port 8080

但无法实现负载均衡

常见部署方式介绍

在这里插入图片描述

standalone模式,Tomcat单独运行,直接接受用户的请求,不推荐
反向代理,单机运行,提供了一个Nginx作为反向代理,可以做到静态由nginx提供响应,动态jsp 代理给Tomcat

  • LNMT:Linux + Nginx + MySQL + Tomcat
  • LAMT:Linux + Apache(Httpd)+ MySQL + Tomcat
    前置一台Nginx,给多台Tomcat实例做反向代理和负载均衡调度,Tomcat上部署的纯动态页面更适合
  • LNMT:Linux + Nginx + MySQL + Tomcat
    多级代理
  • LNNMT:Linux + Nginx + Nginx + MySQL + Tomcat

利用 Nginx 反向代理至同一个主机的 Tomcat

配置说明
在这里插入图片描述
利用nginx反向代理功能,实现上图的代理功能,将用户请求全部转发至指定的同一个tomcat主机
利用nginx指令proxy_pass 可以向后端服务器转发请求报文,并且在转发时会保留客户端的请求报文中的 host首部

[root@ubuntu2404 ~]#apt install -y nginx
[root@ubuntu2404 ~]#vim /etc/nginx/conf.d/www.caoge.conf
upstream tomcat {server 192.168.1.30:8080;server 192.168.1.40:8080;}
server{listen 80;server_name www.caoge.com;location / {proxy_pass http://tomcat;
}
}
[root@ubuntu2404 ~]#nginx -t
[root@ubuntu2404 ~]#systemctl restart nginx
[root@ubuntu2404 ~]#vim /etc/hosts
192.168.1.20 www.caoge.com

实战案例1:实现 HTTP

环境说明

一台主机,实现nginx和tomcat
tomcat上有两个新机器当node1,node2
[root@nginx ~]#apt install nginx[root@node1 ~]#apt install -y tomcat10
[root@node1 ~]#apt install -y openjdk-21-jdk
[root@node2 ~]#apt install -y tomcat10
[root@node2 ~]#apt install -y openjdk-21-jdk#修改nginx.conf配置文件
[root@nginx ~]#vim /etc/nginx/conf.d/tomcat.conf
server{listen 80;server_name node1.caoge.com;location / {proxy_pass http://192.168.1.70:8080;     
}
}
[root@nginx ~]#systemctl restart nginx.service [root@nginx ~]#vim /etc/hosts
192.168.1.60 node1.caoge.com
[root@nginx ~]#curl http://node1.caoge.com
node1[root@nginx ~]#vim /etc/nginx/conf.d/tomcat.conf
server{listen 80;server_name node1.caoge.com;location / {proxy_pass http://192.168.1.80:8080;     
}
}
[root@nginx ~]#systemctl restart nginx.service [root@nginx ~]#curl http://node1.caoge.com
node2
[root@nginx ~]#vim /etc/nginx/conf.d/tomcat.conf
upstream tomcat {server 192.168.1.80:8080;server 192.168.1.70:8080;}       
server{listen 80;server_name node1.caoge.com;
location / {proxy_pass http://tomcat;           
}
}
[root@nginx ~]#systemctl restart nginx.service 
[root@nginx ~]#curl http://node1.caoge.com
node2
[root@nginx ~]#curl http://node1.caoge.com
node1

实战案例2:实现 HTTPS

tomcat 实现 https的参考文档

https://help.aliyun.com/document_detail/98576.html?spm=5176.b657008.0.0.5a471b48Cyahpi

虽然在Tomcat上可以实现HTTPS,不仅配置复杂,而且会导致性能问题,因此生产中更多的是通过Nginx 实现HTTPS再反向代理至Tomcat

Nginx实现反向代理实现 http 自动跳转至 https

server {listen 80;server_name blog.wang.org;return 302 https://$host$request_uri;
}
server {listen 443 ssl;server_name blog.wang.org;ssl_certificate   /etc/nginx/ssl/www.wang.org.pem;ssl_certificate_key /etc/nginx/ssl/www.wang.org.key;location / {proxy_pass http://127.0.0.1:8080;proxy_set_header Host  $http_host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
}

Nginx实现反向代理实现 http 自动跳转至 https 并同时实现动静分离

http://www.lryc.cn/news/622135.html

相关文章:

  • 测控一体化闸门驱动灌区信息化升级的核心引擎
  • C++设计模式:类间关系
  • 自定义数据集(pytorchhuggingface)
  • cut、tr、sort 和 uniq 生产典型示例
  • 微服务的编程测评系统11-jmeter-redis-竞赛列表
  • Nginx反向代理与缓存实现
  • 【论文解读】DDRNet:深度双分辨率网络在实时语义分割中的结构与原理全面剖析
  • 51单片机-驱动蜂鸣器模块教程
  • 开源数据发现平台:Amundsen Frontend Service 安装 开发者指南
  • debian13 安装过程 root配置
  • 从 LLM 到自主 Agent:OpenCSG 打造开源 AgenticOps 生态
  • Linux网络基础概念
  • 【RTOS】RT-Thread 进程间通信IPC源码级分析详解
  • [Pyro] 基础构件 | 随机性sample | 可学习参数param | 批量处理plate
  • 【3D图像技术分析及实现】3DGS与深度学习网络结合以实现跨场景迁移的研究调研
  • 电力系统之常见基础概念
  • 【秋招笔试】2025.08.15饿了么秋招机考-第二题
  • [激光原理与应用-285]:理论 - 波动光学 - 无线电磁波的频谱分配
  • [激光原理与应用-287]:理论 - 波动光学 - 电磁波既能承载能量,又能承载信息?
  • 力扣(接雨水)——单调栈
  • 在 Linux 服务器搭建Coturn即ICE/TURN/STUN实现P2P(点对点)直连
  • Vim 常用快捷键及插件
  • 力扣top100(day04-05)--堆
  • [Linux]双网卡 CentOS 系统中指定网络请求走特定网卡的配置方法
  • 微服务容错与监控体系设计
  • 基于Selenium的web自动化框架
  • 另类pdb恢复方式-2
  • 机器学习中的PCA降维
  • 【GPT入门】第47课 大模型量化中 float32/float16/uint8/int4 的区别解析:从位数到应用场景
  • ifcfg-ens33 配置 BOOTPROTO 单网卡实现静态和dhcp 双IP