在location中配置反向代理
例如:客户端访问:http://192.168.1.67:80/test.jsp
#配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat
server {
listen 80;
server_name localhost;
location ~ \.jsp$ {
#设置真实的访问IP( X-real-ip名称可随意配置,访问的请求接收使用此名称获取真实IP)
proxy_set_header X-real-ip $remote_addr;
#代理请求的URI转发到指定的web服务器
proxy_pass http://192.168.1.67:8080;
}
}
web服务器(tomcat)
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Test Page</title> </head> <body> Test1 Page!!!<br/> <!-- request.getHeader("X-real-ip") 是实际地址 --> remote ip : <%=request.getHeader("X-real-ip") %> <br/> <!-- request.getRemoteAddr() 输出的是Nginx代理服务器地址--> nginx server ip : <%=request.getRemoteAddr()%> </body> </html>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步