nginx-Web server

nginx is most commonly used as a web server, proxy both static resources, but also to forward the request.

The following are blog directories:
Here Insert Picture Description

Create a virtual service

Use listen command ip and port specified:

server {
    listen 127.0.0.1:8080;
    # The rest of server configuration
}
  1. If no port is specified, or the default 80;
  2. If no IP, it listens to all IP (a computer can have several IP right);
  3. If the instruction is not listen, then the monitor 80 / tcp or 8000 / TCP , depending on the system.

If multiple virtual services monitor the same IP and port, then nginx will compare the requested Host parameters and server_name to decide which virtual service.
sever_name may be the following forms:

  1. Full name
  2. Contain wildcards (*)
  3. Regular expressions (beginning with ~) (Note, nginx uses positive language perl regular expression syntax)

If there are multiple virtual services server_name matching Host, nginx be selected using the following order (from the concrete to the abstract):

  1. Full name
  2. To ... beginning
  3. Ending *
  4. Regular expressions (if there is more satisfying, in the order of the configuration file to select a virtual service first met)

Finally, if the Host did not match any server_name, then select the default virtual service; usually the default is the first server block, unless you specify a default virtual default_server service.

server {
    listen      80 default_server;
    ...
}

Configuration path

location parameter of the instruction may be:

  1. Prefix string ( must be the beginning of the path , not the middle portion or an end portion of the path)
  2. Regular Expressions (perl regular expression syntax)

For 1, / some / path / matches /some/path/document.html, but does not match / my-site / some / path ;
for 2,

  1. If it starts with ~, it is case-sensitive;
  2. ~ * If the beginning is not case-sensitive;
location ~ \.html? {
    ...
}

The above code matches any path comprising the .html or .htm.
When the paths match, regular expression higher priority than the prefix string
when nginx processing request, the logical path is selected as follows:

  1. Test all prefix string;
  2. = Defines precisely match the uri and prefix string, if accurate match is found, the search stops;
  3. If the longest prefix matching string ~ ^ beginning, the regular expression can not be checked;
  4. Hold the longest prefix string;
  5. All test regular expressions;
  6. If there is a regular expression matching, then select the appropriate path;
  7. If there is no regular expression matches, the longest path prefix string where the selected saved.

Note that the rules changed 3 regular expressions and priority prefix string of , if not the longest prefix string beginning with ^ ~, regular expression is checked, the longest prefix string select whether to wait regular expression match ends know.

= Generally used frequently requested in the path, so that the agent can accelerate the speed, because once a match, the search stops.

A location both agents block static resources can also forward the request:

server {
    location /images/ {
        root /data;
    }

    location / {
        proxy_pass http://www.example.com;
    }
}

If the request matches the first location, the file / data directory will be provided; If the request matches a second location, will be forwarded to http://www.example.com.
root directive specifies the file system path of the service, after the request uri path will be appended to the path of service, together constitute the full path of the target file. In the example above, the target route is requested /images/example.png /data/images/example.png.
proxy_pass instruction to forward the request to the target server, the target server will reply back to the client. In the above example, any order / images / beginning of the requests are forwarded to the target server.

Using Variables

You can use variables to make nginx in different scenarios with different requests handled in the configuration file. It is named variable value, and is calculated run time, generally used as command parameters. Variable - a name beginning with $. Variables are defined according to the state information nginx, such requests being processed attributes.

nginx preset number of system variables, variables such as HTTP core, may be used set, map and geo directive to define user variables. Most variables are calculated run time, and request and hooks. For example, $ remote_addr contains the client's IP address, $ uri URI saved value.

Return status code

Some sites can return an error code and immediately redirect code, such as the page is temporarily or permanently removed. Achieve this the easiest way is to use a return instruction. such as:

location /wrong/url {
    return 404;
}

The first parameter is the return code of the return instruction. The second parameter is optional, and may be the redirection address (for the return code: 301, 302 and 307) may be text response. E.g:

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

return instruction may be included in the location and the server.

Resources rewrite

uri rewrite instruction request may be modified multiple times during the treatment, the instruction has an optional parameter and two optional parameters. The first parameter (required) defines the uri must match the regular expression. The second parameter is matched uri uri replaced. Optional third parameter is a flag to stop the subsequent rewrite instruction or sends a redirect code (301 or 302). such as:

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

As above, the second parameter captured by the regular expression matching.
and the location server may comprise a plurality of block rewrite instruction. nginx are executed in the order. When a block matching server, server block rewrite instruction executed once.
After a series of processed nginx rewrite instruction, will be matched according to the new location uri. If the location contains a block rewrite instruction, these instructions are executed sequentially rewrite. If there rewrite instruction match, it will open a new round of searches based on the new location of uri.
The following example shows usage rewrite and return instructions:

server {
    ...
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;
    return  403;
    ...
}

This configuration uri difference between the two, similar ( / downloads / some / Media / File ) is rewritten as the uri ( /download/some/mp3/file.mp3 ). Because the last flag, downstream Nginx skip instruction (return instruction and the second rewrite instruction), and continues to match with the new uri. Similarly, like / download / some / audio / file will be replaced by the uri /download/some/mp3/file.ra, if uri not match any rewrite instruction, Nginx 403 returns to the client.

There are two interrupt instruction matching process:

  • last- stop the execution of the current block rewrite instruction server or location, and search for matching new uri the location block and then execute inside rewrite instruction (uri means that you can change it again).
  • break- similar to the last instruction, instruction stops executing rewrite the current block, but canceled the search match the new uri the location block. rewrite command and the new location of the block is not executed.

Answer rewrite

Is sometimes necessary to rewrite or change the content of the response, the other one string for replacement. You may be used to rewrite instruction defines sub_filter applied. This directive supports variables and replace chain to make more complex changes possible.

For example, you can change the link to point to a server other than the absolute agent.

location / {
    sub_filter      /blog/ /blog-staging/;
    sub_filter_once off;
}

Another example of the method from http: // to change htttp_s: // and the request header field to the host name from the localhost address. sub_filter_once instruction instructs nginx continuously applied sub_filter location instruction block.

location / {
    sub_filter     'href="http://127.0.0.1:8080/'    'href="https://$host/';
    sub_filter     'img src="http://127.0.0.1:8080/' 'img src="https://$host/';
    sub_filter_once on;
}

Note that has been modified in a reply sub_filter instruction will not be modified by another sub_filter.

Processing error

Error_page use instruction, may be configured to return a custom page nginx and an error page, instead of a different error codes, or redirect the browser to a different uri. The following example, error_page specified (/404.html) 404 and the error code is returned together.

error_page 404 /404.html;

Note that this command does not mean that error will return (return instruction will return immediately), just indicate when an error occurs, nginx how to deal with. Certain error codes may occur from a server or nginx running (e.g., when the file return 404 not found nginx requested by the client).

The following example, when nginx can not find the page, replacing 404 301, and redirects the client to http: /example.com/new/path.html. When a client has tried to access the page with the old uri it would be useful. 301 tells the browser the page has permanently moved, the results are returned, the need to automatically replace the old address new address.

location /old/path.html {
    error_page 404 =301 http:/example.com/new/path.html;
}

The following is an example of this configuration, when the file is not found, the request will be forwarded to the backend Nginx. = Not specified because the state of the instruction code error_page later, the status code returned by the target server is determined (not necessarily 404).

server {
    ...
    location /images/ {
        # Set the root directory to search for the file
        root /data/www;

        # Disable logging of errors related to file existence
        open_file_cache_errors off;

        # Make an internal redirect if the file is not found
        error_page 404 = /fetch$uri;
    }

    location /fetch/ {
        proxy_pass http://backend/;
    }
}

error_page directive instructs nginx do an internal redirect when no documents. The last parameter in the command error_page uri $ uri variable holds the current request, which is transmitted in the redirect uri.

For example, if / images / some / file not found, it will be replaced with / fetch / images / some / file, open a new round of location search. Eventually, the request will be proxied to the second location of http: // backend /.

open_file_cache_errors written instructions to prevent the error message when the file is not found. There is no need to open the file because the lack of instruction will be handled correctly.

Published 21 original articles · won praise 0 · Views 839

Guess you like

Origin blog.csdn.net/ssehs/article/details/93379329