Nginx Static Resource Deployment [1]
“This is the 14th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.
Previous articles:
I met Nginx
The installation of the nginx
Nginx core configuration file structure
Nginx service operation problem
If you want to start, close, or reload the nginx configuration file, you need to go to the sbin directory of the nginx installation directory, and then use the nginx secondary executable file to operate, relatively cumbersome operation, how to optimize this? And what if we wanted to set Nginx to start automatically as the server starts? And that’s where the next two things come in:
Nginx configuration as a system service Nginx commands are configured to the system environmentCopy the code
Nginx is configured as a system service
The Nginx application service is set as a system service to facilitate the start and stop of the Nginx service and other related operations. Specific implementation steps:
Add nginx.service to /usr/lib/systemd/system as follows:
vim /usr/lib/systemd/system/nginx.service
Copy the code
[Unit] Description=nginx web service Documentation=http://nginx.org/en/docs/ After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=default.targetCopy the code
(2) After the addition, you need to set the permission if there is any problem
chmod 755 /usr/lib/systemd/system/nginx.service
Copy the code
(3) Use system commands to operate the Nginx service
Start: systemctl start nginx Stop: systemctl stop nginx Restart: systemctl restart nginx reload configuration files: Systemctl reload nginx Check the status of the nginx: systemctl status Nginx startup: systemctl enable nginxCopy the code
Configure the Nginx command to the system environment
If you want to use these commands, you need to enter the sbin directory to use them, which is very inconvenient. How to optimize, we can add the binary executable file to the system environment variables. In this way, nginx commands can be used in any directory. The specific implementation steps are as follows:
Demo deletable
/ usr/local/nginx/sbin/nginx -v CD/usr/local/nginx/sbin nginx - how to improve the V???????Copy the code
(1) Modify the /etc/profile file
Vim/etc/profile in the last line added export PATH = $PATH: / usr/local/nginx/sbinCopy the code
(2) with immediate effect
source /etc/profile
Copy the code
Run the nginx command
nginx -V
Copy the code
Nginx static resource deployment
Nginx Static Resource Overview
It is not strange for us to search and visit resources on the Internet. Through the browser, we can send an HTTP request from the client to the server to obtain the required content and display the content in the page. At this point, the content we are asking for is divided into two types, one is static resources, one is dynamic resources. Static resources refer to some files that exist in the server and can be directly displayed, such as common HTML pages, CSS files, JS files, pictures, videos and other resources. Dynamic resources refer to resources that exist on the server but need to be processed by certain business logic to obtain. They are displayed in different parts of the page according to different conditions, such as report data display and specific data display according to the current login user.
Nginx static resource configuration instructions
Listen directive
Listen: used to configure the listening port.
grammar | listen address[:port] [default_server]… ; listen port [default_server]… ; |
---|---|
The default value | listen *:80 |
location | server |
Listen is a flexible setup. Let’s use a few examples to familiarize ourselves with common Settings.
Listen 127.0.0.1:8000; // listen localhost:8000 Listen to the specified IP address and port 127.0.0.1; Listen 8000; Listen *:8000; Listens for a connection on a specified portCopy the code
The default_server attribute is the identifier used to set this virtual host as the default host. The default host is the address:port that will be executed by default if no match is found. If not specified, the first server is used by default.
server{
listen 8080;
server_name 127.0.0.1;
location /{
root html;
index index.html;
}
}
server{
listen 8080 default_server;
server_name localhost;
default_type text/plain;
return 444 'This is a error request';
}
Copy the code
Server_name instruction
Server_name: Set the name of the virtual host service.
127.0.0.1 localhost, domain name [www.baidu.com | www.jd.com]
grammar | server_name name … ; The name can be multiple separated by Spaces |
---|---|
The default value | server_name “”; |
location | server |
There are three configuration modes for server_name:
Exact matching Wildcard matching regular expression matchingCopy the code
Configuration Mode 1: Exact match
Such as:
server { listen 80; server_name www.baidu.com www.jd.com; . }Copy the code
Add a little knowledge:
Hosts is a no extension system files, you can use notepad tools such as open, its role is to some commonly used the IP address of the website domain name corresponding to establish a link between a "database", when the user enter a need to log in to the url in the browser, the system will automatically from the first to find the IP address of the corresponding hosts file, once found, The system immediately opens the corresponding web page. If no web page is found, the system sends the web address to the DNS server for IP address resolution.Copy the code
windows:C:\Windows\System32\drivers\etc
Centos: / etc/hosts
Since domain names charge a certain fee, we can modify the hosts file to create some virtual domain names to use. You need to modify the /etc/hosts file to add it
Vim /etc/hosts 127.0.0.1 www.baidu.com 127.0.0.1 www.jd.comCopy the code
Configuration Mode 2: Use wildcards
Server_name supports the wildcard character “*” (*), but the wildcard cannot appear in the middle of the domain name. It can only appear in the first or last paragraph, for example:
server {
listen 80;
server_name *.baidu.com www.jd.*;
# www.baidu.com abc.baidu.com www.jd.cn www.jd.com
...
}
Copy the code
The following configuration will report an error
server {
listen 80;
server_name www.*.cn www.jd.c*
...
}
Copy the code
Configuration 3: Use regular expressions for configuration
Regular expressions can be used in server_name, and the regular expression string starts with ~.
Common regular expressions
code | instructions |
---|---|
^ | Matches the start of the search string |
$ | Matches the end of the search string |
. | Matches any single character except the newline character \n |
\ | Escape characters that mark the next character as a special character |
[xyz] | Character set that matches any of the specified characters |
[a-z] | Character range that matches any character in the specified range |
\w | Matches any of the following characters a-z A-z 0-9 and underscores, equivalent to [A-za-z0-9_] |
\d | Numeric character matching, equivalent to [0-9] |
{n} | It matches exactly n times |
{n,} | At least n times |
{n,m} | Match at least n times and at most m times |
* | Zero or more times, equivalent to {0,} |
+ | One or more times, equivalent to {1,} |
? | Zero times or once, 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 ~ cannot be followed by a space, and parentheses can be usedCopy the code
Matching execution order
Because the server_name directive supports wildcards and regular expressions, a configuration file containing multiple virtual hosts may have a name that is successfully matched by server_name of multiple virtual hosts. In this case, who is handling 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.baidu.*;
default_type text/plain;
return 200 'wildcard_after_success';
}
server{
listen 80;
server_name *.baidu.com;
default_type text/plain;
return 200 'wildcard_before_success';
}
server{
listen 80;
server_name www.baidu.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';
}
Copy the code
Conclusion:
exact_success
wildcard_before_success
wildcard_after_success
regex_success
default_server not found server!!
Copy the code
No1: Server_name is matched correctly. No2: The wildcard is matched successfully at the beginning. No3: the wildcard is matched successfully at the end No5: is handled by the default default_server, if the first server is not specified by defaultCopy the code
The location instructions
server{
listen 80;
server_name localhost;
location / {
}
location /abc{
}
...
}
Copy the code
Location: Sets the URI of the request
grammar | location [ = | ~ | ~* | ^~ |@ ] uri{… } |
---|---|
The default value | – |
location | server,location |
The URI variable is the request string to be matched, which can contain either regular expression or no regular expression. Therefore, when the Nginx server searches for a matching location, it first uses the non-regular expression to match, finds the one with the highest matching degree, and then matches the one with the regular expression. If a direct access is matched, but not, the request is processed using the location with the highest match.
Properties:
Unsigned, the requirement must start in the specified mode
server { listen 80; Server_name 127.0.0.1; location /abc{ default_type text/plain; return 200 "access success"; }} the following access right 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/abcdefCopy the code
= : A uri that does not contain a regular expression 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 match to http://192.168.200.133/abc http://192.168.200.133/abc? P1 = TOM couldn't match http://192.168.200.133/abc/ http://192.168.200.133/abcdefCopy the code
~ : indicates that the URI contains a regular expression and is case sensitive. ~* indicates that the URI contains a regular expression and is case insensitive
In other words, if a URI contains a regular expression, it needs to be identified by the two conjunctions above
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"; }}Copy the code
^~: for urIs that do not contain regular expressions, the function is the same as unsigned, except that if the pattern matches, the search for other patterns stops.
server { listen 80; Server_name 127.0.0.1; location ^~/abc{ default_type text/plain; return 200 "access success"; }}Copy the code
Set the directory root/alias for the requested resource
Root: Sets the root directory of the request
grammar | root path; |
---|---|
The default value | root html; |
location | HTTP, Server, location |
Path is the root directory path for the Nginx server to search for resources after receiving a request.
Alias: The URI used to change the location
grammar | alias path; |
---|---|
The default value | – |
location | location |
Path is the new root path.
Both directives can specify the path to access a resource, so what is the difference between the two?
For example:
Create an images directory in /usr/local/nginx/html and place an image mv.png under the directory
location /images {
root /usr/local/nginx/html;
}
Copy the code
The path to access the image is:
http://192.168.200.133/images/mv.png
Copy the code
(2) Change root to alias
location /images {
alias /usr/local/nginx/html;
}
Copy the code
Error 404: error 404: error 404: error 404
The root result is: Root path + the location path/usr/local/nginx/HTML/images/mv. PNG alias processing result is: replace the location path to use alias/usr/local/nginx/HTML/imagesCopy the code
Need to change path after alias
location /images {
alias /usr/local/nginx/html/images;
}
Copy the code
(3) If the location path ends in a /, alias must also end in a /
Change the above configuration to
location /images/ {
alias /usr/local/nginx/html/images;
}
Copy the code
Access to the error log or the wrong path, so you need to add a slash to alias
Summary:
Replace location path with alias path alias is the definition of a directory alias, root is the meaning of the uppermost directory. If the location path ends in a slash, alias must also end in a slash. Root does not require thisCopy the code
The index instruction
Index: Sets the default home page of a website
grammar | index file … ; |
---|---|
The default value | index index.html; |
location | HTTP, Server, location |
Index can be followed by multiple Settings. If no specific resource is specified during the access, the system searches for the resource in sequence until the first one is found.
For example:
location / { root /usr/local/nginx/html; index index.html index.htm; } to access this location, you can use http://ip:port/. If you do not add anything to the address, the default is to visit index.html and index.htm, and find the first oneCopy the code
Error_page instruction
Error_page: Sets the error page of the website
grammar | error_page code … [=[response]] uri; |
---|---|
The default value | – |
location | HTTP, server, location…… |
When the corresponding response code appears, how to deal with.
For example:
(1) You can specify the address of a specific jump
server {
error_page 404 http://www.baidu.com;
}
Copy the code
(2) You can specify the redirection address
server{ error_page 404 /50x.html; error_page 500 502 503 504 /50x.html; location =/50x.html{ root html; }}Copy the code
(3) Use @ of location to display error information
server{
error_page 404 @jump_to_error;
location @jump_to_error {
default_type text/plain;
return 404 'Not Found Page...';
}
}
Copy the code
The optional =[response] is used to change the corresponding code to another
server{ error_page 404 =200 /50x.html; location =/50x.html{ root html; Error_page = 404; error_page = 200; error_page = 404; error_page = 200Copy the code