Get the App
SLTechnology News&Howtos  ›  Servers  › 

What if nginx doesn't jump to the upstream address?

Shulou Source: shulou.com Published: 2022-06-01 16:30:47 09月14日 Update

This article mainly introduces how nginx does not jump to upstream address, has certain reference value, interested friends can refer to, I hope you have a lot of gains after reading this article, let Xiaobian take you to understand.

preface

In nginx encountered a very strange problem, in the front tomcat jump page when the jump is upstream address, directly reported 404, but some page access is normal.

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

If it is normal to directly access the intranet ip, 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 is found that because in the backend java code, this address is redirected, which uses request.getServerPort(). If it is through nginx, the correct port of the front end cannot be obtained. The default return is still 80. If the port of nginx listening is not 80 by default, 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 proxy_set_header of the nginx configuration file

proxy_set_header Host $host:$proxy_port; Thank you for reading this article carefully, I hope Xiaobian shared "nginx did not jump to the upstream address how to do" This article is helpful to everyone, but also hope that everyone a lot of support, pay attention to the industry information channel, more relevant knowledge waiting for you to learn!

Tags: Address article how to do front end port problem page configuration code value interest preface slogan at the same time file method time is in more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB MySQL Redmi vpn Huawei