Detailed explanation of server and location configuration of simple forwarding request of nginx

Detailed explanation of server and location configuration of simple forwarding request of nginx

Let's briefly sort out the configuration of server and location in nginx.

For example, URL: www.mask_dev2.com:9999/login/

The first half of the server pipe, that is: www.mask_dev2.com:9999

Location controls the second half, namely: /login/

One nginx can be configured with multiple servers.

Each server can be configured with multiple locations.

The first half of the URL controls which server is selected, and the second half controls which location is selected, which ultimately determines where the request is made.

Server configuration

server {
 listen 9999;
 server_name www.mask_dev2.cn;
 location / {
  default_type text/html;
  content_by_lua '
   ngx.say("<p>first</p>")
  ';
 }
}

server {
 listen 9999;
 server_name www.mask_dev2.*;
 location / {
  default_type text/html;
  content_by_lua '
   ngx.say("<p>second</p>")
  ';    
 }
}

server {
 listen 9998;
 server_name _;
 location / {
  default_type text/html;
  content_by_lua '
   ngx.say("<p>third</p>")
  ';

 }
}

First of all, the address requested for nginx must be the server where the requested nginx is located, that is, the IP address is fixed.

In other words, it doesn't matter what server_name is, it refers to the current server.

So how does the current server correspond to multiple domain names? This only needs to be added in the corresponding DNS server. For example, temporarily treat the local machine as a DNS server and modify the hosts

127.0.0.1 localhost
127.0.0.1 www.mask_dev2.cn
127.0.0.1 www.mask_dev2.com

Server matching order

The matching priority of server_name and host is as follows:

1. Exact match
2. Wildcard first, such as *.test.com
3. After the first one, such as www.test.*
4. Regular matching, such as ~^\.www\.test\.com$

If none of them match

1. Prefer default or default_server after the listen configuration item
2. Find the first server block that matches the listen port

Location configuration

After finding the server, find the specific location

server {
 listen 9998;
 server_name _;
 location = / { 
  #Rule A 
 } 
 location = /login { 
  #Rule B 
 } 
 location ^~ /static/ { 
  #Rule C 
 } 
 location ~ \.(gif|jpg|png|js|css)$ { 
  #Rule D 
 } 
 location ~* \.png$ { 
  #Rule E 
 } 
 location !~ \.xhtml$ { 
  #Rule F 
 } 
 location !~* \.xhtml$ { 
  #Rule G 
 } 
 location / { 
  #Rule H 
 }

Syntax rules:

location [=||*|^~] uri { … }

  1. = at the beginning indicates an exact match
  2. ^~ at the beginning means that the URI starts with a regular string, which can be understood as matching the URL path. Nginx does not encode the URL, so the request is /static/20%/aa, which can be matched by the rule ^~ static /aa (note the space).
  3. ~ starts with a case-sensitive regular expression
  4. ~* starts with a case-insensitive regular expression
  5. ! and !* are case-sensitive and case-insensitive regular expressions respectively.
  6. / Universal match, any request will be matched.
  7. When multiple locations are configured, the matching order is as follows (based on reference materials, not yet verified in practice, you will know after trying it, don’t be bound by it, it’s for reference only):

First, it matches =, then ^~, then the regular expression in the order in the file, and finally the general match of /. When a match is successful, stop matching and process the request according to the current matching rules.

But it is generally not that complicated, there are 3 points.

  1. Default request.
  2. Page request.
  3. Background logic request.
#Directly match the website root. It is more frequent to access the website homepage through the domain name. Using this will speed up the processing, the official website said. 
#This is forwarded directly to the backend application server, or it can be a static homepage# The first required rule location = / { 
  proxy_pass http://tomcat:8080/index 
} 

# The second mandatory rule is to process static file requests, which is the strength of nginx as an http server. # There are two configuration modes, directory matching or suffix matching, choose one or use both location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { 
  root /webroot/res/; 
} 

#The third rule is a general rule, which is used to forward dynamic requests to the backend application server. #Non-static file requests are dynamic requests by default, and you can grasp it according to your actual situation. #After all, some popular frameworks now rarely have .php and .jsp suffixes. location / { 
  proxy_pass http://127.0.0.1:8080/ 
}

Summarize

For example, if you start the front-end system and the back-end system at the same time, you can use two servers (you can configure the host to be api, admin, or directly change the port), and each server has 3 locations to determine the specific page request.

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.

<<:  MySQL 5.5.27 installation graphic tutorial

>>:  Write a React-like framework from scratch

Recommend

CSS3 Tab animation example background switching dynamic effect

CSS 3 animation example - dynamic effect of Tab b...

A brief discussion on several ways to pass parameters in react routing

The first parameter passing method is dynamic rou...

React passes parameters in several ways

Table of contents Passing parameters between pare...

Detailed steps to install VMware Tools from scratch (graphic tutorial)

VMware Tools is a tool that comes with VMware vir...

JS uses clip-path to implement dynamic area clipping function

background Today, I was browsing CodePen and saw ...

Example code for realizing charging effect of B station with css+svg

difficulty Two mask creation of svg graphics Firs...

React event binding details

Table of contents Class component event binding F...

MySQL 5.7.21 installation and configuration method graphic tutorial (window)

Install mysql5.7.21 in the window environment. Th...

Ubuntu 20.04 Chinese input method installation steps

This article installs Google Input Method. In fac...

Solution to 1067 when Mysql starts in Windows

I just started working a few days ago and install...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

Example of using Docker Swarm to build a distributed crawler cluster

During the crawler development process, you must ...

JavaScript to implement the web version of the snake game

This article shares the specific code for JavaScr...

Let's talk briefly about the changes in setup in vue3.0 sfc

Table of contents Preface Standard sfc writing me...