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

HTTP server

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

/usr/local/var/ WWW = /usr/local/var/ WWW = /usr/local/var/ WWW = /usr/local/var/ WWW

** 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; }}}Copy the code

**3, ** access test

  • http://localhost/Point to the/usr/local/var/www/index.htmlIndex.html is the HTML that comes with nginx installed
  • http://localhost/test.htmlPoint to the/usr/local/var/www/html/test.html

Nginx. conf: #user nobody; For Linux, change to user root; Change this parameter to user in macOS. Then reload the configuration file or restart it and try again. The user name can be viewed using the who am I command.

**4, ** instruction introduction

  • Server: Used to define services. HTTP can have multiple server blocks
  • Listen: Specifies the IP address and port on which the server listens for requests. If the IP address is omitted, the server listens for all addresses. If the port is omitted, the standard port is used
  • Server_name: indicates the service name used for the configuration domain name
  • location : A server can have multiple locations, followed by a URI, which can be a regular expression. / means to match any path. When the client accesses a path that meets this URI, the code in the Location block will be executed
  • Root: indicates the root path when accessinghttp://localhost/test.html, /test. HTML matches the/URI, and root is found to be/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 to be accessed onlyserver_nameIf you do not follow any path, you do not go root directly to the index instruction; If no specific file is specified in the access path, the resource set by index is returned if accessedhttp://localhost/html/Index.html is returned by default

**location Specifies the uri regular expression

  • .: Matches any character except newline
  • ?: Repeat 0 or 1 times
  • +: Repeat 1 or more times
  • *: Repeat 0 or more times
  • \d: Matching number
  • ^: Matches the beginning of the string
  • $: Matches the end of the string
  • {n}: Repeat n times
  • {n,}: Repeat n or more times
  • [c]: Matches a single character c
  • [a-z]: Matches any lowercase letter from A to Z
  • (a|b|c)Each case is separated by a vertical line and enclosed by parentheses. The string matching a character, B character, or C character is matched
  • \Backslashes: Used to escape special characters

The matching content between the parentheses () can be referenced later by $1, which represents the content in the previous second (). A confusing part of the re is escaping special characters.

Static server

Static servers are common in companies, often providing an upload function from which other applications can obtain static resources if they need them.

Create images and img directories under /usr/local/var/www, and place a test.jpg file under each directory

The picture

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; }}}Copy the code

Custom variable using set instruction, syntax set variable name value; References use variable name values; References use variable names; The doc_root variable is customized here.

Static server location mapping generally has two ways:

  • Using a path like /images/ usually puts an image in an image directory,
  • Use suffixes such as.jpg and.png to match patterns

http://localhost/test.jpg will be mapped to $doc_root/img

Visit http://localhost/images/test.jpg when the same path to satisfy multiple location, give priority to match the location of the highest priority, because of greater than ~ ^ ~ priority, so will walk/images/corresponds to the location

Common location path mapping paths are as follows:

  • =Perform normal character exact matching. So it’s a perfect match.
  • ^ ~Prefix match. If the match is successful, no more locations will be matched.
  • ~Indicates that a regular match is performed, case sensitive
  • ~ *Indicates that a regular match is performed, case insensitive
  • /xxx/Regular string path matching
  • /Universal match, any request will be matched

The location priority

When a path matches multiple locations, there is a precedence order for which location can be matched. The precedence order depends on the expression type of location value, and does not depend on the order in the configuration file. Expressions of the same type are matched preferentially with strings of length.

Here are the instructions in order of priority:

  • The equal sign type (=) has the highest priority. Once a match is successful, the search stops.

  • ^~ type expression that is not a regular expression. Once a match is successful, the search stops.

  • The priority of the regular expression type (~ ~*). If more than one location regex matches, the one with the longest regular expression is used.

  • General string matching type. Match by prefix.

  • / Universal match. If no match is found, universal is matched

Priority search problem: Different types of location mappings determine whether to continue searching down

  • Equal type, ^~ type: once a match is found, the search is stopped, and no other location is matched
  • Regular expression type (~ ~*), regular string matching type/XXX / : After a match, the search continues for other locations until the highest priority is found, or the search stops when the first case is found

Location Indicates the highest priority:

(location =) > (location ^~ path) > (location ~,~* regular order) > (location partial start path) > (/)

Location = / {# match exactly /, no strings after the host name / [configuration A]} location / {# match all requests starting with /. # But if there is a longer expression of the same type, select the longer expression. If there is a regular expression to match, the regular expression will be matched first. [configuration B]} Location /documents/ {# match all requests that start with /documents/. # But if there is a longer expression of the same type, select the 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/. # so, even if there were in line with the regular expression location, also won't be used [configuration D]} \. The location ~ * (GIF | JPG | jpeg) ${# matches all ends in GIF JPG jpeg's request. [Configuration E]} Location /images/ {# character matches /images/, [configuration F]} location = /test.htm {root /usr/local/var/www/htm; index index.htm; }Copy the code

Note: The priority of location is independent of the location configured for location

Reverse proxy

Reverse Proxy is probably the most used feature of Nginx. In Reverse Proxy mode, a Proxy server accepts Internet connection requests, forwards the requests to a server on the internal network, and returns the results from the server to the Internet client requesting the connection. In this case, the proxy server acts as a reverse proxy server.

In simple terms, 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 resides on the same network environment as the real server, of course, it may be the same server with different ports.

The reverse proxy is implemented by the proxy_pass directive.

Start a Java Web project with port 8081

server { listen 80; server_name localhost; location / { proxy_pass http://localhost:8081; proxy_set_header Host $host:$server_port; Proxy_set_header X-Forwarded-For $remote_ADDR; Proxy_next_upstream error timeout invalid_header http_500 http_502 http_503; }}Copy the code

When we access localhost, we are accessing localhost:8081

Load balancing

Load balancing is also a common function of Nginx. Load balancing means that the execution is distributed among multiple operation units, such as Web server, FTP server, enterprise critical application server, and other critical task servers, so as to complete the work together.

In simple terms, when two or more servers are deployed, requests are randomly distributed to the specified server for processing based on rules. In load-balancing configuration, a reverse proxy must be configured at the same time to switch to load balancing. Nginx currently supports three load balancing policies and two common third-party policies.

Load balancing is done through the upstream directive.

1. RR(Round robin by default)

Each request one by one in chronological order allocation to different backend server, that is to say, on the first request assigned to the first server, the second request assigned to the second server, if there are only two servers, the third request assigned to the first stage, cyclic polling like this, and also is the server receives the request ratio is 1:1, If the backend server becomes Down, it will be deleted automatically. Polling is the default configuration and does not require much configuration

The same project starts with 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; Proxy_set_header Host $Host :$server_port; }}Copy the code

Visit http://localhost/api/user/login? address can still get the response Username =zhangsan&password=111111, this method is polling

2. The weight

Specify the polling probability, weight is proportional to the access ratio, that is, the ratio of the server to receive requests is the ratio of the configured weight, used in the case of uneven back-end server performance, such as server performance is less receiving point requests, server performance is more points to process requests.

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

The example is that only one of the four requests is allocated to 8081 and the other three are allocated to 8082. Backup refers to hot backup. 8083 will be run only when 8081 and 8082 are down

3. ip_hash

Both of the above methods have a problem, 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 will be a big problem, such as storing login information in session. Therefore, it is necessary to re-log in when switching to another server. In most cases, we need a client to access only one server, so we need to use iphash. Each request of iphash is allocated according to the hash result of access IP, so that each visitor has a fixed access to the back-end server, which can solve the session problem.

upstream test {
    ip_hash;
    server localhost:8080;
    server localhost:8081;
}
Copy the code

4. Fair (third Party)

Requests are allocated based on the response time of the back-end server, with priority given to those with short response times. This configuration is designed to give faster response to the user

upstream backend {
    fair;
    server localhost:8080;
    server localhost:8081;
}
Copy the code

5. Url_hash (third party)

Requests are allocated based on the hash result of the url so that each URL is directed to the same backend server, which is more efficient when the backend server is cached. (10) Add the hash to upstream. (10) Do not write weight or other parameters to server. (10) Hash_method is the hash algorithm used

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

The preceding five load balancing modes are applicable to different scenarios. Therefore, you can choose the policy mode based on the actual situation. However, fair and URl_hash can be used only after third-party modules are installed.

Five, static separation

Dynamic and static separation is to make dynamic web pages in the dynamic website according to certain rules to differentiate between constant resources and often changed resources. After dynamic and static resources are split, we can do cache operation according to the characteristics of static resources. This is the core idea of static site 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; Proxy_set_header Host $Host :$server_port; } error_page 500 502 503 504 /50x.html; location = /50x.html { root $doc_root; }}Copy the code

Sixth, other

1. The return instructions

Returns the HTTP status code and the optional second argument can be the URL to redirect

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

2. Rewrite the instructions

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

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

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

An optional third parameter is a flag that can stop the processing of further rewriting instructions or send redirects (codes 301 or 302)

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

3. Error_page instructions

Using the error_page directive, you can configure NGINX to return custom pages with error codes, replace other error codes in responses, or redirect the browser to other URIs. In the following example, the error_page directive specifies the page on which the 404 page error code is to be returned (/404.html).

error_page 404 /404.html;
Copy the code

4. Log

Log access: Enable gzip on compression. Otherwise, no log file is generated and the log_format and access_log annotations are enabled

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;
Copy the code

5. Deny instructions

# blocking access to a directory location ~ * \. (TXT | doc) ${root $doc_root; deny all; }Copy the code

6. Built-in variables

The built-in variables that can be used in nginx configuration files start with the dollar character $, or some call global variables. The values of some predefined variables can be changed.

  • $args: # This variable is equal to the argument in the request line, as in $query_string

  • $content_length: Content-Length field in the request header.

  • $content_type: Content-Type field in the request header.

  • $document_root: The value specified in the root directive in the current request.

  • $host: request host header field, otherwise, 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: indicates the IP address of the client.

  • $remote_port: client port.

  • $remote_user: user name 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 (such as HTTP, HTTPS).

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

  • $server_ADDR: Server address, which can be determined after a system call.

  • $server_name: indicates the server name.

  • $server_port: port number for the request to reach the server.

  • $request_URI: the original URI containing the request parameters, without the host name, e.g. “/foo/bar.php? Arg = baz “.

  • $uri: the current URI with no request parameters. $URI does not contain a host name, such as “/foo/bar.html”.

  • $document_URI: Same as $uri

The author | wisdom elder brother

The original link

This article is the original content of the code farm, and shall not be reproduced without permission.