zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    lecture 11.4
    lecture 10.30
    boolean functions and beyon
    lecture10.21
    golang hex to string
    golang中 将string转化为json
    ubuntu16报错: add-apt-repository command not found
    ubuntu16的防火墙关闭
    ubuntu 16 解决错误 manpath: can't set the locale; make sure $LC_* and $LANG are correct
    go get 安装时报 (https fetch: Get https://golang.org/x/crypto/acme/autocert?go-get=1: dial tcp 220.255.2.153:443: i/o timeout)
  • 原文地址:https://www.cnblogs.com/wyglog/p/12466371.html
Copyright © 2011-2022 走看看