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

What codes should I master when learning web page design?

This article introduces in detail some of the tech...

How to use vs2019 for Linux remote development

Usually, there are two options when we develop Li...

MySQL uses events to complete scheduled tasks

Events can specify the execution of SQL code once...

How to use JS to implement waterfall layout of web pages

Table of contents Preface: What is waterfall layo...

How to add automatic completion commands for docker and kubectl on Mac

Introduction to kubectl kubectl is a command line...

Page Speed ​​Optimization at a Glance

I believe that the Internet has become an increas...

Summary of JavaScript Timer Types

Table of contents 1.setInterval() 2.setTimeout() ...

Introduction and examples of hidden fields in HTML

Basic syntax: <input type="hidden" na...

How to use ES6 class inheritance to achieve a gorgeous ball effect

Table of contents introduce Implementation steps ...

Implementation of Docker deployment of Nuxt.js project

Docker official documentation: https://docs.docke...

Installation and configuration of mysql 8.0.15 under Centos7

This article shares with you the installation and...

JS implements the snake game

Table of contents 1. Initialization structure 2. ...

Detailed tutorial on installing ElasticSearch 6.x in docker

First, pull the image (or just create a container...

Detailed explanation of Xshell common problems and related configurations

This article introduces common problems of Xshell...