Distributed - Server Nginx: Basic series of Nginx static resource configuration instructions server_name | listen | location | root | alias | index | error_page


By sending an HTTP request through the browser, the request is sent from the client to the server to obtain the required content and the content is echoed and displayed on the page. At this time, the content we request is divided into two types, one is static resources and the other is dynamic resources. Static resources refer to files that actually exist on the server side and can be displayed directly, such as common html pages, css files, js files, pictures, videos and other resources; dynamic resources refer to files that actually exist on the server side but need to be obtained It needs to go through certain business logic processing and display different parts of the page according to different conditions, such as report data display, display of relevant specific data and other resources based on the currently logged in user;

When Nginx handles the content of static resources, we need to consider the following issues:

(1) Static resource configuration instructions
(2) Static resource configuration optimization
(3) Static resource compression configuration instructions
(4) Static resource caching processing
(5) Static resource access control, including cross-domain issues and anti-leeching issues

Let’s first look at the configuration instructions for static resources.

01. server_name command

When receiving a request, nginx will first check whether the host name and port number of the request match the listening address (listen) of any server block in the configuration file. If a matching listening address is found, nginx will continue to check the server_name directive in the server block. The server_name directive is used to specify the virtual host or IP matched by the server block. nginx will match in the following order:

  • If a specific hostname is specified in the server_name directive, the requested hostname must match it exactly.
  • If a wildcard character is used in the server_name directive, the requested hostname must match the wildcard character.
  • If a regular expression is used in the server_name directive, the requested hostname must match the regular expression.

If a server block is found that matches the requested hostname, nginx will forward the request to the corresponding location configured in the server block. If no server block is found that matches the requested hostname, nginx will use the default server block to handle the request. If no default server block is set, nginx will use the first server block to handle the request.

There are three configuration methods for server_name, namely: exact matching, wildcard matching, and regular expression matching.

1. Exact match

Exact match syntax:

server_name domain1.com domain2.com;
server_name 192.168.1.100;

Among them, domain1.com and domain2.com are the domain names of the virtual hosts. Multiple domain names can be specified, separated by spaces. If the requested host name matches any domain name specified by server_name, Nginx will hand over the request to this virtual host for processing. In addition to the domain name, server_name can also specify an IP address. In this way, when the requested host name is 192.168.1.100, Nginx will hand over the request to this virtual host for processing.

Hosts is a system file without an extension. Its function is to establish an associated "database" between some commonly used URL domain names and their corresponding IP addresses. When the user enters a URL that needs to be logged in in the browser, the system will first automatically start from Search for the corresponding IP address in the hosts file. Once found, the system will immediately open the corresponding web page. If not found, the system will submit the URL to the DNS domain name resolution server for IP address resolution. Because domain names are subject to a certain fee, we can modify the hosts file to create some virtual domain names for use.

# 配置hosts文件
[root@192 conf]# cat /etc/hosts
127.0.0.1 www.domain1.com
127.0.0.1 www.domain2.com

# 配置nginx.conf文件
[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       8080;
        server_name  127.0.0.1;

        default_type text/plain;
        return 500 'This is a 500 request';
    }

    server {
        listen       8080;
        server_name  www.domain1.com www.domain2.com;

        default_type text/plain;
        return 444 'This is a 444 request';
    }
}

# 测试访问
[root@192 conf]# curl http://www.domain1.com:8080/
This is a 444 request

[root@192 conf]# curl http://www.domain2.com:8080/
This is a 444 request

[root@192 conf]# curl http://127.0.0.1:8080/
This is a 500 request

2. Wildcard matching

Server_name supports wildcards *, but it should be noted that wildcards cannot appear in the middle of the domain name, but can only appear in the first or last paragraph.

# 配置nginx.conf文件
[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server {
    
    
        listen       8080;
        server_name  127.0.0.1;

        default_type text/plain;
        return 500 'This is a 500 request';
    }

    server {
    
    
        listen       8080;
        server_name  *.com;

        default_type text/plain;
        return 444 'This is a 444 request';
    }
}

# 测试访问
[root@192 conf]# curl  http://www.domain1.com:8080/
This is a 444 request

3. Regular expression matching

Regular expressions can be used in server_name and used ~as the start tag of the regular expression string. Common regular expressions:

code illustrate
^ Match the beginning of the search string
$ Match the end of the search string
. Matches any single character except newline \n
\ Escape character to mark the next character as a special character
[xyz] Character set, matches any specified character
[a-z] Character range, matches any character within the specified range
\w Matches any of the following characters AZ az 0-9 and underscore, equivalent to [A-Za-z0-9_]
\d Numeric character matching, equivalent to [0-9]
{n} Match exactly n times
{n,} Match 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 or one time, equivalent to {0,1}
[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server {
    
    
        listen       8080;
        # 以^开头,以$结尾
        server_name  ~^www\.(\w+)\.com$;

        default_type text/plain;
        # $1 匹配的是 (\w+) 中的值
        return 444 '=====》$1 =====》';
    }
}

# 测试
[root@192 conf]# curl  http://www.domain2.com:8080/
=====》domain2 =====[root@192 conf]# curl  http://www.domain1.com:8080/
=====》domain1 =====

4. default_server attribute

In Nginx, the default_server attribute is used to identify a virtual host, and its function is to set this virtual host as the default host. The so-called default host means that when Nginx receives a request, if the corresponding server block is not matched, the virtual host corresponding to default_server will be used by default to process the request. If default_server is not set, Nginx will use the first server block to handle the request.

① If the corresponding server block is not matched, the virtual host corresponding to default_server will be used by default to process the request.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server {
    
    
        listen       8080;
        server_name  www.hh.com;

        default_type text/plain;
        return 444 'This is a 444 request';
    }
    
   server {
    
    
        listen       8080 default_server;
        server_name  www.hh.com ;

        default_type text/plain;
        return 500 'This is a 500 request';
    }
}

# 如果没有匹配到对应的 server,则会默认使用 default_server 所对应的虚拟主机来处理该请求 
[root@192 conf]# curl  http://127.0.0.1:8080/
This is a 500 request

[root@192 conf]# curl http://192.168.38.34:8080/
curl: (7) Failed connect to 192.168.38.34:8080; 没有到主机的路由

② If default_server is not set, Nginx will use the first server to handle the request.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server {
    
    
        listen       8080;
        server_name  www.hh.com;

        default_type text/plain;
        return 444 'This is a 444 request';
    }
    
   server {
    
    
        listen       8080;
        server_name  www.hh.com;

        default_type text/plain;
        return 500 'This is a 500 request';
    }
}

# 如果没有设置 default_server,则 Nginx 会使用第一个 server 来处理该请求 
[root@192 conf]# curl  http://127.0.0.1:8080/
This is a 444 request

5. Matching order case

Since the server_name directive supports wildcards and regular expressions, in a configuration file containing multiple server blocks, a name may be successfully matched by the server_name of multiple virtual hosts. In this case, who will handle the current request? ?

① There is a server block whose server_name is exact matching, regular expression matching, wildcard matching at the beginning, wildcard matching at the end, and default configuration:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name www.domain1.com;
        default_type text/plain;
        return 200 '精确匹配成功';
    }

    server{
    
    
        listen 8080;
        server_name ~^www\.\w+\.com$;
        default_type text/plain;
        return 200 '正则表达式匹配成功';
    }

    server{
    
    
        listen 8080;
        server_name www.domain1.*;
        default_type text/plain;
        return 200 '通配符匹配*成功';
    }

    server{
    
    
        listen 8080;
        server_name *.domain1.com;
        default_type text/plain;
        return 200 '*通配符匹配成功';
    }

    server{
    
    
        listen 8080 default_server;
        server_name _;
        default_type text/plain;
	    return 444 'default_server未找到server匹配成功';
    }
}

[root@192 conf]# curl  http://www.domain1.com:8080/
精确匹配成功

② There is a server block with server_name matching regular expression, wildcard matching at the beginning, wildcard matching at the end, and default configuration:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name ~^www\.\w+\.com$;
        default_type text/plain;
        return 200 '正则表达式匹配成功';
    }

    server{
    
    
        listen 8080;
        server_name www.domain1.*;
        default_type text/plain;
        return 200 '通配符匹配*成功';
    }

    server{
    
    
        listen 8080;
        server_name *.domain1.com;
        default_type text/plain;
        return 200 '*通配符匹配成功';
    }

    server{
    
    
        listen 8080 default_server;
        server_name _;
        default_type text/plain;
	    return 444 'default_server未找到server匹配成功';
    }
}

[root@192 conf]# curl  http://www.domain1.com:8080/
*通配符匹配成功

③ There is a server block with server_name matching regular expression, wildcard matching at the end, and default configuration:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name ~^www\.\w+\.com$;
        default_type text/plain;
        return 200 '正则表达式匹配成功';
    }

    server{
    
    
        listen 8080;
        server_name www.domain1.*;
        default_type text/plain;
        return 200 '通配符匹配*成功';
    }

    server{
    
    
        listen 8080 default_server;
        server_name _;
        default_type text/plain;
	    return 444 'default_server未找到server匹配成功';
    }
}

[root@192 conf]# curl  http://www.domain1.com:8080/
通配符匹配*成功

④ There is a server block whose server_name is regular expression matching and default configuration:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name ~^www\.\w+\.com$;
        default_type text/plain;
        return 200 '正则表达式匹配成功';
    }

    server{
    
    
        listen 8080 default_server;
        server_name _;
        default_type text/plain;
	    return 444 'default_server未找到server匹配成功';
    }
}

[root@192 conf]# curl  http://www.domain1.com:8080/
正则表达式匹配成功

⑤ Match the server block of the default configuration:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080 default_server;
        server_name _;
        default_type text/plain;
	    return 444 'default_server未找到server匹配成功';
    }
}

[root@192 conf]# curl  http://www.domain1.com:8080/
default_server未找到server匹配成功

02. listen command

Official document address: https://nginx.org/en/docs/http/ngx_http_core_module.html#listen

In Nginx, the listen directive is used to specify the IP address and port number that Nginx listens on. Its syntax is as follows:

listen address:port [options];

Among them, address represents the IP address to be monitored, which can be a specific IP address or a wildcard character, such as * indicating monitoring of all available IP addresses. port indicates the port number to be monitored. options is optional and is used to specify some additional options, such as SSL/TLS configuration, HTTP/2 configuration, etc.

Here are some examples of common listen directives:

# 监听指定的IP和端口
listen 127.0.0.1:8000;  

# 监听指定IP的所有端口
listen 127.0.0.1;	

# 监听所有可用的 IP 地址,端口号为 8000
listen 8000;	

# 监听所有可用的 IP 地址,端口号为 80
listen *:80;

# 监听指定的 IP 地址,端口号为 8080,启用 SSL/TLS
listen 192.168.1.100:8080 ssl;

# 监听指定的 IP 地址,端口号为 443,启用 SSL/TLS 和 HTTP/2
listen 192.168.1.100:443 ssl http2;

① Modify the nginx configuration file: listen to the 8080 port of the virtual host

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server {
        listen       8080;
        server_name  127.0.0.1;

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

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

② Test access: http://192.168.38.33:8080/

Insert image description here

03. location directive

The location directive is used to match the requested URI and specify how to handle the matched request. The syntax of the location directive is as follows:

location [ = | ~ | ~* | ^~ ] uri {
    ...
}

Among them, uri can be a string or a regular expression used to match the requested URI. = represents exact matching, represents case-sensitive regular matching, * represents case-insensitive regular matching, ^~ represents ordinary string matching. When a request arrives at Nginx, it will match the location directives one by one in a certain order until the first matching directive is found. The following is the matching order of location directives:

① Exact match ( =): If the requested URL path exactly matches the specified string, this configuration block is used to process the request. For example:

location = /path {
    ...
}

② Prefix matching ( ^~): If the requested URL path starts with the specified string, use this configuration block to process the request and stop matching other location blocks. For example:

location ^~ /path {
    ...
}

③ Regular expression matching ( ~or ~*): If the requested URL path matches the specified regular expression, this configuration block is used to process the request. ~Indicates case-sensitive, ~*indicates case-insensitive. For example:

location ~ /path {
    ...
}

④ Ordinary string matching: If the requested URL path contains the specified string, this configuration block is used to process the request. For example:

location /path {
    ...
}

1. Exact match (=)

If the requested URL path exactly matches the specified string, the request is processed using this configuration block.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name localhost;
        
        location = /path {
    
    
            default_type text/plain;
       	    return 200 'access success';
        }
    }
}

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path
access success

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path?a=zhangsan
access success

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/pathabc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

2. Prefix matching (^~)

If the requested URL path begins with the specified string, the request is processed using that configuration block and matching of other location blocks is stopped.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name localhost;
        
        location ^~ /path {
    
    
            default_type text/plain;
       	    return 200 'access success';
        }
    }
}

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path
access success

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/pathabc
access success

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path/abc
access success

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/pat
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

3. Regular expression matching (~ or ~*)

If the requested URL path matches the specified regular expression, this configuration block is used to process the request. ~Indicates case-sensitive, ~*indicates case-insensitive.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name localhost;
        
        # 正则表达式匹配,~^ 表示后面的路径是以 "/path" 开头,\w 表示匹配一个字母或数字,$ 表示路径的结尾。
        location ~^/path\w$ {
    
    
            default_type text/plain;
       	    return 200 'access success';
        }
    }
}

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path2
access success

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/patha
access success

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/Patha
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/pathab
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>
[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name localhost;
        
        # 正则表达式匹配,~*^ 表示后面的路径是以 "/path" 开头,\w 表示匹配一个字母或数字,$ 表示路径的结尾。
        location ~*^/path\w$ {
    
    
            default_type text/plain;
       	    return 200 'access success';
        }
    }
}

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/patha
access success

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path1
access success

# 可以匹配
[root@192 conf]# curl  http://192.168.38.33:8080/Patha
access success

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/Path/abc
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

# 无法匹配
[root@192 conf]# curl  http://192.168.38.33:8080/path
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.16.1</center>
</body>
</html>

4. Ordinary string matching

If the requested URL path contains the specified string, this configuration block is used to process the request.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    server{
        listen 8080;
        server_name localhost;
        
        location /path {
            default_type text/plain;
       	    return 200 'access success';
        }
    }
}

[root@192 conf]# curl  http://192.168.38.33:8080/path
access success

[root@192 conf]# curl  http://192.168.38.33:8080/path/
access success

[root@192 conf]# curl  http://192.168.38.33:8080/path/abc
access success

[root@192 conf]# curl  http://192.168.38.33:8080/path/abc/
access success

5. Matching order case

① Exact matching, prefix matching, regular expression matching, and ordinary string matching exist

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location = /path {
    
    
            default_type text/plain;
       	    return 200 '精确匹配';
        }
    }

    server{
    
    
        listen 8080;
        server_name localhost;
        
        location ^~ /path {
    
    
            default_type text/plain;
       	    return 200 '前缀匹配';
        }
    }
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location ~ /path {
    
    
            default_type text/plain;
       	    return 200 '正则表达式匹配';
        }
    }
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /path {
    
    
            default_type text/plain;
       	    return 200 '普通字符串匹配';
        }
    }
}

[root@192 conf]# curl  http://192.168.38.33:8080/path
精确匹配

② There are prefix matching, regular expression matching, and ordinary string matching.

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    server{
    
    
        listen 8080;
        server_name localhost;
        
        location ^~ /path {
    
    
            default_type text/plain;
       	    return 200 '前缀匹配';
        }
    }
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location ~ /path {
    
    
            default_type text/plain;
       	    return 200 '正则表达式匹配';
        }
    }
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /path {
    
    
            default_type text/plain;
       	    return 200 '普通字符串匹配';
        }
    }
}

[root@192 conf]# curl  http://192.168.38.33:8080/path
前缀匹配

③ There are regular expression matching and ordinary string matching

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location ~ /path {
    
    
            default_type text/plain;
       	    return 200 '正则表达式匹配';
        }
    }
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /path {
    
    
            default_type text/plain;
       	    return 200 '普通字符串匹配';
        }
    }
}

[root@192 conf]# curl  http://192.168.38.33:8080/path
正则表达式匹配

04. root command

The root directive is a directive used to specify the root directory of the Nginx server. In the Nginx configuration file, you can use the root directive to specify the root directory of the server. Its syntax is as follows:

root path;

where path is the path to the server root directory. You can use absolute or relative paths. If using a relative path, it is relative to the path of the Nginx configuration file.

Create an image directory under /usr/local/nginx/htmlthe directory and put 2 a.jpg、b.jpgpictures in the directory:

① Use relative paths:

[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg  b.jpg

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /image {
    
    
            root   html;
        }
    }
}

When the client requests http://192.168.38.33:8080/image/b.jpg, this will access /usr/local/nginx/html/image/b.jpg

② Use absolute path: The processing result of root is: root path + location path, /usr/local/nginx/html/image/b.jpg

[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg  b.jpg

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /image {
    
    
            root   /usr/local/nginx/html;
        }
    }
}

When the client requests http://192.168.38.33:8080/image/b.jpg, it will access /usr/local/nginx/html/image/b.jpg, which is the path specified by root + the path specified by location.

05. alias command

The alias directive maps a URL path to another location in the server's file system. Its syntax is as follows:

location /path {
    alias /new/path;
}

Among them, /path is the URL path requested by the client, and /new/path is the actual path in the server file system. When the client requests /path, Nginx will map it to /new/path and return the corresponding file or directory.

/usr/local/nginx/htmlCreate an image directory under the directory and put 2 a.jpg、b.jpgpictures in the directory:

[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg  b.jpg

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /image {
    
    
            alias /usr/local/nginx/html;
        }
    }
}

When the client requests http://192.168.38.33:8080/image/b.jpg, an error 404 Not Found is reported.

Check the nginx error log:

[root@192 conf]# tail -f /usr/local/nginx/logs/error.log
2023/09/03 16:07:02 [error] 91629#0: *87 open() "/usr/local/nginx/html/b.jpg" failed (2: No such file or directory), client: 192.168.38.1, server: localhost, request: "GET /image/b.jpg HTTP/1.1", host: "192.168.38.33:8080"

It can be seen that the accessed is /usr/local/nginx/html/b.jpg, which is the path specified by alias. The path specified by alias is used instead of the path specified by location.

② Re-modify the path specified by alias in the configuration file:

[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg  b.jpg

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /image {
    
    
            alias /usr/local/nginx/html/image;
        }
    }
}

When the client requests http://192.168.38.33:8080/image/b.jpg, the access is /usr/local/nginx/html/image/b.jpg

06. index command

The index directive is used to set the welcome page of the website. Its syntax is as follows:

index file1 [file2 ...];

Among them, file1, file2, etc. are index file names, and you can specify multiple ones, separated by spaces. nginx will search for these files in the specified order until they are found. If neither is found, a 403 Forbidden error is returned.

[root@192 conf]# ls /usr/local/nginx/html/image
a.jpg  b.jpg

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        location /image {
    
    
            root /usr/local/nginx/html;
            index b.jpg a.jpg;
        }
    }
}

When the client requests http://192.168.38.33:8080/image/, because the default file b.jpg is specified using the index directive, Nginx will put back the /usr/local/nginx/html/image/b.jpg file. If the file does not exist, a directory listing or a 403 Forbidden error will be returned.

07. error_page command

The error_page directive defines the behavior of the server when it encounters an error while processing a request. This directive can redirect the error page to the specified URL or local file, or return the specified HTTP status code. The following is the syntax of the error_page directive:

error_page code ... [=[response]] uri;

Among them, code is the HTTP status code, which can be a single status code or multiple status codes, separated by spaces. file or uri is the file path or URL of the error page to display.

# 将 404 错误页面重定向到 /404.html 页面
error_page 404 /404.html;

# 把所有 404、500、502、503 和 504 错误页面重定向到 /error.html 页面。
error_page 404 500 502 503 504 /error.html;

# 把所有 404 错误页面重定向到一个 403 状态码。
error_page 404 =403;

① You can specify a specific jump address:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server{
    
    
        listen 8080;
        server_name localhost;
        
        error_page 404 http://www.baidu.com;
    }
}

When the client requests http://192.168.38.33:8080/a, jump to http://www.baidu.com

② You can specify the redirection address:

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server {
    
    
        listen 8080;
        server_name localhost;
        
        # 将 404 状态码到 /50x.html 页面
        error_page 404 /50x.html;
        
        # 把所有 500 502 503 504 状态码重定向到 /50x.html 页面。
        error_page 500 502 503 504 /50x.html;
        
        error_page 404 =200 /50x.html;
        location =/50x.html{
    
    
            root html;
        }
    }
}

Client access http://192.168.38.33:8080/a

Insert image description here

③ The optional =[response]function is to change the status code

[root@192 conf]# cat nginx.conf
worker_processes  1;
events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;
    
    server {
    
    
        listen 8080;
        server_name localhost;

        # 当返回404找不到对应的资源的时候,在浏览器上可以看到,最终返回的状态码是200
        # 这块需要注意下,编写error_page后面的内容,404后面需要加空格,200前面不能加空格
        error_page 404 =200 /50x.html;
        location =/50x.html{
    
    
            root html;
        }
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/qq_42764468/article/details/132652998