Thoroughly understand the five application scenarios of Nginx! Go out and brag and don't worry anymore

Time:2022-8-5

One: HTTP server

Nginx itself is also a server for static resources. When there are only static resources, Nginx can be used as the server. If a website is only static pages, it can be deployed in this way.

1, first in the document root directoryDocroot(/usr/local/var/www)Create the html directory under, and then put a test.html in the html;

Thoroughly understand the five application scenarios of Nginx! Go out and brag and don't worry anymore

2. Configure the server in nginx.conf
user mengday staff;

http {
    server {
        listen       80;
        server_name  localhost;
        client_max_body_size 1024M;

        # default location
        location / {
            root   /usr/local/var/www/html;
            index  index.html index.htm;
        }
    }
}
3. Access test

Notice: If there is a 403 Forbidden error when accessing the picture, it may be because the user configuration in the first line of nginx.conf is incorrect. The default is #user nobody; it is a comment, change it to user root under linux; Then reload the configuration file or restart, and try again, the user name can be viewed through the who am i command.

4. Instruction introduction
  • server : used to define services, there can be multiple server blocks in http
  • listen : Specifies the IP address and port where the server listens for requests, if the address is omitted, the server will listen on all addresses, if the port is omitted, the standard port is used
  • server_name : service name, used to configure the domain name
  • location : used to configure the configuration corresponding to the mapping path uri. There can be multiple locations in a server. The location is followed by a uri, which can be a regular expression. / means matching any path. When the path accessed by the client satisfies this uri The code in the location block will be executed
  • root : root path, when accessinghttp://localhost/test.html, "/test.html" will match "/" uri, find the root as /usr/local/var/www/html, the physical address of the resource accessed by the user = root + uri = /usr/local/var/www/html + /test.html=/usr/local/var/www/html/test.html
  • index : Set the home page. When only accessing server_name without any path after it, the index instruction will be followed directly without root; if no specific file is specified in the access path, the resource set by the index will be returned.http://localhost/html/then returns index.html by default
5, location uri regular expression
. : matches any character except newline
? : Repeat 0 or 1 time
+ : Repeat 1 or more times
* : Repeat 0 or more times
\d : match digits
^ : matches the beginning of the string
$ : Match the end of the string
{n} : repeat n times
{n,} : repeat n or more times
[c] : matches a single character c
[az] : matches any of the lowercase letters of az
(a|b|c) : The attribute line means to match any one of the cases, each case is separated by a vertical bar, usually enclosed in parentheses, and matches a string that matches the a character or the b character or the c character
\ backslash: used to escape special characters

The content matched between parentheses () can be referenced later by $1, and $2 represents the content in the second () in front. The most confusing thing in regex is that \ escapes special characters.

2. Static server

Static servers are often encountered in companies, which usually provide an upload function, and other applications obtain static resources from the static server if they need them.

1. Create the images and img directories under /usr/local/var/www respectively, and put a test.jpg in each directory

Thoroughly understand the five application scenarios of Nginx! Go out and brag and don't worry anymore

http {
    server {
        listen       80;
        server_name  localhost;


        set $doc_root /usr/local/var/www;

        # default location
        location / {
            root   /usr/local/var/www/html;
            index  index.html index.htm;
        }

        location ^~ /images/ {
            root $doc_root;
       }

       location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
           root $doc_root/img;
       }
    }
}

Custom variables use the set instruction, syntax set variable name value; reference uses variable name value; reference uses variable name; here the doc_root variable is customized.

There are generally two ways to map static server locations:

  • Use the path, such as /images/ general pictures will be placed in a picture directory,
  • Use suffixes, such as .jpg, .png, etc. to match patterns

accesshttp://localhost/test.jpgwill map to $doc_root/img

accesshttp://localhost/images/test.jpgWhen the same path satisfies multiple locations, the location with higher priority will be matched first. Since the priority of ^~ is greater than ~, it will go to the location corresponding to /images/

Common location path mapping paths are as follows:

  • = does an exact match of ordinary characters. That is, an exact match.
  • ^~ prefix matches. If the match is successful, no other locations will be matched.
  • ~ means to perform a regular match, case sensitive
  • ~* means to perform a regular match, case-insensitive
  • /xxx/ regular string path matching
  • / Universal match, any request will match
location priority

When a path matches multiple locations, which location can be matched has a priority order, and the priority order is related to the expression type of the location value, and has nothing to do with the order in the configuration file. For expressions of the same type, the longer string will be matched first.

Here are the instructions in order of priority:

  • The equals type (=) has the highest precedence. Once the match is successful, no more matches are found and the search stops.
  • ^~ type expression, not a regular expression. Once the match is successful, no more matches are found and the search stops.
  • Regular expression types (~ ~*) have the next highest priority. If there are multiple location regular expressions that can be matched, the one with the longest regular expression is used.
  • Regular string match type. Match by prefix.
  • / Generic match, if there is no match, match the generic

Priority search problem: different types of location mapping decide whether to continue searching downwards

  • Equal sign type, ^~ type: Once matched, the search will stop, and no other locations will be matched.
  • Regular expression type (~ ~*), regular string matching type /xxx/ : After matching, it will continue to search for other locations until the highest priority is found, or the first case is found and the search is stopped

location priority from high to low:

(location =) > (location full path) > (location ^~ path) > (location ~,~* regular sequence) > (location partial starting path) > (/)
location = / {
    # Exact match /, no string after the hostname /
    [ configuration A ]
}
location / {
    # Match all requests starting with /.
    # But if there is a longer expression of the same type, the longer expression is chosen.
    # If there is a regular expression that can be matched, the regular expression will be matched first.
    [ configuration B ]
}
location /documents/ {
    # Match all requests starting with /documents/, after matching, continue to search down.
    # But if there is a longer expression of the same type, the longer expression is chosen.
    # If there is a regular expression that can be matched, the regular expression will be matched first.
    [ configuration C ]
}
location ^~ /images/ {
    # Match all expressions starting with /images/, if the match is successful, stop matching and searching, stop searching.
    # So, even if there is a matching regular expression location, it will not be used
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    # Match all requests ending in gif jpg jpeg.
    # But requests starting with /images/ will use Configuration D, which has higher priority
    [ configuration E ]
}

location /images/ {
    # character matches /images/, and will continue to search down
    [ configuration F ]
}


location = /test.htm {
    root   /usr/local/var/www/htm;
    index  index.htm;
}

Note: The priority of location has nothing to do with the location configured by location

3. Reverse proxy

Reverse proxy should be the most used function of Nginx. Reverse proxy means that the proxy server accepts connection requests on the internet, and then forwards the request to the server on the internal network, and will get the request from the server. The result is returned to the client requesting connection on the Internet, and the proxy server acts as a reverse proxy server to the outside world.

Simply put, the real server cannot be directly accessed by the external network, so a proxy server is needed, and the proxy server can be accessed by the external network and is in the same network environment as the real server, of course, it may be the same server, port Just different.

The reverse proxy is implemented through the proxy_pass directive.

Start a Java Web project with port number 8081

server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:8081;
        proxy_set_header Host $host:$server_port;
        # set user ip address
         proxy_set_header X-Forwarded-For $remote_addr;
         # When the requesting server fails, go to another server
         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; 
    }

}

When we visit localhost, it is equivalent to visiting localhost:8081

Fourth, load balancing

Load balancing is also a commonly used function of Nginx. Load balancing means that it is allocated to multiple operation units for execution, such as Web servers, FTP servers, enterprise key application servers and other mission-critical servers, so as to complete work tasks together.

To put it simply, when there are two or more servers, requests are randomly distributed to the specified servers for processing according to the rules. Generally, the load balancing configuration needs to configure the reverse proxy at the same time, and jump to the load balancing through the reverse proxy. Nginx currently supports 3 load balancing strategies and 2 commonly used third-party strategies.

Load balancing is achieved through upstream directives.

1. RR (round robin: round robin default)

Each request is assigned to different backend servers one by one in chronological order, that is to say, the first request is assigned to the first server, and the second request is assigned to the second server. If there are only two servers, the third The second request continues to be allocated to the first one, and the polling continues, that is, the ratio of the server to receive requests is 1:1. If the back-end server goes down, it can be automatically eliminated. Polling is the default configuration and does not require much configuration

The same project starts the project using ports 8081 and 8082 respectively

upstream web_servers {  
   server localhost:8081;  
   server localhost:8082;  
}

server {
    listen       80;
    server_name  localhost;
    #access_log  logs/host.access.log  main;


    location / {
        proxy_pass http://web_servers;
        # Must specify Header Host
        proxy_set_header Host $host:$server_port;
    }
 }

The access address can still get a responsehttp://localhost/api/user/log…, this way is polling

2. Weight

Specify the polling probability, and the weight is proportional to the access ratio, that is, the ratio of the server to receive requests is the ratio of the respective configured weights. It is used in the case of uneven performance of the back-end server. For example, if the server performance is poor, it will receive fewer requests. Better to handle more requests.

upstream test {
    server localhost:8081 weight=1;
    server localhost:8082 weight=3;
    server localhost:8083 weight=4 backup;
}

The example is that only one of the 4 requests is allocated to the 8081, and the other 3 requests are allocated to the 8082. Backup refers to hot backup, only when 8081 and 8082 are down, go to 8083

3. ip_hash

There is a problem with the above two methods, that is, when the next request comes, the request may be distributed to another server. When our program is not stateless (using session to save data), then there is a big problem. It is very problematic. For example, if the login information is saved in the session, then you need to log in again when you jump to another server, so many times we need a client to access only one server, so we need to use iphash, iphash Each request is allocated according to the hash result of the access ip, so that each visitor fixedly accesses a back-end server, which can solve the problem of session.

upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}
4. fair (third party)

Requests are allocated according to the response time of the backend server, and those with short response times are allocated first. This configuration is for faster response to the user

upstream backend {
    fair;
    server localhost:8080;
    server localhost:8081;
}
5. url_hash (third party)

Allocate requests according to the hash result of accessing URLs, so that each URL is directed to the same back-end server, which is more effective when the back-end server is cached. Add a hash statement to the upstream, and other parameters such as weight cannot be written in the server statement. hash_method is the hash algorithm used

upstream backend {
    hash $request_uri;
    hash_method crc32;
    server localhost:8080;
    server localhost:8081;
}

The above five types of load balancing are applicable to different situations, so you can choose which strategy mode to use according to the actual situation, but fair and url_hash need to install third-party modules to use them.

5. Dynamic and static separation

Dynamic and static separation is to allow the dynamic web pages in a dynamic website to distinguish constant resources from frequently changing resources according to certain rules. After the dynamic and static resources are split, we can cache them according to the characteristics of static resources. This is the core idea of ​​website static processing.

upstream web_servers {  
       server localhost:8081;  
       server localhost:8082;  
}

server {
    listen       80;
    server_name  localhost;

    set $doc_root /usr/local/var/www;

    location ~* \.(gif|jpg|jpeg|png|bmp|ico|swf|css|js)$ {
       root $doc_root/img;
    }

    location / {
        proxy_pass http://web_servers;
        # Must specify Header Host
        proxy_set_header Host $host:$server_port;
    }

    error_page 500 502 503 504  /50x.html;  
    location = /50x.html {  
        root $doc_root;
    }

 }

6. Others

1. return instruction

Returns http status code and optional second parameter can be redirect URL

location /permanently/moved/url {
    return 301 http://www.example.com/moved/here;
}
2. rewrite command

Rewrite URI Request rewrite, which modifies the request URI multiple times during request processing by using the rewrite directive, which has one optional parameter and two required parameters.

The first (required) parameter is the regular expression that the request URI must match.

The second parameter is the URI used to replace the matching URI.

The optional third parameter is a flag that can stop the processing of further rewrite instructions or send a redirect (code 301 or 302)

location /users/ {
    rewrite ^/users/(.*)$ /show?user=$1 break;
}
3. error_page command

Using the error_page directive, you can configure NGINX to return a custom page along with error codes, replace other error codes in the response, or redirect the browser to a different URI. In the following example, the error_page directive specifies the page (/404.html) to return a 404 page error code.

error_page 404 /404.html;
4. Logs

Access log: need to enable compressed gzip on; otherwise, no log file will be generated, open log_format, access_log comments

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /usr/local/etc/nginx/logs/host.access.log  main;

gzip  on;
5. deny directive
# disable access to a directory
location ~* \.(txt|doc)${
    root $doc_root;
    deny all;
}
6. Built-in variables

The built-in variables that can be used in the nginx configuration file start with the dollar sign $, and some people call them global variables. Among them, the value of some predefined variables can be changed.

  • $args : #This variable is equal to the parameters in the request line, same as $query_string
  • $content_length : The Content-length field in the request header.
  • $content_type : The Content-Type field in the request header.
  • $document_root : The value specified in the root directive for the current request.
  • $host : The request host header field, otherwise the server name.
  • $http_user_agent : client agent information
  • $http_cookie : client cookie information
  • $limit_rate : This variable can limit the connection rate.
  • $request_method : The action requested by the client, usually GET or POST.
  • $remote_addr : The IP address of the client.
  • $remote_port : The port of the client.
  • $remote_user : The username that has been authenticated by the Auth Basic Module.
  • $request_filename : The file path of the current request, generated by the root or alias directive and the URI request.
  • $scheme : HTTP method (eg http, https).
  • $server_protocol : The protocol used by the request, usually HTTP/1.0 or HTTP/1.1.
  • $server_addr : The server address, which can be determined after a system call.
  • $server_name : Server name.
  • $server_port : The port number where the request arrives at the server.
  • $request_uri : The original URI containing the request parameters, excluding the hostname, eg: "/foo/bar.php?arg=baz".
  • $uri : The current URI without request parameters, $uri does not contain the hostname, such as "/foo/bar.html".
  • $document_uri : the given $uri homolog

Author: vbirdbest
blog.csdn.net/vbirdbest/article/details/80913319

Thoroughly understand the five application scenarios of Nginx! Go out and brag and don't worry anymore