更新 nginx匹配.md

This commit is contained in:
wxin 2025-03-02 11:38:09 +08:00
parent e4a6490051
commit 35a25ca0a5

View File

@ -1,294 +1,294 @@
<h2><center>Nginx匹配</center></h2> <h2><center>Nginx匹配</center></h2>
------ ------
## 一Location匹配 ## 一Location匹配
### 1. location简介 ### 1. location简介
location 指令是 nginx 中最关键的指令之一location 指令的功能是用来匹配不同的 URI 请求,进而对请求做不同的处理和响应。 location 指令是 nginx 中最关键的指令之一location 指令的功能是用来匹配不同的 URI 请求,进而对请求做不同的处理和响应。
### 2. location语法 ### 2. location语法
```nginx ```nginx
Nginx 的 HTTP 配置主要包括三个区块,结构如下: Nginx 的 HTTP 配置主要包括三个区块,结构如下:
http { # 这个是协议级别 http { # 这个是协议级别
include mime.types; include mime.types;
default_type application/octet-stream; default_type application/octet-stream;
keepalive_timeout 65; keepalive_timeout 65;
gzip on; gzip on;
server { # 这个是服务器级别 server { # 这个是服务器级别
listen 80; listen 80;
server_name localhost; server_name localhost;
location / { # 这个是请求级别 location / { # 这个是请求级别
root html; root html;
index index.html index.htm; index index.html index.htm;
} }
location ~ \.(html|jpg)$ { location ~ \.(html|jpg)$ {
root /web; root /web;
} }
} }
} }
浏览器-->地址栏-->URI -->http:// --> 主机:80 -->http协议块的配置 --> server --> location -->页面内容-->返回客户端浏览器 浏览器-->地址栏-->URI -->http:// --> 主机:80 -->http协议块的配置 --> server --> location -->页面内容-->返回客户端浏览器
``` ```
```nginx ```nginx
location 区段 location 区段
- location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。 - location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。
- location 是有顺序的会根据不同请求配置的优先级来匹配的location 处理。 - location 是有顺序的会根据不同请求配置的优先级来匹配的location 处理。
基本语法如下: 基本语法如下:
location [=|~|~*|^~|@] pattern{……} location [=|~|~*|^~|@] pattern{……}
``` ```
### 3. location前缀含义 ### 3. location前缀含义
```nginx ```nginx
= 表示精确匹配,优先级也是最高的 = 表示精确匹配,优先级也是最高的
^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可 ^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可
~ 表示区分大小写的正则匹配 ~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配 ~* 表示不区分大小写的正则匹配
!~ 表示区分大小写不匹配的正则 !~ 表示区分大小写不匹配的正则
!~* 表示不区分大小写不匹配的正则 !~* 表示不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到 / 通用匹配,任何请求都会匹配到
@ 内部服务跳转 @ 内部服务跳转
``` ```
### 4. 查找顺序和优先级 ### 4. 查找顺序和优先级
```nginx ```nginx
= 大于 ^~ 大于 ~|~* 大于 !~|!~* 大于 / = 大于 ^~ 大于 ~|~* 大于 !~|!~* 大于 /
多个location配置的情况下匹配顺序为首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。 多个location配置的情况下匹配顺序为首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
Named Location (location @) 使用@符号定义的命名位置块不匹配URI前缀而是根据具体的用途或配置定义来执行内部重定向。它们通常不与请求URI的前缀直接匹配因此不涉及前缀匹配的优先级。 Named Location (location @) 使用@符号定义的命名位置块不匹配URI前缀而是根据具体的用途或配置定义来执行内部重定向。它们通常不与请求URI的前缀直接匹配因此不涉及前缀匹配的优先级。
``` ```
### 5. 配置实例 ### 5. 配置实例
1. 没有修饰符 表示:必须以指定模式开始 1. 没有修饰符 表示:必须以指定模式开始
```bash ```bash
[root@test ~]# vim /etc/nginx/conf.d/test.conf [root@test ~]# vim /etc/nginx/conf.d/test.conf
server { server {
listen 80; listen 80;
server_name 192.168.159.130; server_name 192.168.159.130;
location / { location / {
root /usr/share/nginx/web; root /usr/share/nginx/web;
index index.html index.htm; index index.html index.htm;
} }
location /abc { location /abc {
alias /usr/share/nginx/web/a.html; alias /usr/share/nginx/web/a.html;
} }
} }
测试: 测试:
[root@test ~]# curl http://192.168.159.130 [root@test ~]# curl http://192.168.159.130
web1 web1
[root@test ~]# curl http://192.168.159.130/abc [root@test ~]# curl http://192.168.159.130/abc
web2 web2
``` ```
2. =表示:必须与指定的模式精确匹配 2. =表示:必须与指定的模式精确匹配
```bash ```bash
[root@test ~]# vim /etc/nginx/conf.d/test.conf [root@test ~]# vim /etc/nginx/conf.d/test.conf
server { server {
listen 80; listen 80;
server_name 192.168.159.130; server_name 192.168.159.130;
location / { location / {
root /usr/share/nginx/web; root /usr/share/nginx/web;
index index.html index.htm; index index.html index.htm;
} }
location = / { location = / {
root /usr/share/nginx/web; root /usr/share/nginx/web;
index index.html index.htm; index index.html index.htm;
} }
} }
测试: 测试:
[root@test ~]# curl http://192.168.159.130/ [root@test ~]# curl http://192.168.159.130/
/ /
[root@test ~]# curl http://192.168.159.130/a.html [root@test ~]# curl http://192.168.159.130/a.html
= / = /
``` ```
3. ~ 表示:指定的正则表达式要区分大小写 3. ~ 表示:指定的正则表达式要区分大小写
```bash ```bash
[root@test ~]# vim /etc/nginx/conf.d/test.conf [root@test ~]# vim /etc/nginx/conf.d/test.conf
server { server {
listen 80; listen 80;
server_name 192.168.159.130; server_name 192.168.159.130;
location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg)$ { location ~ \.(html|jpg|png|js|css|gif|bmp|jpeg)$ {
root /usr/share/nginx/web; root /usr/share/nginx/web;
index index.html index.htm; index index.html index.htm;
expires 1d; #为客户端设置静态资源缓存时间 expires 1d; #为客户端设置静态资源缓存时间
} }
} }
测试: 测试:
[root@test ~]# curl 192.168.159.130:80/index.html [root@test ~]# curl 192.168.159.130:80/index.html
web1 web1
``` ```
4. ~* 表示:指定的正则表达式不区分大小写 4. ~* 表示:指定的正则表达式不区分大小写
```bash ```bash
[root@test ~]# vim /etc/nginx/conf.d/test.conf [root@test ~]# vim /etc/nginx/conf.d/test.conf
server { server {
listen 80; listen 80;
server_name 192.168.159.130; server_name 192.168.159.130;
location ~* \.(HTML|css)$ { location ~* \.(HTML|css)$ {
root /usr/share/nginx/web; root /usr/share/nginx/web;
index index.html index.htm; index index.html index.htm;
} }
} }
[root@test ~]# curl http://192.168.159.130/a.html [root@test ~]# curl http://192.168.159.130/a.html
= / = /
``` ```
5. ^~ :类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了 5. ^~ :类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了
```bash ```bash
``` ```
### 6. 案例 ### 6. 案例
![](C:\Users\wxin\Desktop\nginx\accent\image-202502240004.png) ![](http://182.92.143.66:40072/directlink/nginx-img/image-202502240004.png)
![](C:\Users\wxin\Desktop\nginx\accent\image-202502240005.png) ![](http://182.92.143.66:40072/directlink/nginx-img/image-202502240005.png)
### 7. echo模块 ### 7. echo模块
获取nginx的安装版本 获取nginx的安装版本
```bash ```bash
[root@test ~]# nginx -v [root@test ~]# nginx -v
``` ```
下载一个相同版本的nginx包并解压 下载一个相同版本的nginx包并解压
```bash ```bash
[root@test ~]# wget https://nginx.org/download/nginx-1.16.0.tar.gz [root@test ~]# wget https://nginx.org/download/nginx-1.16.0.tar.gz
[root@test ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/ [root@test ~]# tar xzf nginx-1.16.0.tar.gz -C /usr/local/
``` ```
下载echo模块的安装包并解压到指定位置 下载echo模块的安装包并解压到指定位置
```bash ```bash
[root@test ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz [root@test ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
[root@test ~]# tar xzf v0.61.tar.gz -C /usr/local/ [root@test ~]# tar xzf v0.61.tar.gz -C /usr/local/
``` ```
安装编译所需软件: 安装编译所需软件:
```bash ```bash
[root@test ~]# yum install -y pcre* openssl* gcc gcc-c++ make [root@test ~]# yum install -y pcre* openssl* gcc gcc-c++ make
``` ```
获取源nginx的配置 获取源nginx的配置
```bash ```bash
[root@test ~]# nginx -V [root@test ~]# nginx -V
``` ```
添加上原来已经有的参数和新添加的模块: 添加上原来已经有的参数和新添加的模块:
```bash ```bash
[root@test ~]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/usr/local/echo-nginx-module-0.61 [root@test ~]# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/usr/local/echo-nginx-module-0.61
``` ```
重新编译: 重新编译:
```bash ```bash
[root@test ~]# make [root@test ~]# make
``` ```
将原来的nignx备份 将原来的nignx备份
```bash ```bash
[root@test ~]# mv /usr/sbin/nginx /usr/sbin/nginx_bak [root@test ~]# mv /usr/sbin/nginx /usr/sbin/nginx_bak
[root@test ~]# cp objs/nginx /usr/sbin/ [root@test ~]# cp objs/nginx /usr/sbin/
``` ```
重新启动: 重新启动:
```bash ```bash
[root@test ~]# systemctl restart nginx [root@test ~]# systemctl restart nginx
``` ```
使用echo 使用echo
```bash ```bash
配置 $foo=hello 配置 $foo=hello
[root@test ~]# cd /etc/nginx/conf.d/ [root@test ~]# cd /etc/nginx/conf.d/
[root@test conf.d]# vim echo.conf [root@test conf.d]# vim echo.conf
server { server {
listen 80; listen 80;
server_name localhost; server_name localhost;
location /test { location /test {
set $foo hello; set $foo hello;
echo "foo: $foo"; echo "foo: $foo";
} }
} }
[root@test conf.d]# nginx -s reload [root@test conf.d]# nginx -s reload
[root@test conf.d]# curl localhost/test [root@test conf.d]# curl localhost/test
foo: hello foo: hello
``` ```
### 8. alias和root ### 8. alias和root
- alias 是一个目录别名的定义 - alias 是一个目录别名的定义
- root 则是最上层目录的定义 - root 则是最上层目录的定义
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无。 还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无。
```shell ```shell
location /img/ { location /img/ {
alias /var/www/image/; alias /var/www/image/;
} }
``` ```
若按照上述配置的话,则访问/img/目录里面的文件时ningx会自动去/var/www/image/目录找文件。 若按照上述配置的话,则访问/img/目录里面的文件时ningx会自动去/var/www/image/目录找文件。
```shell ```shell
location /img/ { location /img/ {
root /var/www/image; root /var/www/image;
} }
``` ```
若按照这种配置的话,则访问/img/目录下的文件时nginx会去/var/www/image/img/目录下找文件。 若按照这种配置的话,则访问/img/目录下的文件时nginx会去/var/www/image/img/目录下找文件。