【Web 集群实战】09_Nginx location

版权声明: https://blog.csdn.net/weixin_42061048/article/details/82780070

【Web 集群实战】09_Nginx location

标签(空格分隔): Web集群实战


1. location 作用

  • location 指令的作用是根据用户请求的 URI 来执行不同的应用。即根据用户请求的网站地址 URL 进行匹配,匹配成功即可进行相关的操作。

2. location 语法

  • location 使用的语法为:
location [ = | ~ | ~* | ^~  ] uri {
...
}
location [ = | ~ | ~* | ^~ ] uri {…}
指令 匹配标识 匹配的网站网址 匹配 URI 后要执行的配置段

注:

  • URI 可以是普通字符串地址路径,或者是正则表达式,匹配成功则执行后面大括号里的相关指令;
  • ‘~’ 用于区分大小写的匹配(大小写敏感);
  • ‘~*’ 用于不区分大小写的匹配
  • ‘^~’ 的作用是在进行常规的字符串匹配检查之后,不做正则表达式的检查。

3. location 匹配配置

  • 修改 www.conf 配置文件
[root@ylt001 conf]# cat extra/www.conf
#www virtualhost by ylt
server {
    listen       80;
    server_name  www.yangyangyang.org yangyangyang.org;
    location / {
        return 401;
    }
    location = / {
        return 402;
    }
    location /documents/ {
        return 403;
    }
    location ^~ /images/ {
        return 404;
    }
    location ~* \.(gif|jpg|jpeg)$ {
        return 500;
    }
    access_log logs/access_www.log main gzip buffer=32k flush=5s;
}
  • 检查语法并重启服务
[root@ylt001 conf]#  /application/nginx/sbin/nginx
[root@ylt001 conf]#  /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.0//conf/nginx.conf test is successful
[root@ylt001 conf]#  /application/nginx/sbin/nginx -s reload
  • 在 Linux 客户端对上述 location 进行测试
[root@ylt001 conf]# /application/nginx/sbin/nginx -s reload
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org
402
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/
402
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/index.html
401
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/documents/document.html
403
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/images/1.gif
404
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/documents/1.jpg
500
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/ylt/
401
[root@ylt001 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.yangyangyang.org/abc/
401

猜你喜欢

转载自blog.csdn.net/weixin_42061048/article/details/82780070