Five application scenarios of Nginx

Posted by gloeilamp on Sun, 26 Dec 2021 02:55:04 +0100

 

1: HTTP server

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

1. First, create an HTML directory in the document root directory, / Docroot(/usr/local/var/www), and then put a test html;

2. Configure nginx server in 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

  • http://localhost/ Point to / usr / local / var / www / index html, index. Html is the HTML that comes with installing nginx

  • http://localhost/test.html Point to / usr / local / var / www / HTML / test html

Note: if 403 Forbidden error occurs when accessing the picture, it may be because of nginx The user configuration in the first line of conf is incorrect, and the default is #user nobody; Yes, it is. Under linux, it is changed to user root; Under MacOS, change to the group of user user name; Then reload the configuration file or restart and try again. The user name can be viewed through the who am i command. Nginx common configuration summary! From entry to work is enough

4. Instruction introduction

  • Server: used to define services. There can be multiple server blocks in http

  • Listen: Specifies the IP address and port on which the server listens for requests. If the address is omitted, the server will listen for all addresses. If the port is omitted, the standard port will be used

  • server_name: service name, used to configure domain name

  • Location: used to configure the configuration corresponding to the mapping path uri. A server can have multiple locations followed by a uri, which can be a regular expression, / means to match any path. When the path accessed by the client meets this uri, the code in the location block will be executed

  • Root: root path, when accessing http://localhost/test.html "/ test. HTML" will match the "/" uri, find that the root is / usr / local / var / www / HTML, and 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 the server_ When name is not followed by any path, the index command is directly followed without root; If no specific file is specified in the access path, the resource set by index is returned http://localhost/html/ Index. Is returned by default html

5. location uri regular expression

. : Matches any character except the newline character
? : Repeat 0 or 1 times
+ : Repeat 1 or more times
* : Repeat 0 or more times
\d : Match number
^ : Start of matching string
$ : End of matching string
{n} : repeat n second
{n,} : repeat n Times or more
[c] : Match single character c
[a-z] : matching a-z Any of the lowercase letters
(a|b|c) : Generic line indicates matching any case. Each case is separated by a vertical line, which is generally enclosed in parentheses, and the matching is consistent a character or b character or c Character string
\ Backslash: used to escape special characters

The matching content between parentheses () can be referenced later. 2 represents the content in the second () in front. What is confusing in regular is \ escaping special characters. Five measures to accelerate Nginx response! Use

2, Static server

Companies often encounter a static server, which usually provides an upload function. If other applications need static resources, they can obtain them from the static server. Try the ox knife! Nginx builds a static resource server.

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

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;
       }
    }
}

The set instruction is used for the user-defined variable, and the syntax is set variable name and value; Use variable name and value for reference; Use variable name for reference; Doc is customized here_ Root variable. 2W word summary! Systematization brings you a comprehensive understanding of Nginx

There are generally two ways to map static server location s:

  • Use a path, such as / images /. Generally, pictures will be placed in a picture directory,

  • Use suffixes, such as jpg,. png suffix matching pattern

visit http://localhost/test.jpg Will map to $doc_root/img

visit http://localhost/images/test.jpg When the same path satisfies multiple locations, the location with higher priority will be matched first. Since ^ ~ has a higher priority than ~, the location corresponding to / images / will be taken. Nginx practice: location path matching

Common location path mapping paths are as follows:

  • =Exact matching of ordinary characters. That is, an exact match.

  • ^~Prefix match. If the match is successful, no other location s will be matched.

  • ~Represents performing a regular match, case sensitive

  • ~*Indicates that a regular match is performed, case insensitive

  • /xxx / regular string path matching

  • /Universal matching, any request will be matched

location priority

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

The following is a prioritized Description:

  • The equal sign type (=) has the highest priority. Once the match is successful, no other matches will be found and the search will be stopped.

  • ^~Type expression, not a regular expression. Once the match is successful, no other matches will be found and the search will be stopped.

  • The regular expression type (~ *) takes precedence. If there are regular matches of multiple location s, the longest regular expression is used.

  • General string matching type. Match by prefix.

  • /Universal matching. If there is no match, it will match the universal

  • In the Chinese official account of Linux, the Chinese community replied to "private dishes" to get a surprise package.

Priority search problem: different types of location mappings determine whether to continue the downward search

  • Equal sign type, ^ ~ type: once matched, the search will stop, and other location s will not be matched

  • Regular expression type (~ *), regular string matching type / xxx /: after matching, you will continue to search other location s until you find the one with the highest priority, or stop searching when you find the first case

location priority from high to low:

(location =) > (location Full path) > (location ^~ route) > (location ~,~* Regular order) > (location Partial starting path) > (/)
location = / {
    #Exact match /, host name cannot be followed by any string/
    [ configuration A ]
}
location / {
    #Matches all requests that begin with /.
    #However, if there is a longer expression of the same type, select a longer expression.
    #If there is a regular expression to match, the regular expression will be matched first.
    [ configuration B ]
}
location /documents/ {
    #Match all requests starting with / documents /. After matching, continue to search.
    #However, if there is a longer expression of the same type, select a longer expression.
    #If there is a regular expression to match, 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.
    #Therefore, even if there is a matching regular expression location, it will not be used
    [ configuration D ]
}

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

location /images/ {
    #If the characters match to / images /, the search will continue
    [ 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 of location configuration. The great role of an insignificant character "/" in Nginx configuration is lost.

3, Reverse proxy

Reverse proxy should be the most used function of Nginx. Reverse proxy means that the proxy server accepts the connection request on the internet, then forwards the request to the server on the internal network, and returns the result obtained from the server to the client requesting connection on the internet, At this time, the proxy server appears as a reverse proxy server.

In short, the real server cannot be accessed directly by the external network, so a proxy server is required. While the proxy server can be accessed by the external network, it is in the same network environment as the real server. Of course, it may also be the same server with different ports.

Reverse proxy through proxy_pass instruction.

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 request server makes an error, look for another server
         proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; 
    }

} 

When we access localhost, it is equivalent to accessing localhost:8081

4, Load balancing

Load balancing is also a common function of Nginx. Load balancing means that it is allocated to multiple operating units for execution, such as Web server, FTP server, enterprise key application server and other key task servers, so as to complete work tasks together. This article explains in detail the real difference between load balancing and reverse agent.

In short, when there are two or more servers, the requests are randomly distributed to the specified servers for processing according to the rules. Generally, the reverse proxy needs to be configured at the same time for load balancing configuration, and jump to load balancing through the reverse proxy. At present, Nginx supports three load balancing strategies and two commonly used third-party strategies.

Load balancing is realized through upstream instructions.

1. RR(round robin: polling default)

Each request is allocated to different back-end servers one by one in chronological order, that is, the first request is allocated to the first server and the second request is allocated to the second server. If there are only two servers, the third request will continue to be allocated to the first server. This circular polling continues, that is, the proportion of requests received by the server is 1:1, If the back-end server goes down, it can be automatically eliminated. Polling is the default configuration and does not require too many configurations

The same project uses ports 8081 and 8082 to start the project 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;
        #Header} Host must be specified
        proxy_set_header Host $host:$server_port;
    }
 }

The response can still be obtained by accessing the address http://localhost/api/user/login?username=zhangsan&password=111111 , this method is polling

2. Weight

Specify the polling probability. The weight is directly proportional to the access ratio, that is, the proportion of requests received by the server is the proportion of their configured weight. It is used in the case of uneven performance of the back-end server. For example, if the server performance is poor, it receives fewer point requests, and if the server performance is good, it processes more point requests.

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

For example, only one of the four requests is allocated to 8081, and the other three are allocated to 8082. backup refers to hot standby. Only when both 8081 and 8082 are down can 8083 go

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), there is a big problem, such as saving login information in session, So when you jump to another server, you need to log in again, so many times we need a client to access only one server, so you need to use iphash. Each request of iphash is allocated according to the hash result of accessing ip. In this way, each visitor accesses a back-end server regularly, 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 back-end server, and those with short response time are allocated first. This configuration is for faster user response

upstream backend {
    fair;
    server localhost:8080;
    server localhost:8081;
}

5. url_ Hash (third party)

The request is allocated according to the hash result of the access url, so that each url is directed to the same back-end server, which is more effective when the back-end server is cache. Add a hash statement to the upstream. Other parameters such as weight cannot be written in the server statement_ Method is the hash algorithm used

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

The above five load balancing modes are applicable to different situations, so you can choose which policy mode to use according to the actual situation, but fair and URL_ The hash can only be used after a third-party module is installed.

5, Dynamic and static separation

Dynamic and static separation is to make the dynamic pages in the dynamic website distinguish the constant resources from the 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, which 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;
        #Header} Host must be specified
        proxy_set_header Host $host:$server_port;
    }

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

 }

6, Other

1.return instruction

Returns the http status code and the optional second parameter, which can be the redirected URL

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

2. rewrite instruction

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

In the official account, GitHub apes reply to the "plane" and get a surprise package.

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 rewriting instructions or send redirection (code 301 or 302)

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

3. error_page instruction

Use error_page command, you can configure NGINX to return custom pages and error codes, replace other error codes in the response, or redirect the browser to other URI s. In the following example, error_ The page instruction specifies the page to return the 404 page error code (/ 404.html).

error_page 404 /404.html;

4. Log

Access log: gzip on compression needs to be enabled; Otherwise, the log file will not be generated and the log file will be opened_ 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 instruction

#Prohibit 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 configuration file of nginx start with the dollar sign $and are also called global variables. Among them, the values of some predefined variables can be changed.

  • args: # this variable is equal to the parameter in the request line, the same as query_string

  • $content_length: the content length field in the request header.

  • $content_type: content type field in the request header.

  • $document_root: the value specified in the root instruction of the current request.

  • $host: request host header field, otherwise it is the server name.

  • $http_user_agent: client agent information

  • $http_ Cookies: 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: IP address of the client.

  • $remote_port: the port of the client.

  • $remote_user: the user name that has been authenticated by Auth Basic Module.

  • $request_filename: the file path of the current request, which is generated by the root or alias instruction and URI request.

  • $scheme: http method (such as HTTP, https).

  • $server_protocol: the protocol used by the request, usually HTTP/1.0 or HTTP/1.1.

  • $server_addr: server address, which can be determined after a system call.

  • $server_name: server name.

  • $server_port: the port number on which the request arrives at the server.

  • $request_uri: the original URI containing the request parameters, excluding the host name, such as: "/ foo / bar php? arg=baz”.

  • : the current uri without request parameters does not contain the host name, such as "/ foo / bar html”.

  • : same as uri

Topics: Nginx