Nginx is configured correctly Location

Original article in Public number: ape Zhou Xiansen program. This platform is not updated regularly, like my article, I welcome attention to the micro-channel public number.
file

Before already talked about the basic configuration of Nginx, this article Nginx major role in the Location directive introduced. This article mainly Location Nginx configuration guidelines are detailed story. Location is performed according to the URI user requests a different location, location on a different approach, i.e., the matching is successful related operations. First of all you need to tell us about the echo Nginx module, it can be configured in the Location label is correct, whether the purpose of configuration.

Installation module echo
echo module can be used to output some information Nginx, the troubleshooting process is debugging a good tool. After this module may output the command character Nginx using echo to the user's browser can be used to detect the correctness of the configuration of Nginx.

Download Nginx-echo

cd /usr/local/nginx-1.12.2/
wget https://github.com/openresty/echo-nginx-module/archive/v0.60.tar.gz

unzip files

tar zxvf v0.60.tar.gz

Check the installed modules

nginx -V

Enter Nginx configuration directory

cd /usr/local/nginx-1.12.2/
./configure --add-module=/usr/local/nginx-1.12.2/echo-nginx-module-0.61 --add-module=/usr/local/nginx-1.12.2/ngx_image_thumb-master

Compile and install, if you are upgrading can be used make upgrade

make && make install

Nginx actual processing request module ngxhttpcore_module module in processing the request, the variable can be accessed through the log record, the module can also be used for echo output. We can simply look at an example:
output request parameter
file

Request results:

[root@VM_0_2_centos ~]# curl www.niyueling.cn/index.html?author=niyueling --header "content-type:text/html;" -H "content-length:200"
query_string: from=jdilong
request_method: GET
content_type: text/html;
content_length: 200
fastcgi_script_name: /index.php
request_uri: /index.php?from=jdilong
document_uri: /index.php
document_root: /application/nginx1.8.1/html
server_protocol: HTTP/1.1
https:
nginx_version: 1.8.1
remote_addr: 192.168.229.196
remote_port: 46786
server_addr: 192.168.229.196
server_port: 80
server_name: www.xyz.com
uri: /index.php

We can see that we can request a series of parameters extracted by the echo module printed.

echo_sleep timing output
LOCATION / echo_with_sleep / {
echo Hello;
echo_flush;
echo_sleep. 3;
echo World;
}
may be provided by the delayed output echo_sleep seconds.

Other asynchronous access URL
LOCATION / {
echo_reset_timer;

echo_location_async /sub1/;
echo_location_async /sub2/;

echo "took $echo_timer_elapsed sec for total.";
}

location /sub1/ {
echo_sleep 2;
echo hello;
}

LOCATION / SUB2 / {
echo_sleep. 1;
echo World;
}
echo_location_async asynchronously access a different address, the current function does not affect execution. $ Echo_timer_elapsed the execution result of the final output value is 0.
Use echo One thing to note is that if there is configured return or echo back configuration proxy_pass, the echo of the output will be overwritten, that is, the browser can not see the contents of the echo.

Location Configuration
Location of syntax :

location [=|~|~*|^~] patt { }

Location Type:

location = patt {} [precise match]
LOCATION PATT {} [Match Normal]
LOCATION PATT {~} [regular match]

In parentheses modifier, you can not write any parameters. In this case, the common match.

Nginx match type of priority:
file

Nginx virtual server configuration
Server {
the listen address [: PORT] | PORT;
server_name the SERVER_NAME;
the root DOCUMENT_ROOT;
}

Can listen on port through listen, there are a variety of settings options:

listen 192.168.0.100:8000; listening 192.168.0.100 port 8000
listen 192.168.0.100; listening on port 80 of 192.168.0.100
listen 8000; listening on a local port 8000
listen *: 8000; listening on a local port 8000
listen localhost: 8000; monitor 127.0. 80 0.1 ports
listen [::]: 8000; 8000 listens local ipv6 port
listen [:: 1]; 80-port monitor local ipv6 address

By setting the virtual server name server_name, simply point, we visit is the domain name in the browser, if the domain name you need to set up multiple separated by a space, and support any character with any length * pass. Simple example:

server_name niyueling.cn www.niyueling.cn

Location Configuration
Location grammar rules:

location [=|~|~*|^~] patt { }

Location shooting process

  1. First for accurate matching, if the hit immediately return a result and end the process of resolution;
  2. Match precisely matched normal miss determination, if a plurality of hit records the "longest" hits, but does not resolve the ends;
  3. Analyzing continued regular match, according to the rules set matches the regular regular expression matching, if there are multiple matches to the regular matching on a once a match is successful and ends immediately returns a result resolution.

And priority order pattern matching:
prioritizing top-down, gradually reduce the priority.

  • location = / uri = represents the beginning of an exact match, the only exact match to take effect.
  • location ^ ~ / uri ^ ~ at the beginning of the URL prefix match path, and before a regular.
  • location ~ pattern ~ represents the beginning of a case-sensitive match regular.
  • location ~ * pattern ~ * represents the beginning of the regular case-insensitive matching.
  • After the location / uri without any modifier, said prefix match, but the match is positive.
  • location / generic matches, is not matched to any other requests are matched to the location, corresponding to switch the default.

Simple example:
Server {
the listen 80;
server_name localhost;
LOCATION = / Text.html
the root / var / WWW / HTML;
index Text.html;
}

    location / {
            root html;   
            index default.html;
    }

    location ~ image { 
            root /var/www/image;
            index index.html;
    }

}

A precise location corresponding to the first match, the browser input 127.0.0.1/text.html, to locate the file server /var/www/html/text.html. The second location corresponding to an ordinary match, the browser input 127.0.0.1, to locate the file server /usr/local/nginx/html/default.html. The third location corresponding to the regular matching, the browser input 127.0.0.1/image, to locate the file server /var/www/html/text.html.

Note:
When prefix match, will first have to find the longest prefix match and then see that there is no front of the prefix match modifier ^ ~, ^ ~ if no modifier will then go look for regular match, find matching regular match after executing this location. If the longest prefix match if there is a hit ^ ~ modifier location, does not match the other regular back matching LOCATION; examples are as follows:
Request http: //localhost/static/files/test.jpg hit rule C, if the rule B has ^ ~ modifier will hit rule B:

location ^~ /static/ {
> echo "规则A";
}

location /static/files {
echo "规则B";
}

~ LOCATION (GIF | JPG | PNG | JS | CSS). {$
echo "Rule C
}

Welcome to my personal public concern number: ape Zhou Xiansen program.
file

Guess you like

Origin www.cnblogs.com/niyueling/p/11563251.html