12. Nginx进阶-Location

简介

Nginx的三大区块

在Nginx中主要配置包括三个区块,结构如下:

http {												#协议级别
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;

  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  access_log  /var/log/nginx/access.log  main;

  sendfile        on;
  keepalive_timeout  65;
  server {									 #服务器级别
    listen 80;
    server_name wang.mingqu.com;
    charset utf-8;

    location / {							#请求级别
      root /www/html/web/;
      index index.html index.htm;
    }
  }
}

什么是location?

location是配置在Server模块中的请求级别配置。
location可以根据不同的URI使用不同的配置来处理不同的请求。
location是有顺序的,会根据不同请求配置的优先级来匹配的location处理。

应用

基本语法

server {
  ......
  location [=|~|~*|^~|@] pattern {
    ......
  }
}

前缀匹配

符号的解释

符号解释
=表示精确匹配,优先级最高
^~表示URI以某个常规字符串开头的匹配,匹配URL的路径
~表示区分大小写的正则匹配
~*表示不区分大小写的正则匹配
!~表示区分大小写且不匹配的正则
!~*表示不区分大小写且不匹配的正则
/通用匹配符,匹配任意请求
@内部服务跳转

符号的优先级


注意:有多个location配置的情况下,依照优先级匹配;当匹配成功后,停止匹配。

应用举例

主配置文件
路径:/etc/nginx/nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

无修饰符

  1. Nginx配置

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  listen 80;
  server_name wang.mingqu.com;
  charset utf-8;

  location /web01/ {
    root /www/wangmingqu/html;
    index index.html index.htm;
  }
}
  1. 数据准备
mkdir /www/wangmingqu/html/web01 -p
echo "无修饰符" > /www/wangmingqu/html/web01/index.html
  1. 启动验证
nginx -t
systemctl restart nginx

image.png

=匹配

  1. Nginx配置

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  listen 80;
  server_name wang.mingqu.com;
  charset utf-8;

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

  location = / {
    root /www/wangmingqu/html;
    index index.html index.htm;
  }
}
  1. 数据准备
mkdir /www/wangmingqu/html/{web01,web} -p
echo "无修饰符" > /www/wangmingqu/html/web01/index.html
echo "精确匹配" > /www/wangmingqu/html/web/index.html
  1. 启动验证
nginx -t
systemctl restart nginx

image.png

^~匹配

  1. Nginx配置

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  listen 80;
  server_name wang.mingqu.com;
  charset utf-8;

  location ^~ /yewu/ {
    root /www/wangmingqu/html;
    index index.html index.htm;
  }
}
  1. 数据准备
mkdir -p /www/wangmingqu/html/yewu
echo "以yewu开头的匹配" > /www/wangmingqu/html/yewu/index.html
  1. 启动验证
nginx -t
systemctl restart nginx

image.png

~匹配

  1. Nginx配置

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  listen 80;
  server_name wang.mingqu.com;
  charset utf-8;

  location ~* \.jpeg$ { #区分大小写匹配的所有以.jpeg结尾的文件
    root /www/wangmingqu/html/images/;
  }
}
  1. 数据准备
mkdir -p /www/wangmingqu/html/images/
#上传.png和.PNG结尾的图片
ll /www/wangmingqu/html/images/
total 1360
-rw-r--r-- 1 root root 1166629 Mar  4 14:22 lower.jpeg
-rw-r--r-- 1 root root  222061 Mar  4 14:22 upper.JPEG
  1. 启动验证
nginx -t
systemctl restart nginx

后缀大写
image.png
后缀小写
image.png

~*匹配

  1. Nginx配置

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  listen 80;
  server_name wang.mingqu.com;
  charset utf-8;

  location ~ \.jpeg$ { #不区分大小写匹配的所有以.jpeg结尾的文件
    root /www/wangmingqu/html/images/;
  }
}
  1. 数据准备
mkdir -p /www/wangmingqu/html/images/
#上传.png和.PNG结尾的图片
ll /www/wangmingqu/html/images/
total 1360
-rw-r--r-- 1 root root 1166629 Mar  4 14:22 lower.jpeg
-rw-r--r-- 1 root root  222061 Mar  4 14:22 upper.JPEG
  1. 启动验证
nginx -t
systemctl restart nginx

后缀大写
image.png
后缀小写
image.png

/匹配

  1. Nginx配置

路径:/etc/nginx/conf.d/wangmingqu.conf

server {
  listen 80;
  server_name wang.mingqu.com;
  charset utf-8;

  location / {
    root /www/wangmingqu/html/;
    index index.html index.htm;
  }
}
  1. 数据准备
mkdir -p /www/wangmingqu/html/
echo "通用匹配符" > /www/wangmingqu/html/index.html
  1. 启动验证
nginx -t
systemctl restart nginx

image.png

@匹配

@符号,用于定义一个location块,且该location块不能被外部client访问,只能被Nginx内部配置的指令访问,如try_files、error_page。

  1. try_files举例:
server {
    listen       80;
    server_name  wang.mingqu.com;
    charset utf-8;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ @router;
    }
    location @router{
        rewrite ^(.+)$ /index.html last;
    }
}
  1. error_page举例:
server {
  listen 80;
  server_name wang.mingqu.com;
  charset urf-8;
  
  location / {
    error_page 418 = @queryone;
    error_page 419 = @querytwo;
    error_page 420 = @querythree;

    if ( $args ~ "service=one" ) { return 418; }
    if ( $args ~ "service=two" ) { return 419; }
  }

  location @queryone {
    return 200 'do stuff for one';
  }

  location @querytwo {
    return 200 'do stuff for two';
  }
}
  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值