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

Thoughts on truncation of multi-line text with a "show more" button

I just happened to encounter this small requireme...

JavaScript to achieve slow motion animation effect

This article shares the specific code for JavaScr...

Install docker offline by downloading rpm and related dependencies using yum

You can use yum to install all dependencies toget...

A useful mobile scrolling plugin BetterScroll

Table of contents Make scrolling smoother BetterS...

CSS Pick-up Arrows, Catalogs, Icons Implementation Code

1. CSS Miscellaneous Icons There are three ways t...

jQuery implements shopping cart function

This article example shares the specific code of ...

Encapsulation method of Vue breadcrumbs component

Vue encapsulates the breadcrumb component for you...

Detailed steps to install Nginx on Linux

1. Nginx installation steps 1.1 Official website ...

A brief discussion on several specifications of JS front-end modularization

Table of contents Preface The value of front-end ...

Mysql GTID Mha configuration method

Gtid + Mha + Binlog server configuration: 1: Test...

Detailed process of installing and deploying onlyoffice in docker

0. System requirements CPU I5-10400F or above Mem...

TypeScript enumeration basics and examples

Table of contents Preface What are enums in TypeS...

A brief discussion on order reconstruction: MySQL sharding

Table of contents 1. Objectives 2. Environmental ...

Detailed explanation of how to reduce memory usage in MySql

Preface By default, MySQL will initialize a large...