One, 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 just a static page, then deployment can be achieved in this way.

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

image.png

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

Note: If a 403 Forbidden error occurs when accessing pictures, it may be because the user configuration in the first line of nginx.conf is incorrect. The default is #user nobody; which is commented. Under linux, change to user root; under macos, change to user name. Group; Then reload the configuration file or restart, and then 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: Specify the IP address and port on which the server listens to the request. If the address is omitted, the server will listen to all addresses. If the port is omitted, the standard port will be 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 to match any path. When the path accessed by the client meets this uri The code in the location block will be executed when

  • root: root path, when accessing http://localhost/test.html, "/test.html" will match "/" uri, and find root as /usr/local/var/www/html, the resource accessed by the user Physical address=root + uri = /usr/local/var/www/html + /test.html=/usr/local/var/www/html/test.html

  • index: Set the homepage. When only accessing server_name, if there is no path after it, the index instruction is not followed by root; if the specific file is not specified in the access path, the resource set by index will be returned. If you access http://localhost /html/ returns index.html by default

5. Location uri regular expression

  • .: match any character except newline
  • ?: Repeat 0 or 1 times
  • +: Repeat 1 or more times
  • *: Repeat 0 or more times
  • \d: match numbers
  • ^: match the beginning of the string
  • $: match the end of the string
  • {n}: Repeat n times
  • {n,}: Repeat n times or more
  • [c]: match a single character c
  • [a-z]: match any one of a-z lowercase letters
  • (a|b|c): The genus line means to match any of the cases, each case is separated by a vertical bar, usually enclosed in parentheses, matching characters that match the a character, the b character, or the c character string
  • \ Backslash: used to escape special characters

The matching content between the parentheses () can be quoted later by $1, and $2 represents the content in the second () before. What is confusing in the regular is the \ escape special characters

Two, static server

A static server is often encountered in a company, and an upload function is usually provided. If other applications need static resources, they will obtain it from the static server.

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

image.png

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 command, the syntax is 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 path, such as /images/ Generally pictures will be placed in a certain picture directory
-Use suffixes, such as .jpg, .png and other suffix matching modes

  1. Visiting http://localhost/test.jpg will map to $doc_root/img
  2. Visit http://localhost/images/test.jpg When the same path meets multiple locations, the location with higher priority will be matched first. Since the priority of ^~ is greater than ~, it will go / images/corresponding location

The common location path mapping paths are as follows:
-= Perform an exact match of ordinary characters. That is, an exact match.
-^~ prefix matching. If the match is successful, no other locations will be matched.
-~ means to perform a regular matching, case sensitive
-~* means to perform a regular match, not case sensitive
-/xxx/ regular string path matching
-/ General matching, any request will be matched

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. Expressions of the same type, the longer string will be matched first

The following is the order of priority:
-The equal sign type (=) has the highest priority. Once the match is successful, no other matches will be found and the search will stop.
-^~ type expression, not a regular expression. Once the match is successful, no other matches will be found and the search will stop.
-Regular expression type (~ ~*) has the second priority. If there are multiple locations that can match the regular expression, the one with the longest regular expression is used.
-Regular string matching type. Match by prefix.
-/ General match, if there is no match, match the general

Priority search problem: different types of location mapping determine whether to continue searching downwards
-Equal sign type, ^~ type: Once matched, the search will stop, and no more locations will be matched
-Regular expression type (~ ~*), regular string matching type /xxx/: After the match is reached, it will continue to search for other locations until it finds the highest priority, or finds the first case. Stop searching

Location priority from high to bottom:
(location =)> (location full path)> (location ^~ path)> (location ~,~* regular order)> (location part starting path)> (/)

location = / {
    # Exact match /, no character string can be behind the host name /
    [configuration A]
}
location / {
    # Match all requests starting with /.
    # But if there are longer expressions of the same type, choose the longer expression.
    # 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 the match is matched, continue to search.
    # But if there are longer expressions of the same type, choose the longer expression.
    # If there is a regular expression that can be matched, the regular expression will be matched first.
    [configuration C]
}
location ^~ /images/ {
    # Match all expressions beginning with /images/. If the match is successful, stop the matching search and stop the search.
    # Therefore, even if there is a regular expression location that matches, it will not be used
    [configuration D]
}

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

location /images/ {
    # Character matches /images/, will continue to search
    [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

Three, reverse proxy

The reverse proxy should be the most used function of Nginx. The reverse proxy method refers to the proxy server to accept the connection request on the internet, and then forward the request to the server on the internal network, and get it 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 externally.

To put it simply, the real server cannot be directly accessed by the external network, so a proxy server is required. 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 and port. It's just different.

The reverse proxy is implemented by the proxy_pass instruction.

Start a Java Web project, the port number is 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 fails to find other servers
         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 to distribute 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.

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

Load balancing is achieved 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 to say, 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 The second request continues to be allocated to the first one, so that the circular polling continues, that is, the ratio of the server receiving the request is 1:1, if the back-end server is down, it can be automatically eliminated. Polling is the default configuration and does not require much configuration

The same project uses port 8081 and port 8082 to start the project

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 response http://localhost/api/user/login?username=zhangsan&password=111111, this method is polled

2. Weight

Specify the polling probability, the weight is proportional to the access ratio, that is, the ratio of the server receiving requests is the ratio of the respective configured weight, which is used in the case of uneven back-end server performance, such as server performance almost receiving fewer requests, server performance If you are better, 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, and only go to 8083 when both 8081 and 8082 are down.

3. ip_hash

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

upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}

5. url_hash (third party)

Distribute requests according to the hash result of the visited URL, 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 in 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 5 types of load balancing are applicable in 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

V. Separation of movement and static

Separation of dynamic and static is to allow dynamic web pages in dynamic websites to distinguish constant resources from constantly changing resources according to certain rules. After dynamic and static resources are split, we can cache them according to the characteristics of static resources. This is the core idea of ​​static website 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;
    }

 }

Six, other

1.return instruction

Return the http status code and the optional second parameter can be the redirect URL

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

2. rewrite instruction

Rewrite the URI request rewrite, by using the rewrite command to modify the request URI several times during request processing. The command 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 rewriting instructions or send redirection (code 301 or 302)

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

3. error_page instruction

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

error_page 404 /404.html;

4. Log

Access log: need to turn on compression gzip on; otherwise no log file will be generated, open log_format, access_log comment

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 command

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 nginx configuration file start with the dollar sign $, which is also called 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, the same as $query_string
-$content_length: 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 instruction of the current request.
-$host: Request the host header field, otherwise it is 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 user name that has been verified by the Auth Basic Module.
-$request_filename: The file path of the current request, which is generated by the root or alias command 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, this value can be determined after completing a system call.
-$server_name: server name.
-$server_port: The port number of the request to reach the server.
-$request_uri: The original URI containing the request parameters, without the host name, such as: "/foo/bar.php?arg=baz".
-$uri: The current URI without request parameters. $uri does not contain the host name, such as "/foo/bar.html".
-$document_uri: same as $uri

点赞(0)

评论列表 共有 0 评论

暂无评论

微信服务号

微信客服

淘宝店铺

support@elephdev.com

发表
评论
Go
顶部