首页 > 编程知识 正文

nginx反向代理配置文件,nginx反向代理配置教程

时间:2023-05-04 03:37:44 阅读:282810 作者:1124

反向代理和正向代理概念以及原理参见博客:https://blog.csdn.net/permike/article/details/52229507

该博客只讲配置和测试。

1、在本机下载tomcat,运行起来


2、在tomcat的apache-tomcat-8.5.24webappsROOT目录下新建test.jsp文件,内容如下

<%--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.--%><%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %><%java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");request.setAttribute("year", sdf.format(new java.util.Date()));request.setAttribute("tomcatUrl", "http://tomcat.apache.org/");request.setAttribute("tomcatDocUrl", "/docs/");request.setAttribute("tomcatExamplesUrl", "/examples/");%><!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" /> <title><%=request.getServletContext().getServerInfo() %></title> <link rel="external nofollow" rel="external nofollow" href="favicon.ico" rel="icon" type="image/x-icon" /> <link rel="external nofollow" rel="external nofollow" href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> <link rel="external nofollow" href="tomcat.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="wrapper"> <div id="navigation" class="curved container"><span id="nav-home">The remote server IP is:<%=request.getHeader("X-real-ip")%></span><br/> <span id="nav-home">The nginx server IP is:<%=request.getRemoteAddr() %></span> <br class="separator" /> </div> </div> </body></html>

解释:主要是如下两行代码,分别获取自定义变量和nginx服务器所在IP。自定义变量"X-real-ip"定义在nginx配置文件中。

<span id="nav-home">The remote server IP is:<%=request.getHeader("X-real-ip")%></span><br/><span id="nav-home">The nginx server IP is:<%=request.getRemoteAddr() %></span>

3、修改nginx配置文件,在默认的80端口的配置下,增加location配置并保存

#配置反向代理tomcat服务器:拦截.jsp结尾的请求转向到tomcat, #表示对.进行转义 location ~ .jsp$ {#设置客户端真实ip地址,X-real-ip $remote_addr表示在头部设置键值对,#在测试页可以通过如下方式获取:<%=request.getHeader("X-real-ip")%> proxy_set_header X-real-ip $remote_addr; proxy_pass http://192.168.248.100:8080; }

解释:上述IP地址192.168.248.100是tomcat服务器(物理机)所在的地址,因为这里使用的是虚拟机进行测试的,所以配置的是VMware Network Adapter VMnet8的地址。至于虚拟机和主机怎么通信,参见博客:

https://blog.csdn.net/BruceLeeNumberOne/article/details/80102546

4、重启nginx服务

./nginx –s reload

5、分别在虚拟机内部和宿主机访问nginx的80端口

在虚拟机内部访问:


解释:上述显示的两个IP,都是WMware里面安装的ubuntu的IP,因为nginx安装在ubuntu中,所以两个IP地址相等。

6、在宿主机(安装VMware的机器)上访问nginx的80端口


解释:注意第5步和第6步骤打印的IP地址的差别。上述remote server IP是VMware Network Adapter VMnet8的IP地址;server IP是VMware里安装的ubuntu的真实IP,也就是nginx服务器所在的IP地址。

7、总结

反向代理的作用是隐藏客户端的IP,经过nginx的转发,客户端获取到的远程IP始终是nginx服务器的IP地址。但是仍然可以通过上面设置自定义变量的方式,获取到实际客户端的IP地址。





版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。