Solution to nginx not jumping to the upstream address

Solution to nginx not jumping to the upstream address

Preface

Today I encountered a very strange problem in nginx. When the front-end tomcat jumped to the page, it jumped to the upstream address and reported 404 directly, but some page accesses were normal.

http://tomcat/tomcat-web/account/index

If the access is normal using the intranet ip directly, it can be determined that it is a problem with nginx. The nginx configuration is as follows

upstream tomcat { 
  server 192.168.11.172:8061; 
  server 192.168.11.172:8062;
  ip_hash;  
}  
 
 server { 
  listen 8060;  
  server_name www.example.com;

  location / { 
    proxy_pass http://tomcat; 
    proxy_set_header Host $host:8060;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    index index.html index.htm; 
  } 
 }

After investigation, it was found that in the backend Java code, this address was redirected, and request.getServerPort() was used. If it was redirected through nginx, the correct front-end port could not be obtained, and the default return was still 80. If the default listening port of nginx is not 80, response.sendRedirect cannot jump to the correct address.

response.sendRedirect(getBasePath(request) + "account/index");
  private String getBasePath(HttpServletRequest request) {
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
        + ":" + request.getServerPort() + path + "/";
    return basePath;
  }

The solution is to add the port number to the nginx configuration file proxy_set_header

proxy_set_header Host $host:$proxy_port;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the solution for NGINX to jump from https to http
  • How to redirect URL using nginx rewrite
  • How to redirect HTTP 301 to a domain name with www in Nginx server
  • How to force nginx to use https access (http jumps to https)
  • How to configure Nginx page redirection according to different browser languages
  • Detailed explanation of nginx to solve the problem of home page jump
  • Detailed explanation of nginx 301 redirect to domain name with www
  • How to redirect to https through nginx load balancing
  • Using Nginx's map command to redirect pages
  • Solve the problem of only redirecting to the home page when deploying thinkPHP 5 with nginx
  • Example code for using Nginx to implement 301 redirect to https root domain name
  • Nginx prohibits direct access via IP and redirects to a custom 500 page
  • Detailed explanation of Nginx rewrite jump application scenarios
  • Detailed explanation of how to enable HSTS in nginx to force the browser to redirect to HTTPS access
  • Implementation of rewrite jump in nginx
  • Detailed explanation of location matching and rewrite redirection in Nginx
  • Nginx hidden redirect (browser URL remains unchanged after redirection)

<<:  Implementation of the function of the vue circular percentage progress bar component

>>:  Analysis of slow insert cases caused by large transactions in MySQL

Recommend

Detailed explanation of the use of MySQL paradigm

1. Paradigm The English name of the paradigm is N...

Detailed explanation of the simple use of MySQL query cache

Table of contents 1. Implementation process of qu...

How to use DPlayer.js video playback plug-in

DPlayer.js video player plug-in is easy to use Ma...

Summarize the common application problems of XHTML code

<br />For some time, I found that many peopl...

About the pitfall record of Vue3 transition animation

Table of contents background Problem location Fur...

Docker deployment RabbitMQ container implementation process analysis

1. Pull the image First, execute the following co...

How to view the docker run startup parameter command (recommended)

Use runlike to view the docker run startup parame...

Detailed explanation of the mechanism and implementation of accept lock in Nginx

Preface nginx uses a multi-process model. When a ...

The functions and differences between disabled and readonly

1: readonly is to lock this control so that it can...

Detailed tutorial on running Tomcat in debug mode in IDEA Maven project

1. Add the following dependencies in pom.xml <...

Two ways to implement square div using CSS

Goal: Create a square whose side length is equal ...

How to connect idea to docker to achieve one-click deployment

1. Modify the docker configuration file and open ...

MySql learning day03: connection and query details between data tables

Primary Key: Keyword: primary key Features: canno...

In-depth analysis of HTML table tags and related line break issues

What is a table? Table is an Html table, a carrie...

jQuery implements time selector

This article example shares the specific code of ...