Tomcat obtains the client domain name of Nginx reverse proxy

Tomcat obtains the client domain name of Nginx reverse proxy

question

After Nginx reverse proxy, the Tomcat application obtains the Nginx host through request.getHeader("host") , not the real domain name in the client browser address bar.

For example, on a certain server, Tomcat's port number is 8080, Nginx's port number is 80, and Nginx reverse proxy is port 8080.

server {
  listen 80;
  location / {
    proxy_pass http://127.0.0.1:8080;
  }
}

On another machine, use a browser to open http://haha/test to access the application under Tomcat and obtain the client domain name.

System.out.println(request.getHeader("host"));

turn out:

localhost:8080

Causes of the problem

Nginx's reverse proxy is actually a bridge between the client and the real application server. The client (usually a browser) accesses the Nginx server, and Nginx then accesses the Web application server. For the Web application, the client of this HTTP request is Nginx rather than the real client browser. If no special processing is done, the Web application will regard Nginx as the request client, and the client information obtained is some information about Nginx.

Problem Solving

Nginx configures HTTP Header. Host contains the client's real domain name and port number

proxy_set_header Host $http_host;

Tomcat obtains client information from the HTTP Header passed by Nginx.

<Valve className="org.apache.catalina.valves.RemoteIpValve" />

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Explanation of the steps for Tomcat to support https access
  • Tomcat+Mysql high concurrency configuration optimization explanation
  • Explanation on the use and modification of Tomcat's default program publishing path
  • Idea configures maven-tomcat-plugin to implement project deployment
  • Detailed explanation of the server path method for compiling Maven projects using different tomcats in Intellij idea
  • Detailed steps for importing eclipse projects into IDEA and deploying them to tomcat
  • In-depth explanation of the event mechanism in Tomcat and Spring
  • Solve the problem of Eclipse Tomcat OutOfMemoryError: PermGen space
  • Spring Boot uses Thymeleaf + Gradle to build war to Tomcat
  • Connector configuration in Tomcat

<<:  js implements a simple method of encapsulating jQuery and a detailed explanation of chain operations

>>:  How to bypass unknown field names in MySQL

Recommend

Use and understanding of MySQL triggers

Table of contents 1. What is a trigger? 2. Create...

How to use IDEA to configure tomcat and create JSP files

Before using idea to write JSP files, you need to...

How to implement dual-machine master and backup with Nginx+Keepalived

Preface First, let me introduce Keepalived, which...

Mysql queries the transactions being executed and how to wait for locks

Use navicat to test and learn: First use set auto...

The difference between br and br/ in HTML

answer from stackflow: Simply <br> is suffic...

Discuss the application of mixin in Vue

Mixins provide a very flexible way to distribute ...

Implementation of dynamic particle background plugin for Vue login page

Table of contents The dynamic particle effects ar...

Detailed explanation of jQuery's core functions and event handling

Table of contents event Page Loading Event Delega...

A bug fix for Tomcat's automatic shutdown

Preface Recently, a Java EE web project that has ...

Why should MySQL fields use NOT NULL?

I recently joined a new company and found some mi...

MySQL8.0.18 configuration of multiple masters and one slave

Table of contents 1. Realistic Background 2. Agre...

Detailed summary of web form submission methods

Let's first look at several ways to submit a ...

js realizes two-way data binding (accessor monitoring)

This article example shares the specific code of ...