1. work process command
Document address: https://nginx.org/en/docs/http/ngx_http_core_module.html
master_process: Used to specify whether to start the working process.
Syntax | master_process on|off; |
---|---|
default | master_process on; |
position | global block |
worker_processes: Used to configure the number of worker processes generated by Nginx, which is the key to the concurrent processing service of the Nginx server. Theoretically, the larger the value of worker process, the more concurrent processing it can support, but in fact, the setting of this value is subject to the limitations of the server itself. It is recommended to keep this value consistent with the number of cores of the server CPU.
Syntax | worker_processes num/auto; |
---|---|
default | 1 |
position | global block |
default |
[root@localhost conf]# ps -ef|grep nginx root 7208 1 0 05:19 ? 00:00:00 nginx: master process ./nginx nobody 7209 7208 0 05:19 ? 00:00:00 nginx: worker process
If you set worker_processes to 2, you will see the following:
root 7208 1 0 05:19 ? 00:00:00 nginx: master process ./nginx nobody 7227 7208 0 05:28 ? 00:00:00 nginx: worker process nobody 7228 7208 0 05:28 ? 00:00:00 nginx: worker process
2. server_name directive
server_name: used to set the virtual host service name.
127.0.0.1, localhost, domain name [www.baidu.com | www.jd.com]
Syntax | server_name name …;name can provide multiple spaces separated by spaces |
---|---|
default | server_name “”; |
location | server |
There are three ways to configure server_name, namely:
exact match wildcard match regular expression match
Configuration method 1: exact match
like:
server {<!-- --> listen 80; server_name www.itcast.cn www.itheima.cn; ... }
Supplementary knowledge points:
hosts is a system file without extension, which can be opened with Notepad and other tools. Its function is to establish an associated “database” between some commonly used website domain names and their corresponding IP addresses. When entering a URL, the system will first automatically search for the corresponding IP address from the hosts file. Once found, the system will open the corresponding web page immediately. If not found, the system will submit the URL to the DNS domain name resolution server for IP address resolution.
windows:C:\Windows\System32\drivers\etc centos: /etc/hosts
Because the domain name is charged a certain fee, we can modify the hosts file to make some virtual domain names for use. You need to modify the /etc/hosts
file to add
vim /etc/hosts 127.0.0.1 www.itcast.cn 127.0.0.1 www.itheima.cn
Configuration method 2: use wildcard configuration
The wildcard character “*” is supported in server_name, but it should be noted that the wildcard character cannot appear in the middle of the domain name, and can only appear in the first or last paragraph, such as:
server {<!-- --> listen 80; server_name *.itcast.cn www.itheima.*; # www.itcast.cn abc.itcast.cn www.itheima.cn www.itheima.com ... }
The following configuration will report an error
server {<!-- --> listen 80; server_name www.*.cn www.itheima.c* ... }
Configuration 3: Use regular expression configuration
Regular expressions can be used in server_name, and ~
is used as the start tag of the regular expression string.
common regular expressions
code | description |
---|---|
^ | Match search string start position |
$ | Match search string end position |
. | Match any single character except newline \\ |
\ | Escape character, the next character Marked as a special character |
[xyz] | Character set, matches any specified character |
[a-z] | Character range, match any character in the specified range |
\w | Match any of the following characters A-Z a-z 0-9 and underscore, equivalent to [A-Za-z0-9_] |
\d | Number character matching, equivalent In [0-9] |
{n} | Match exactly n times |
{n ,} | Match at least n times |
{n,m} | Match at least n times and up to m times |
* | zero or more times, equivalent to {0,} |
+ | One or more times, equivalent to {1,} |
? | Zero or one time, equivalent to {0,1} |
The configuration is as follows:
server{<!-- --> listen 80; server_name ~^www\.(\w + )\.com$; default_type text/plain; return 200 $1 $2 ..; } Note that no spaces can be added after ~, and the brackets can take values
Match execution order
Since the server_name
directive supports wildcards and regular expressions, in a configuration file containing multiple virtual hosts, a name may be successfully matched by server_name of multiple virtual hosts. When this happens , who will handle the current request?
server{<!-- --> listen 80; server_name ~^www\.\w + \.com$; default_type text/plain; return 200 'regex_success'; } server{<!-- --> listen 80; server_name www.itheima.*; default_type text/plain; return 200 'wildcard_after_success'; } server{<!-- --> listen 80; server_name *.itheima.com; default_type text/plain; return 200 'wildcard_before_success'; } server{<!-- --> listen 80; server_name www.itheima.com; default_type text/plain; return 200 'exact_success'; } server{<!-- --> listen 80 default_server; server_name_; default_type text/plain; return 444 'default_server not found server'; }
in conclusion:
exact_success wildcard_before_success wildcard_after_success regex_success default_server not found server!!
No1: Exact match server_name No2: The wildcard matches server_name successfully at the beginning No3: The wildcard matches server_name successfully at the end No4: The regular expression matches server_name successfully No5: Processed by the default default_server, if not specified, the first server will be found by default
3. location
Location syntax
URL: https://nginx.org/en/docs/http/ngx_http_core_module.html#location
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... } location @name { ... } Default: - Context: server, location
1. Attribute introduction:
Without a symbol, it must start with a specified pattern
server {<!-- --> listen 80; server_name 127.0.0.1; location /abc{<!-- --> default_type text/plain; return 200 "access success"; } } The following visits are all correct http://192.168.200.133/abc http://192.168.200.133/abc?p1=TOM http://192.168.200.133/abc/ http://192.168.200.133/abcdef
= : before being used for uri that does not contain regular expressions, it must match the specified pattern exactly
server {<!-- --> listen 80; server_name 127.0.0.1; location =/abc{<!-- --> default_type text/plain; return 200 "access success"; } } can be matched to http://192.168.200.133/abc http://192.168.200.133/abc?p1=TOM not match http://192.168.200.133/abc/ http://192.168.200.133/abcdef
~ : Used to indicate that the current uri contains regular expressions and is case-sensitive
~*: Used to indicate that the current uri contains regular expressions and is not case-sensitive
In other words, if the uri contains a regular expression, it needs to be identified by the above two matches
server {<!-- --> listen 80; server_name 127.0.0.1; location ~^/abc\w${ default_type text/plain; return 200 "access success"; } } server {<!-- --> listen 80; server_name 127.0.0.1; location ~*^/abc\w${ default_type text/plain; return 200 "access success"; } }
^~: Used before the uri that does not contain regular expressions, the function is the same as that without symbols, the only difference is that if the pattern matches, then stop searching for other patterns.
server {<!-- --> listen 80; server_name 127.0.0.1; location ^~/abc{<!-- --> default_type text/plain; return 200 "access success"; } }
2. location matching order
nginx has two layers of directives to match request URIs. The first level is the server command, which uses the domain name, ip and port to do the first-level matching. When a matching server is found, it enters the location matching of this server. Locations are not matched exactly in the order they appear in the configuration file. The request URI will match the location configured in the server according to the following rules.
- Look for a location with or without the “=” equal sign parameter that completely matches. If there is a fully matched equal sign location, stop matching, execute the instructions in the location, and do not match other types of locations.
- Matches all non-regular expression URI locations (including empty, =, ^~ three parameters). Find the longest location matching the request URI and location URI according to the prefix. If the parameter of the longest location is ^~, stop matching and execute the instructions in the location, otherwise temporarily store the location.
- Match the location of the regular expression URI (including , * parameters), and match the location in the order in which it appears in the configuration file. If the first matching locaiton is found, the matching will stop and the location will be executed.
- After all regular expressions are matched and there is no matching location, the longest prefix matching location temporarily stored in the second step is executed.
Simply follow this rule:
= > ^~ > ~ = ~* > longest prefix match > /
4. root, alias
Set the directory root/alias of the requested resource
root: set the root directory of the request
Syntax | root path; |
---|---|
default | root html; |
location | http, server, location |
path is the path of the root directory where the Nginx server searches for resources after receiving the request.
alias: the URI used to change the location
Syntax | alias path; |
---|---|
default | – |
location | location |
path is the modified root path.
Both of the above two instructions can be used to specify the path to access resources, so what is the difference between the two?
for example:
(1) Create an images directory under the /usr/local/nginx/html
directory, and put a picture mv.png
image in the directory
location /images {<!-- --> root /usr/local/nginx/html; }
The path to access the image is:
http://192.168.200.133/images/mv.png
(2) If you change root to alias
location /images {<!-- --> alias /usr/local/nginx/html; }
If you visit the above address again, a 404 error will appear on the page. If you check the error log, you will find that it is because the address is wrong, so it is verified:
The processing result of root is: root path + location path /usr/local/nginx/html/images/mv.png The result of alias processing is: replace the location path with the alias path /usr/local/nginx/html/images
The path behind the alias needs to be changed to
location /images {<!-- --> alias /usr/local/nginx/html/images; }
(3) If the location path ends with /, the alias must also end with /, which is not required by root
Modify the above configuration to
location /images/ {<!-- --> alias /usr/local/nginx/html/images; }
There will be problems when accessing. Check the error log or the path is wrong, so you need to add / after the alias
summary:
The processing result of root is: root path + location path The result of alias processing is: replace the location path with the alias path alias is the definition of a directory alias, and root is the meaning of the top-level directory. If the location path ends with /, the alias must also end with /, which is not required by root
5. Other
index directive
index: Set the default homepage of the website
Syntax | index file …; |
---|---|
default | index index.html; |
location | http, server, location |
Syntax | error_page code … [=[response]] uri; |
---|---|
default | – |
location | http, server, location… |
When the corresponding response code appears, how to deal with it.
for example:
(1) You can specify the specific jump address
server {<!-- --> error_page 404 http://www.itcast.cn; }
(2) Redirect address can be specified
server{<!-- --> error_page 404 /50x.html; error_page 500 502 503 504 /50x.html; location =/50x.html{<!-- --> root html; } }
(3) Use location’s @ match to complete error message display
server{<!-- --> error_page 404 @jump_to_error; location @jump_to_error {<!-- --> default_type text/plain; return 404 'Not Found Page...'; } }
The optional =[response]
is used to change the corresponding code to another
server{<!-- --> error_page 404 =200 /50x.html; location =/50x.html{<!-- --> root html; } } In this case, when 404 is returned and the corresponding resource cannot be found, it can be seen on the browser that the final returned status code is 200. This piece needs to be paid attention to. When writing the content after error_page, a space needs to be added after 404, 200 no spaces in front