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

5 common scenarios and examples of JavaScript destructuring assignment

Table of contents Preface 1. Extract data 2. Alia...

Summary of commonly used escape characters in HTML

The commonly used escape characters in HTML are s...

How to use css overflow: hidden (overflow hiding and clearing floats)

Overflow Hide It means hiding text or image infor...

How to operate Linux file and folder permissions

Linux file permissions First, let's check the...

Linux installation Redis implementation process and error solution

I installed redis today and some errors occurred ...

Detailed code examples of seven methods for vertical centering with CSS

When we edit a layout, we usually use horizontal ...

React ref usage examples

Table of contents What is ref How to use ref Plac...

The difference between this.$router and this.$route in Vue and the push() method

The official document states: By injecting the ro...

Mybatis fuzzy query implementation method

Mybatis fuzzy query implementation method The rev...

Introduction to Linux environment variables and process address space

Table of contents Linux environment variables and...

Web page experience: planning and design

1. Clarify the design direction <br />First,...