• nginx的localtion指令详解


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

    http {

    include mime.types;

    # 这个是协议级别

    default_type application/octet-stream; keepalive_timeout 65;

    gzip on;

    server { # 这个是服务器级别

    listen 80;

    server_name localhost;

    location / { # 这个是请求级别 root /usr/share/nginx/html; index index.html index.htm;

    }

    }

    }

    location 区段

    location 是在 server 块中配置,根据不同的 URI 使用不同的配置,来处理不同的请求。

    location 是有顺序的,会被第一个匹配的location 处理。 基本语法如下:

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

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

    2location 前缀含义

    = 表示精确匹配,优先级也是最高的

    ^~ 表示uri以某个常规字符串开头,理解为匹配url路径即可

    ~ 表示区分大小写的正则匹配

    ~* 表示不区分大小写的正则匹配

    !~ 表示区分大小写不匹配的正则

    !~* 表示不区分大小写不匹配的正则

    / 通用匹配,任何请求都会匹配到

    @ 内部服务跳转

    查找顺序和优先级

    = 大于 ^~ 大于 ~|~*|!~|!~* 大于 /

    多个location配置的情况下匹配顺序为:首先匹配 =,其次匹配^~, 其次是按正则匹配,最后是交给

    / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

    3、location 配置示例

    1、没有修饰符 表示:必须以指定模式开始

    server {

    listen

    80;

    server_name localhost;

    location /abc {

    root /home/www/nginx; index 2.html;

    }

    那么,如下是对的:

    http://192.168.1.9/abc

    2、=表示:必须与指定的模式精确匹配

    server {

    listen 80; server_name localhost;

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

    location / {

    root /usr/share/nginx/html; index a.html index.htm;

    }

    location = / {

    root /usr/share/nginx/html; index b.html index.htm;

    }

    }

    测试:http://192.168.1.9

    =/

    http://192.168.1.9/a.html

    /

    3、~ 表示:指定的正则表达式要区分大小写

    server {

    server_name localhost; location ~ /abc {

    root /home/www/nginx; index 2.html index.html;

    }

    }

    测试访问:http://192.168.1.9/abc 不正确的 http://192.168.1.9/ABC

    ========================================

    如果将配置文件修改为

    location ~ /ABC {

    root /home/www/nginx; index 2.html index.html;

    }

    在创建目录和文件:

    [root@ansible-server html]# cd /home/www/nginx/ [root@ansible-server nginx]# mkdir ABC [root@ansible-server nginx]# vim ABC/2.html 访问:

    http://192.168.1.9/ABC/

    结论:~ 需要区分大小写。而且目录需要根据大小写定义。

    location 区段匹配示例

    location = / {

    # 只匹配 / 的查询.

    [ configuration A ]

    }

    location / {

    # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。

    [ configuration B ]

    }

    location ^~ /images/ {

    # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。

    [ configuration C ]

    }

    location ~* .(gif|jpg|png)$ {

    # 匹配任何以gif, jpg, or png结尾的文件 [ configuration D ]

    }

    各请求的处理如下例:

    / → configuration A

    /documents/document.html → configuration B

    /images/1.gif → configuration C

    /documents/1.jpg → configuration D

  • 相关阅读:
    git在iOS开发中的使用
    搜索联系人是去掉拼音中的空格
    xmPP(即时通讯)向远程服务器请求数据
    使用CFStringTransform进行汉字转拼音(可去掉声调)
    node的模块系统和commonJS规范的关系
    在centos7中通过使用yum安装mongoDB
    vue跨组件通信,简易状态管理的使用
    Linux(centos7) 常用命令
    前端打包后, 路由模式为history时,用express测试服务端能否正常解析路由路径
    几个文件目录树生成工具tree,treer,tree-cli,tree-node-cli的使用配置和对比
  • 原文地址:https://www.cnblogs.com/wyglog/p/12466371.html
Copyright © 2020-2023  润新知