nginx反向代理配置與測試

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

該博客只講配置和測試。

1、在本機下載tomcat,運行起來


2、在tomcat的apache-tomcat-8.5.24\webapps\ROOT目錄下新建test.jsp文件,內容如下

<%--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this 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 with
the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed 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 and
limitations 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 href="favicon.ico" rel="icon" type="image/x-icon" />
        <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <link 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地址。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章