nginx-md-diange/nginx-md/第四章:Nginx匹配.md

217 lines
6.7 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<center><h1>Nginx匹配</h1></center>
作者:行癫(盗版必究)
------
## 一Location匹配
#### 1.location简介
location 指令是 nginx 中最关键的指令之一location 指令的功能是用来匹配不同的 URI 请求,进而对请求做不同的处理和响应
#### 2.location语法
location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求
location 是有顺序的会被第一个匹配的location 处理
```shell
Nginx 的 HTTP 配置主要包括三个区块,结构如下:
http { # 这个是协议级别
  include mime.types;
  default_type application/octet-stream;
  keepalive_timeout 65;
  gzip on;
    server { # 这个是服务器级别
      listen 80;
      server_name localhost;
        location / { # 这个是请求级别
          root html;
          index index.html index.htm;
        }
location ~ \.(html|jpg)$ {
root /web;
}
      }
}
```
#### 3.前缀含义
```shell
= 表示精确匹配,优先级也是最高的
^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可
~ 表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配
!~ 表示区分大小写不匹配的正则
!~* 表示不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到
```
#### 4.配置示例
```shell
1、没有修饰符 表示:必须以指定模式开始
location / {
root /abc/location; (/abc/location目录下有abc的目录index.html)
index 2.html;
}
2=表示必须与指定的模式精确匹配创建一个网站发布目录下面分别a.html和b.html
location = / {
root /usr/share/nginx/html;
index b.html index.htm;
}
location / {
root /usr/share/nginx/html;
index a.html index.htm;
}
3、~ 表示:指定的正则表达式要区分大小写
  location ~ \.(jpg|css)$ {
   root location; //要在location目录下有一个以.jpg结尾的文件
  }
4、~* 表示:指定的正则表达式不区分大小写
location ~* \.(JPG|css)$ {
  root location; //要在location目录下有一个以.jpg结尾的文件
  }
5、^~ :类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,那么就停止搜索其他模式了
```
#### 5.优先级
带有“=“的精确匹配优先
没有修饰符的精确匹配
正则表达式按照他们在配置文件中定义的顺序
带有“^~”修饰符的,开头匹配
带有“~” 或“~*” 修饰符的如果正则表达式与URI匹配
没有修饰符的如果指定字符串与URI开头匹配
总结:
多个location配置的情况下匹配顺序为首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求
```shell
= 大于 ^~ 大于 ~|~*|!~|!~* 大于 /
```
#### 6.案例
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230425215743676.png" alt="image-20230425215743676" style="zoom:50%;" />
<img src="https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230425215806679.png" alt="image-20230425215806679" style="zoom:50%;" />
#### 7.echo模块
获取nginx的安装版本
```shell
[root@192 ~]# nginx -v
```
上传或者下载一个相同版本的nginx包并解压
下载echo模块的安装包并解压到指定位置
```shell
[root@192 ~]# wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz
```
安装编译所需软件:
```shell
[root@192 local]# yum install -y pcre* openssl* gcc gcc-c++ make
```
获取源nginx的配置
```shell
[root@192 ~]# nginx -V
```
添加上原来已经有的参数和新添加的模块:
```shell
[root@192 nginx-1.16.0]# ./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
```
重新编译:
```shell
[root@192 nginx-1.16.0]# make
```
将原来的nignx备份
```shell
[root@192 nginx-1.16.0]# mv /usr/sbin/nginx /usr/sbin/nginx_bak
[root@192 nginx-1.16.0]# cp objs/nginx /usr/sbin/
```
重新启动:
```
[root@192 nginx-1.16.0]# systemctl restart nginx
```
使用echo
```shell
配置 $foo=hello
[root@192 ~]# cd /etc/nginx/conf.d/
[root@192 conf.d]# vim echo.conf
server {
listen 80;
server_name localhost;
location /test {
set $foo hello;
echo "foo: $foo";
}
}
[root@192 conf.d]# nginx -s reload
[root@192 conf.d]# curl localhost/test
foo: hello
```
#### 8.alias和root
alias 是一个目录别名的定义
root 则是最上层目录的定义
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的,而root则可有可无
```shell
location /img/ {
alias /var/www/image/;
}
```
若按照上述配置的话,则访问/img/目录里面的文件时ningx会自动去/var/www/image/目录找文件
```shell
location /img/ {
root /var/www/image;
}
```
若按照这种配置的话,则访问/img/目录下的文件时nginx会去/var/www/image/img/目录下找文件