ngnix simple forward request for server and location configuration details

  • 2020-05-17 07:39:41
  • OfStack

Simply comb 1 about the configuration of server and location in nginx.

Such as URL: www mask_dev2. com: 9999 / login /

The first half of the server tube, www.mask_dev2.com :9999

The location tube is the latter part, that is, /login/

Multiple server can be configured with one nginx.

Multiple location can be configured per server.

The first half of URL controls which server to choose, the second half controls which location to choose, and finally decides where to ask for it.

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, request the address of nginx, which must be the server where the requested nginx is located, that is, ip is fixed.

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

So how does the current server correspond to multiple domain names? This one only needs to be added to the corresponding dns server. For example, temporarily use the local machine as dns server and modify 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. Perfect match
2, the wildcard character in front, such as *.test.com
3, the latter, such as www.test.*
4. Regular matching, such as ~^\.www \.test \.com $

If they don't match

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

location configuration

Once you find server, go to the specific location


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

Grammar rules:

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

The beginning of = indicates an exact match ^~ means that uri begins with some regular string, which is interpreted as matching the url path. nginx does not encode url, so the request is /static/20%/aa, which can be matched by the rule ^~ static /aa (note space). The ~ beginning represents a case-sensitive regular match The ~* beginning represents a case-insensitive regular match ! And! * are case - sensitive and case - insensitive, respectively General match, any request will be matched. In the case of multiple location configurations, the matching order is as follows (reference materials are provided, which have not been actually verified, just try to find out, no need to be rigid, just for reference) :

First match =, then match ^~, then regular match in the order in the file, and finally give/universal match. When there is a successful match, stop the match and process the request according to the current match rule.

But 1 is not as complicated as that. It has 3 points.

Default request. Page request. Background logic requests.

# Directly matching the website root, through the domain name to visit the website home page more frequently, using this will speed up the processing, the official website said.  
# This is directly forwarded to the back-end application server, or it could be 1 Static home page  
#  The first 1 Three mandatory rules  
location = / { 
  proxy_pass http://tomcat:8080/index 
} 

#  The first 2 The required rule is to handle static file requests, which is nginx As a http Server strengths  
#  There are two configuration patterns, directory matching or suffix matching , Choose the 1 Or in combination  
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ { 
  root /webroot/res/; 
} 

# The first 3 These rules are generic rules for forwarding dynamic requests to back-end application servers  
# Non-static file request is the default dynamic request, according to the actual grasp  
# After all, the current 1 Some frames are popular with tape .php,.jsp Suffixes are rare  
location / { 
  proxy_pass http://127.0.0.1:8080/ 
} 

conclusion

For example, starting both the foreground system and the background system now allows you to use two server(you can configure host to be api,admin, or you can modify the port directly), three location in each server to determine the request for a specific page.


Related articles: