Nginx handles http request implementation process analysis

Nginx handles http request implementation process analysis

Nginx first decides which server{} block in the configuration file to use for processing. Assume the following server{} configuration

server {
  listen 80;
  server_name aaa;
  ...
}

server {
  listen 80;
  server_name bbb;
  ...
}

Nginx will determine which server to use based on the value in the Host field in the incoming http request header{}.

If there is no Host field in the request header, or the value in the Host field does not match the {server_name} in the server{} in the Nginx configuration file, the first server{} is used to process the request.

If the value in the Host field in the request header matches the {server_name} in a server{} in the Nginx configuration file, this server{} is used to process the request.

You can use the curl tool to easily do experiments. curl can set the request header of the http request, so you can set the Host field arbitrarily, using [-H] to set it. The 10.210.65.73 below is the IP address of the machine where nginx is installed.

So using the following command, after sending the http request, nginx will use server{server_name aaa} to process the request.

curl.exe -H "Host: aaa" 10.210.65.73

Very important conclusion: server_name corresponds to the value of the Host field in the http request header. With the above theoretical support, it is easy to set up reverse proxy and load balancing:

When the Host field in the incoming http request header is aaa, storage.test will handle it.

When the Host field in the incoming http request header is bbb, tracker.test will handle it.

  #Load balancing configuration, the machine with IP 129 has a high configuration, so the number 27 is given to it to let it handle more upstream storage.test {
   server 10.210.65.129:80 weight=27;
   server 10.210.65.130:80 weight=1;
  }

  #Load balancing configuration upstream tracker.test {
   server 10.210.65.52:80 weight=7;
   server 10.210.65.53:80 weight=2;
  }

  #File storage server {
    listen 80;
    server_name aaa;
    location / {
      #The content after http::// is self-defined, corresponding to the upstream name proxy_pass http://storage.test above;
    }
  }

  #File server tracker
  server {
    listen 80;
    server_name bbb;

    location / {
      #The content after http::// is self-defined, corresponding to the upstream name proxy_pass http://tracker.test;
    }

  }

Whose port is the listen in server{} listening on?

What is being listened to is: the port of the process (mostly browsers) that sends the http request (if it is an http request, the port is 80), not the port of the nginx server's own process.

Nginx decides which server to use to handle the http request based on the value in the Host field of the http request header and the port of the process that sends the http request (mostly the browser).

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 analysis of each stage of nginx's http request processing
  • Nginx implements https website configuration code example
  • Detailed tutorial on configuring nginx for https encrypted access
  • Implementation of Nginx domain name forwarding https access
  • Alibaba Cloud Nginx configures https to implement domain name access project (graphic tutorial)
  • Nginx configures the same domain name to support both http and https access
  • Detailed configuration of Nginx supporting both Http and Https
  • Use nginx + secondary domain name + https support

<<:  An example of elegant writing of judgment in JavaScript

>>:  Mysql Sql statement exercises (50 questions)

Recommend

Implementation of Docker deployment of ElasticSearch and ElasticSearch-Head

This article mainly explains how to deploy Elasti...

A simple way to put HTML footer at the bottom of the page

Requirement: Sometimes, when the page content is ...

Docker container exits after running (how to keep running)

Phenomenon Start the Docker container docker run ...

How to solve jQuery conflict problem

In front-end development, $ is a function in jQue...

Installation tutorial of mysql8.0rpm on centos7

First, download the diagram 1. First uninstall th...

TypeScript problem with iterating over object properties

Table of contents 1. Problem 2. Solution 1. Decla...

About the problem of dynamic splicing src image address of img in Vue

Let's take a look at the dynamic splicing of ...

Talk about nextTick in Vue

When the data changes, the DOM view is not update...

A small question about the execution order of SQL in MySQL

I encountered a sql problem at work today, about ...

border-radius is a method for adding rounded borders to elements

border-radius:10px; /* All corners are rounded wi...