nginx匹配规则说明以及匹配的优先级

2018-05-19  本文已影响410人  jackcooper

location 匹配规则

语法规则

location [=|~|~*|^~] /uri/ { … }
模式 含义
location = /uri = 表示精确匹配,只有完全匹配上才能生效
location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前。
location ~ pattern 开头表示区分大小写的正则匹配
location ~* pattern 开头表示不区分大小写的正则匹配
location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后
location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default

前缀匹配时,Nginx 不对 url 做编码,因此请求为 /static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)

多个 location 配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

例子,有如下匹配规则:

location = / {
   echo "规则A";
}
location = /login {
   echo "规则B";
}
location ^~ /static/ {
   echo "规则C";
}
location ^~ /static/files {
    echo "规则X";
}
location ~ \.(gif|jpg|png|js|css)$ {
   echo "规则D";
}
location ~* \.png$ {
   echo "规则E";
}
location /img {
    echo "规则Y";
}
location / {
   echo "规则F";
}

那么产生的效果如下:

所以实际使用中,笔者觉得至少有三个匹配规则定义,如下:

# 直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
# 这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}

# 第二个必选规则是处理静态文件请求,这是 nginx 作为 http 服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}

# 第三个规则就是通用规则,用来转发动态请求到后端应用服务器
# 非静态文件请求就默认是动态请求,自己根据实际把握
# 毕竟目前的一些框架的流行,带.php、.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}

rewrite 语法

1、下面是可以用来判断的表达式:

-f 和 !-f 用来判断是否存在文件
-d 和 !-d 用来判断是否存在目录
-e 和 !-e 用来判断是否存在文件或目录
-x 和 !-x 用来判断文件是否可执行

2、下面是可以用作判断的全局变量

例:http://localhost:88/test1/test2/test.php?k=v
$host:localhost
$server_port:88
$request_uri:/test1/test2/test.php?k=v
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php

redirect 语法

server {
    listen 80;
    server_name start.igrow.cn;
    index index.html index.php;
    root html;
    if ($http_host !~ "^star\.igrow\.cn$") {
        rewrite ^(.*) http://star.igrow.cn$1 redirect;
    }
}

防盗链

location ~* \.(gif|jpg|swf)$ {
    valid_referers none blocked start.igrow.cn sta.igrow.cn;
    if ($invalid_referer) {
       rewrite ^/ http://$host/logo.png;
    }
}

根据文件类型设置过期时间

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        expires 1h;
        break;
    }
}

禁止访问某个目录

location ~* \.(txt|doc)${
    root /data/www/wwwroot/linuxtone/test;
    deny all;
}

完整demo

upstream appzuul {
   server 192.168.*.*:18600;
   server 192.168.*.*:18600;
}

server {
   listen 80;
   server_name a.cooper.com   b.cooper.com;
   rewrite ^/(.*)$ https://a.cooper.com/$1 permanent;
}

server {
    listen      443 ssl;
    server_name a.cooper.com b.cooper.com;
    root   /opt/web_html/auth.100credit.com;
    index login index.html index.htm login.html applicationMasterPage.html;

    ssl_certificate      /opt/nginx-1.4.7/conf/ssl/100credit.com.crt;
    ssl_certificate_key  /opt/nginx-1.4.7/conf/ssl/100credit.com.key;

    ssl_prefer_server_ciphers   on;

    ssl_protocols  SSLv3 TLSv1;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    access_log logs/a.cooper.com_access.log;
    error_log  logs/a.cooper.com_error.log;

    error_page  404  /view/404.html;
    #重定向首页
    location ~ /base/{
        rewrite ^/base/login\.html /#/login permanent;
    }
   #前端目录调整
    location ^~ /v/
    {
        rewrite ^/v/(.*)$ /$1 permanent;
    }

    # 静态资源配置方式一
    location  / {
        root /opt/web_html/apiservice.100credit.com;
        index  index.html;
        try_files $uri $uri/ /index.html;
    }

   # 静态资源配置方式二
   # location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css|json|md|woff)$
   # {
    #    root   /opt/web_html/auth.100credit.com;
    #    index  index.html index.htm applicationMasterPage.html;
   # }

    location ^~ /api/
    {
        proxy_set_header        Host  $host;
        proxy_set_header        X-Real-IP  $remote_addr;
        proxy_set_header        REMOTE-HOST $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://appzuul;
    }

    location ^~ /zuul/api/
    {
        proxy_set_header        Host  $host;
        proxy_set_header        X-Real-IP  $remote_addr;
        proxy_set_header        REMOTE-HOST $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://appzuul;
    }

 }

上一篇 下一篇

猜你喜欢

热点阅读