The security server is to allow only the required number of servers. Ideally, we will build a server based on a minimal system by individually enabling other functions. Minimal configuration is also helpful for debugging. If the error is not available in the minimal system, add functions separately, and then continue to search for errors.
This is the minimum configuration required to run nginx:
# /etc/nginx/nginx.confevents {}
# event context have to be defined to consider config valid
http {}
server {
listen 80;
server_name javatpoint.co www.javatpoint.co *.javatpoint.co;
return 200 "Hello";
}
Root, Location and try_files instructions
Root command
The root directive is used to set the root directory of the request, allowing nginx to map the incoming request to the file system.
server {
listen 80;
server_name javatpoint.co;
root /var/www/javatpoint.co;
}
It allows nginx to return server content upon request:
javatpoint.co:80/index.html # returns /var/www/learnfk.com/index.html
javatpoint.co:80/foo/index.html # returns /var/www/learnfk.com/foo/index.html
Location directive
The location instruction is used to set the configuration according to the requested URI (Uniform Resource Identifier).
The syntax is:
location [modifier] path
Example:
location /foo { # ...}
If no modifier is specified, the path is treated as a prefix, and anything can be followed by it. The example above will match:
/foo
/fooo
/foo123
/foo/bar/index.html...
We can also use multiple location directives in a given context:
server {
listen 80;
server_name javatpoint.co;
root /var/www/javatpoint.co;
location/{
return 200 "root";
}
location /foo {
return 200 "foo";
}
}
javatpoint.co:80 / # => "root"
javatpoint.co:80 /foo # => "foo"
javatpoint.co:80 /foo123 # => "foo"
javatpoint.co:80 /bar # => "root"
Nginx also provides some modifiers that can be used in conjunction with the location directive.
Modifiers have been assigned priority:
= - Exact match
^~ - Preferential match
~ && ~* - Regex match
no modifier - Prefix match
First, nginx will check all exact matches. If it does not exist, it will look for the preferred option. If this match also fails, the regular expression matches will be tested in the order in which they appear. If all other operations fail, the last prefix match will be used.
location /match {
return 200 'Prefix match: will match everything that starting with /match';
}
location ~* /match[0-9] {
return 200 'Case insensitive regex match';
}
location ~ /MATCH[0-9] {
return 200 'Case sensitive regex match';
}
location ^~ /match0 {
return 200 'Preferential match';
}
location = /match {
return 200 'Exact match';
}
/match # => 'Exact match'
/match0 # => 'Preferential match'
/match1 # => 'Case insensitive regex match'
/MATCH1 # => 'Case sensitive regex match'
/match-abc # => 'Prefix match: matches everything that starting with /match'
try\_files directive
The instruction tries different paths and returns any path found.
try_files $uri index.html =404;
Therefore, /foo.html will try to return the files in the following order:
$uri(/foo.html);
index.html
If not found: 404
If we define try\_files in the server context, and then define where to find all requests, try\_files will not be executed. This happens because try\_files in the server context defines its pseudo location, which is the lowest specific location possible. Therefore, the definition of location/ will be more specific than our pseudo location.
server {
try_files $uri /index.html =404;
location/{
}
}
Therefore, we should avoid using try_files in the server context:
server {
location/{
try_files $uri /index.html =404;
}
}
For more information about Nginx configuration, please refer to the old article: Nginx Common Configuration Summary! From entry to work is enough
If you need to learn more about the knowledge system of nginx, can also click on the picture below to enter the Nginx knowledge system column to learn.
basic service installation, configuration file introduction, virtual host configuration practice, Nginx optimization configuration detailed explanation, LNMP architecture Nginx reverse proxy load balancing configuration, Nginx+Tomcat multi-instance and load balancing configuration, high availability, smooth upgrade and recovery of Nginx version Roll, Nginx current limiting configuration, Nginx log production actual combat, Nginx configuration file online generation tool introduction, etc. .
Author: GeekGay Link: imooc.com/article/319107
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。