Home Backend Development PHP Tutorial nginx front-end, tomcat back-end server obtains the real IP of the customer, including the configuration of tomcat access log to obtain the real IP

nginx front-end, tomcat back-end server obtains the real IP of the customer, including the configuration of tomcat access log to obtain the real IP

Jul 28, 2016 am 08:29 AM
nginx proxy quot tomcat


在安装完以nginx+tomcat的WEB服务器,使用默认的配置,会导致服务器上的日志文件,只有nginx日志能获取到客户的真实IP,而tomcat以及上面的JAVA WEB应用均不能正常获取到真正的IP地址,而仅是LOOP(回还地址127.0.0.1,或者0.0.0.0.0.0.1),会导致存入到数据库的也是如此,通过以下配置,即可以改善结果。

nginx端配置文件/etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name localhost;
    location /{
                        rewrite ^/web(.*)$ /$1 last;
                        proxy_pass http://localhost:8080/web/;
                        proxy_cookie_path /web /;
#以下三个proxy_set_header配置项是重点
                        proxy_set_header Host $host;
                        proxy_set_header X-Real-IP $remote_addr;
                        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}
Copy after login

tomcat日志配置文件$CATALINA_HOME/conf/server.xml

在host中,修改以下内容

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
       prefix="localhost_access_log." suffix=".txt"
       pattern="%{X-Real-IP}i %l %u %t %D "%r" %s %b" />
Copy after login
其中X-Real-IP与NGINX中配置的要对应,此变量即是客户的真实IP,pattern的其它参数,请参考官方:http://tomcat.apache.org/tomcat-7.0-doc/config/valve.html#Access_Logging
添加以下valve

<Valve className="org.apache.catalina.valves.RemoteIpValve"
	   internalProxies="127\.0\.0\.1"
	   remoteIpHeader="x-forwarded-for"
	   remoteIpProxiesHeader="x-forwarded-by"
	   trustedProxies="127\.0\.0\.1"/>
Copy after login

以上就介绍了 nginx前端,tomcat后端服务器获取客户的真实IP,包括tomcat访问日志获取真实IP的配置,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to deploy multiple projects in tomcat How to deploy multiple projects in tomcat Apr 21, 2024 am 09:33 AM

To deploy multiple projects through Tomcat, you need to create a webapp directory for each project and then: Automatic deployment: Place the webapp directory in Tomcat's webapps directory. Manual deployment: Manually deploy the project in Tomcat's manager application. Once the project is deployed, it can be accessed by its deployment name, for example: http://localhost:8080/project1.

Where is the root directory of the tomcat website? Where is the root directory of the tomcat website? Apr 21, 2024 am 09:27 AM

The Tomcat website root directory is located in Tomcat's webapps subdirectory and is used to store web application files, static resources, and the WEB-INF directory; it can be found by looking for the docBase attribute in the Tomcat configuration file.

How to run two projects with different port numbers in tomcat How to run two projects with different port numbers in tomcat Apr 21, 2024 am 09:00 AM

Running projects with different port numbers on the Tomcat server requires the following steps: Modify the server.xml file and add a Connector element to define the port number. Add a Context element to define the application associated with the port number. Create a WAR file and deploy it to the corresponding directory (webapps or webapps/ROOT). Restart Tomcat to apply changes.

How to run html and jsp on tomcat How to run html and jsp on tomcat Apr 21, 2024 am 09:04 AM

Tomcat can run HTML and JSP. The method is as follows: copy the HTML file to the corresponding subdirectory of the Tomcat directory and access it in the browser. Copy the JSP file to the corresponding subdirectory of the Tomcat directory, and use the <%@ page %> directive to specify the Java code and access it in the browser.

How to generate URL from html file How to generate URL from html file Apr 21, 2024 pm 12:57 PM

Converting an HTML file to a URL requires a web server, which involves the following steps: Obtain a web server. Set up a web server. Upload HTML file. Create a domain name. Route the request.

Reasons for garbled characters in tomcat Reasons for garbled characters in tomcat Apr 21, 2024 am 10:18 AM

Reasons for Tomcat garbled characters: 1. Character set mismatch; 2. HTTP response header is not set correctly; 3. Filter or encoder configuration error; 4. Web page encoding is incorrect; 5. Other reasons (including server-side language, database encoding and proxy server issues).

How to add a server in eclipse How to add a server in eclipse May 05, 2024 pm 07:27 PM

To add a server to Eclipse, follow these steps: Create a server runtime environment Configure the server Create a server instance Select the server runtime environment Configure the server instance Start the server deployment project

How to configure domain name in tomcat How to configure domain name in tomcat Apr 21, 2024 am 09:52 AM

To configure Tomcat to use a domain name, follow these steps: Create a server.xml backup. Open server.xml and add the Host element, replacing example.com with your domain name. Create an SSL certificate for the domain name (if required). Add an SSL connector in server.xml, change the port, keystore file, and password. Save server.xml. Restart Tomcat.

See all articles