44 Nginx的root和alias指令

44 Nginx的root和alias指令

设置请求资源的目录 root  / alias

44.1 root

root:设置请求的根目录

语法 root path;
默认值 root html;
位置 http 、server 、 location

 

 

 

 

path:Nginx服务器接收到请求后,查找资源的根目录路径

44.2 alias

alias:设置更改 location 的 URI

语法 alias path;
默认值 -
位置 location

 

 

 

 

path:修改后的根路径

44.3 演示

[root@nginx-100 /usr/local/nginx/conf]# mkdir ../html/images
[root@nginx-100 /usr/local/nginx/conf]# mv ~/mv.png ../html/images/
[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images {
          root html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/images/mv.png ,使用 root

image

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images {
          alias html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/images/mv.png ,使用 alias

image

[root@nginx-100 /usr/local/nginx/conf]# tail -f /usr/local/nginx/logs/error.log 
2026/05/13 11:11:27 [error] 4858#0: *96 open() "/usr/local/nginx/html/mv.png" \
failed (2: No such file or directory), client: 10.0.0.1, server: localhost, \
request: "GET /images/mv.png HTTP/1.1", host: "10.0.0.100"

根据日志判断,没有携带images目录

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images {
          alias html/images;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器访问:http://10.0.0.100/images/mv.png ,使用 alias 新增 images目录

image

44.4 结论

1.在 /usr/local/nginx/html 目录下创建一个 images 目录,并在目录下放一张命名 mv.png 的图片

        location /images {
          root html;

访问图片的路径为:http://10.0.0.100/images/mv.png

 2.如果把 root 改为 alias

        location /images {
          alias html;

再次访问:http://10.0.0.100/images/mv.png 页面返回 404,后端访问地址不正确

root 的处理结果,root 路径 + location 路径
/usr/local/nginx/html/images/mv.png
alias 的处理结果,使用 alias 路径替换 location 路径
/usr/local/nginx/html/images

3.注意 alias 配置 /

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

访问图片的路径为:http://10.0.0.100/images/mv.png

image

[root@nginx-100 /usr/local/nginx/logs]# tail -f error.log 
2026/05/13 15:53:36 [error] 5092#0: *102 open() "/usr/local/nginx/html/imagesmv.png" failed (2: No such file or directory), \
client: 10.0.0.1, server: localhost, request: "GET /images/mv.png HTTP/1.1", host: "10.0.0.100"

修改,将 alias 携带 /

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        
        location ^~/abcd {
          default_type text/plain;
          return 200 'abcd access success';
        }
        location ~*^/abc\w$ {
      default_type text/plain;
      return 200 'access success';
        }

        location /images/ {
          alias html/images/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

访问图片的路径为:http://10.0.0.100/images/mv.png

image

4.总结

root 的处理结果,root 路径 + location 路径
alias 的处理结果,使用 alias 路径替换 location 路径

alias 是一个目录别名的定义,root 则是最上层目录的含义

如果 location 的路径是以 / 结尾,则 alias 也必须是以 / 结尾,root 没有要求

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-12 10:29  马俊南  阅读(20)  评论(0)    收藏  举报