Nginx服务location、rewrite区块

利用location区块可以用于定位或者匹配网站资源信息
企业需求解决:
搭建好一台nginx的web服务器。配置好内网卡地址与外网卡地址
web服务的网站域名为www.etiantian.org,站点目录为html/www
要求内网用户可以访问网站http://www.etiantian.org/AV资源信息
要求外网用户禁止访问网站http://www.etiantian.org/AV资源信息

①. 如何利用nginx进行访问控制
deny allow
ngx_http_access_module --- 实现访问控制模块
官方链接:nginx.org/en/docs/http/ngx_http_access_module.html
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}

②. 如何定位站点目录资源信息
location区块进行定位站点目录下资源信息
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location
官方链接:http://nginx.org/en/docs/http/ngx_http_core_module.html#location

第一个里程:编写nginx配置文件
server {
listen 80;
server_name www.etiantian.org;
root html/www;
index index.html index.htm;
location /AV {
allow 172.16.1.0/24;
deny 10.0.0.0/24;
}
}

第二个里程:创建测试访问资源
cd mkdir /application/nginx/html/www    注意:需要切换到站点目录下创建目录
mkdir AV
echo "AV info" >AV/oldboy.html
cat AV/oldboy.html 

第三个里程:重启nginx服务
/application/nginx/sbin/nginx -t
/application/nginx/sbin/nginx -s reload

location [ = | ~ | ~* | ^~ ] uri { ... }
=     --- 精确匹配网站uri资源信息
~     --- 区分大小写匹配网站uri资源信息
~*    --- 不区分大小写匹配网站uri资源信息
^~    --- 优先匹配网站uri资源信息
/AV/  --- 指定匹配网站资源目录信息
/     --- 默认匹配网站资源信息
!     --- 对匹配的内容进行取反

location = / {
    [ configuration A ]       --- 优先级最高 ①
}

location / {                  --- 所有匹配都不满足时候,匹配默认location ④
    [ configuration B ]
}

location /documents/ {        --- 根据资源目录进行匹配         ③
    [ configuration C ]
}

location ^~ /images/ {        --- 优先匹配 ②
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {  --- 不区分大小写匹配网站资源  ③
    [ configuration E ]
}

Nginx服务rewrite模块功能说明

  1. 实现域名地址信息跳转
  2. 用于做伪静态
    www.etiantian.org/oldboy?edu.html ---动态资源
    www.etiantian.org/oldboy-edu.html ---伪静态

    实现类似百度重写域名的功能?
    baidu.com ===> www.baidu.com
    etiantian.org ===> www.etiantian.org

    rewrite
    Syntax: rewrite regex replacement [flag];
    Default: —
    Context: server, location, if

    last 持续匹配新的uri资源
    stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
    break 匹配到第一个uri资源后就终止
    stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
    redirect 临时跳转到uri资源信息
    returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;
    permanent 永久跳转
    returns a permanent redirect with the 301 code.

    rewrite指令实践操作一:(错误)
    [root@web01 extra]# cat bbs.conf
    server {
    listen 80;
    server_name www.etiantian.org bbs.org;
    rewrite ^/(.*) http://www.etiantian.org/$1 permanent; ----此处用到了perl语言的正则表达式
    root html/bbs;
    index index.html index.htm;
    }

    [root@web01 extra]# curl -L etiantian.org
    curl: (47) Maximum (50) redirects followed
    [root@web01 extra]# curl -Lv etiantian.org --- 显示无限循环过程
    说明:以上配置进入了无限循环状态

    rewrite指令实践操作二:(正确)
    cat bbs.conf
    server {
    listen 80;
    server_name etiantian.org;
    rewrite ^/(.*) http://bbs.etiantian.org/$1 permanent;
    }
    server {
    listen 80;
    server_name bbs.etiantian.org bbs.org;
    root html/bbs;
    index index.html index.htm;
    }

    rewrite指令实践操作三:(正确)
    [root@web01 extra]# cat bbs.conf
    server {
    listen 80;
    server_name bbs.etiantian.org bbs.org;
    if ($host ~ "^etiantian.org$") {
    rewrite ^/(.
    ) http://bbs.etiantian.org/$1 permanent;
    }
    root html/bbs;
    index index.html index.htm;
    }

猜你喜欢

转载自blog.51cto.com/tangyong/2130871