欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > Nginx: 配置项之root和alias的区别, 深入理解location, 以及stub_status模块的用法

Nginx: 配置项之root和alias的区别, 深入理解location, 以及stub_status模块的用法

2025/9/24 19:53:52 来源:https://blog.csdn.net/Tyro_java/article/details/141430916  浏览:    关键词:Nginx: 配置项之root和alias的区别, 深入理解location, 以及stub_status模块的用法

root和alias的区别

  • root 和 alias 两者都是来指定我们的URI和我们磁盘文件上的一个具体静态资源文件的一个映射关系

语法结构

1 ) root

  • 语法:root path;
  • 上下文:http server location if

2 ) alias

  • 语法:alias path;
  • 上下文:location

共同点和区别

  • 相同点:URI到磁盘文件的映射
  • 区别:root会将定义路径与URI叠加;alias 则只取定义路径

区别示例

1 )root

location /picture {root /opt/nginx/html/picture;
}
  • 客户端请求 www.test.com/picture/1.jpg, 则对应磁盘映射
  • 路径 /opt/nginx/html/picture/picture/1.jpg

2 )alias

location /picture {alias /opt/nginx/html/picture;
}
  • 客户端请求 www.test.com/picture/1.jpg, 则对应磁盘映射
  • 路径 /opt/nginx/html/picture/1.jpg

5 ) 注意事项

  • 使用 alias 时,末尾一定加 /
  • alias 只能位于 location 块中

location

1 ) 基础用法

  • 语法:location [ = | ~ | ~* | ^~ ] uri { ... }
  • 上下文: server location
匹配规则含义示例
=精确匹配location = /images/ { … }
~正则匹配,区分大小写location ~ \.(jpg|gif)$ { … }
~*正则匹配,不区分大小写location ~* \.(jpg|gif)$ { … }
∧~匹配到即停止搜索location ^~ /images/ { … }
不带任何符号location / { … }
  • 上述,~* 用处不大,linux 默认区分大小写,与 nginx版本也有关
  • 上述,∧~ 中,如果 遇到 /images/xx?id=1 这种也会匹配

示例

http {server {listen 80;server_name  www.nginx-test.com;location = /match_all/ {root html;index index.html;}location ~ \.(jpegljpg) $ {root html/images;}location ^~ /bbs/ {root html;index index.html index.htm;}}
}
  • $ mkdir { match_all, bbs }

  • $ touch match_all/index.html

    match_all page
    
  • $ touch match_all/match.html

    match page
    
  • $ touch bbs/index.html

    bbs page
    
  • 配置 hosts

    192.168.1.23      www.nginx-test.com
    
  • 访问:http://www.nginx-test.com/match_all 显示 match_all page

  • 访问:http://www.nginx-test.com/match_all/match.html 显示 match page

  • 访问:http://www.nginx-test.com/1.jpg 显示 1.jpg

  • 访问:http://www.nginx-test.com/bbs 显示 bbs page

2 )指令中匹配规则的优先级

  • 优先级排序如下

    • = > ^~ > ~ > ~* > 不带任何字符
  • 官方示例

    location = / {[ configuration A ]
    }location / {[ configuration B ]
    }location /documents/ {[ configuration C ]
    }location ^~ /images/ {[ configuration D ]
    }location ~* \.(gif|jpg|jpeg)$ {[ configuration E ]
    }
    
    • 请求 / 则会匹配 A,B 但 A 优先级高,最终选择A
    • 请求 /index.html 会优先匹配 B, 因为A是精确匹配 (A, B均可能匹配)
    • 请求 /documents/document.html 会优先匹配C, 因为B比较模糊 (B, C均可能匹配)
    • 请求 /images/1.gif 会优先匹配 D, 因为优先级 ^~ > ~* (D,E均可能匹配)
    • 请求 /documents/1.jpg 只匹配 E
  • 特别注意,以下不能重复出现,否则报错

    location /test/ {[ configuration A ]
    }location ^~ /test/ {[ configuration B ]
    }
    

3 ) 理解 location 中的 URL 结尾的反斜线

url 写法的区别

3.1 不带 /

location /test {...
}
  • 首先,还是将 /test 作为一个目录来进行处理,寻找相关test目录并且下面是否有index.html
  • 如果找不到,则会尝试查找 test 文件,存在,则直接返回给浏览器

3.2 带 /

location /test/ {...
}
  • 这种会直接将 test 作为目录,并寻找 index.html
  • 如果找不到,仍不会将 test 作为一个文件,而是直接返回 404

stub_status 模块

  • 它是一个能够给 nginx 提供监控页面的模块,可以实现在页面中实时查看 nginx 服务运行的状态
  • 比如说当前正在处理用户的一个请求,连接数量,已经处理的所有客户端的请求数量
  • 包括正在接受的还有正在处理的,处于响应状态等等各种状态的监控的一个web页面

1 )语法结构

  • 指令: stub_status; 如果低于 1.7.5 版本: stub_status on;
  • 上下文: server location

2 ) 配置示例

# 备注,这个 uri 尽可能 长一些, 复杂一些,防止用户无意间访问到,引发安全问题
location /uri {stub_status;
}

3 )效果示例

Active connections: 2
server accepts handled requests883 883 928Reading: 0 Writing: 1 Waiting: 1
  • 上面 883 883 928 分别表示

    • 已接受的用户连接数
    • 正在处理的用户连接数
    • 处理完的所有的客户端请求数
  • 状态项,如下:

    状态项含义
    Active Connections活跃的连接数量
    accepts接受的客户端连接总数量
    handled处理的客户端连接总数量
    requests客户端总的请求数量
    Reading读取客户端的连接数
    Writing响应数据到客户端的连接数
    Waiting空闲客户端请求连接数量
  • 内嵌变量

    变量名含义
    $connections_active同Active connections值
    $connections_reading同Reading值
    $connections_writing同Writing值
    $connections_waiting同Waiting值

4 )配置示例

  • 配置前明确自己 nginx 的版本是否低于 1.7.5, 这个模块默认不会编译到 nginx 中的
  • nginx想要去支持, 编译的时候,必须要加上 --with-http_stub_status_module
  • 可以使用 $ opt/nginx/sbin/nginx -V 检验是否含有上述参数
    • 可以显示当前版本的nginx编译了哪些模块
  • nginx 本身是一个模块化的设计,在 configure 编译的时候,有很多这样的指令
  • 可以通过 $ ./configure --help (注意,在有这个二进制的目录下执行) 查看类似指令

现在开始配置

server {location /monitor_status {stub_status;}
}
  • $ nginx -t
  • $ nginx -s reload
  • 访问 http://www.nginx-test.com/monitor_status
  • 就会出现内容,展示如下
    Active connections: 1
    server accepts handled requests44 44 51Reading: 0 Writing: 1 Waiting: 0
    
  • 当刷新页面,数据就会变化,响应速度很快

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词