Solve the problem of Nginx returning 404 after configuring proxy_pass

Solve the problem of Nginx returning 404 after configuring proxy_pass

1. Troubleshooting and locating the problem of Nginx returning 404 after configuring proxy_pass

1.1. Problem

In a production configuration involving multiple forwardings, the requirement is the following diagram:
After configuring proxy_pass, requesting www.djx.com directly returns 404 without any other exceptions. However, when we directly request the backend www.baidu.com, it responds normally. This is very strange. Looking at the log, the request is also forwarded to www.baidu.com. But the request response is 404.

1.2. Find the cause of the problem

Our default Nginx proxy_set_header configuration is

proxy_set_header Host $host;
  • Server: 192.168.2.189
  • Server 1: 192.168.1.180 Nginx1
  • Server 2: 192.168.1.90 Nginx2

When we use this setting, when the first layer of Nginx (Nginx1) is proxying, the domain name we request is www.djx.com, and the host value obtained from the header of this request is www.djx.com . We configure

proxy_set_header Host $host;

The host value is set to the forwarded Host value, but the requested domain name is www.baidu.com , that is, the host field in the header is www.djx.com. This is caused by the inconsistency between the requested domain name and the Host in the header.

1.3. proxy_set_header official information

Official Documentation

The default setting is

proxy_set_header Host $proxy_host;
proxy_set_header Connection close;

1.4 Solution

The value of Host is set to $proxy_host . See the following extension for details on the value of $proxy_host .

proxy_set_header Host $proxy_host;

2. Expanding Common Configurations

1. $proxy_host

proxy_set_header Host $proxy_host; 
#Default configuration# As the name implies, the request header is set to the domain name behind the proxy.

Example 1:
When we configure upstream, the value of $proxy_host is the name of the upstream.

upstream open-hz8443{
server 10.60.6.184:8000 max_fails=1 fail_timeout=3s weight=10;
}

Then the value of $proxy_host here is open-hz8443.

Example 2:
When we do not configure upstream, the value of $proxy_host is the address ip and port after proxy_pass . 10.60.6.184:8000 . If proxy_pass http://www.djx.com:8000; then the value of $proxy_host is www.djx.com:8000 .

location ^~ /wss/v1
{
 proxy_pass http://10.60.6.184:8000;
 proxy_set_header Host $proxy_host;
 proxy_set_header Connection "upgrade";
 proxy_set_header Upgrade $http_upgrade;
 tcp_nodelay on; 
 
}

2. $host

proxy_set_header Host $host;
# When the field is not in the request header, it cannot be passed. In this case, you can assign the value to the Host variable by setting the Host variable.

When there is no Host value in the request header, the value of server_name is directly used to fill it.

When the Host value in the request header is received, the Host value in the request header is directly used.

3. $host:$proxy_port

proxy_set_header Host $host:$proxy_port;

# The server name and port are passed together through the proxy server. Compared with the previous item, there is an additional $proxy_port. This $proxy_port is the port in proxy_pass. If there is no port, such as 80 and 443. It will also use 80/443 filling. 

Example: 
proxy_pass http://www.baidu.com;

$host:$proxy_port = Baidu ip:80

4. $http_host

proxy_set_header Host $http_host; 
# A non-changing "Host" request header field can be passed as follows:

When there is no Host value in the request header, the value of server_name is directly used to fill it. And add the port. If it is 80/443, do not add it. In fact, it is to request the value in the url. http://server:port/v1

When the Host value in the request header is received, the Host value in the request header is directly used.

Example

  • Server: 192.168.2.189
  • Server 1: 192.168.1.180 Nginx1
  • Server 2: 192.168.1.90 Nginx2

Basic Configuration
192.168.1.180 Nginx1

server{
 listen 80;
 server_name www.djx.com;
 
 location / {
  proxy_pass http://www.baidu.com/;
 }
 
}

192.168.1.190 Nginx2

server{
 listen 80;
 server_name www.baidu.com;
 
 location / {
  proxy_pass http://192.168.1.80:8080/;
 }
 
}

** Using Basic Configuration **
That is

proxy_set_header Host $proxy_host;

Then the value of the header to http://192.168.1.80:8080/ is www.baidu.com.

** Using $host **
That is

proxy_set_header Host $host;

Reference article: https://cloud.tencent.com/developer/article/1557504

This is the end of this article about the 404 error after Nginx configuration proxy_pass. For more relevant content about Nginx configuration proxy_pass, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of the mechanism and implementation of accept lock in Nginx
  • Solution to Nginx SSL certificate configuration error
  • Nginx 502 Bad Gateway Error Causes and Solutions
  • Proxy_pass method in multiple if in nginx location
  • Nginx configuration sample code for downloading files
  • Implementation of nginx multiple locations forwarding any request or accessing static resource files
  • How to view nginx configuration file path and resource file path
  • Detailed explanation of the implementation of nginx process lock

<<:  HTML form tag tutorial (5): text field tag

>>:  Practical tutorial on modifying MySQL character set

Recommend

Detailed explanation of three ways to connect Docker containers to each other

There are three ways to interconnect and communic...

How to add a column to a large MySQL table

The question is referenced from: https://www.zhih...

CocosCreator Getting Started Tutorial: Making Your First Game with TS

Table of contents premise TypeScript vs JavaScrip...

Steps to build the vite+vue3+element-plus project

Use vite to build a vue3 project You can quickly ...

Detailed explanation of this pointing problem in JavaScript

Preface Believe me, as long as you remember the 7...

Vue implements verification code countdown button

This article example shares the specific code of ...

Four categories of CSS selectors: basic, combination, attribute, pseudo-class

What is a selector? The role of the selector is t...

HTML code to add icons to transparent input box

I was recently writing a lawyer recommendation we...

How to deploy kafka in docker

Table of contents 1. Build Docker 2. Enter the co...

Awk command line or script that helps you sort text files (recommended)

Awk is a powerful tool that can perform some task...

Detailed explanation of ECharts mouse event processing method

An event is an action performed by the user or th...

How to install SVN server under Linux

1. Yum installation yum install subversion 2. Con...

Install mysql 5.6 from yum source in centos7.4 system

System environment: centos7.4 1. Check whether th...