Nginx的location剖析

1、location的作用:

   location指令的作用是根据用户的请求的URL来执行不同的应用

2、location的语法:

location   [ = |  ~  |  ~*  | ^~ ]  uri {  ....... }
  • location:指令
  • [ = | ~ | ~* | ^~ ] :匹配的标识(在这里面 ‘~’ 用于区分大小写,‘~*’ 不区分大小写,'^~' 是在做完常规检查后不检查正则匹配)

  • uri:匹配的网站地址
  • {......}:匹配URI后要执行的配置段

3、官方示例:

locarion = / {
	[ configuration A ]
}						# 用户访问 / 的时候,匹配configuration A
locarion / {
	[ configuration B ]
}						# 用户访问 /index.html 的时候,匹配configuration B
locarion /documents/ {
	[ configuration C ]
}						# 用户访问 /documents/documents.html 的时候,匹配configuration C
locarion ^~ /images/ {
	[ configuration D ]
}						# 用户访问 /images/1.gif 的时候,匹配configuration D
locarion ~* \.(gif|jpg|jpeg)$ {
	[ configuration E ]
}						# 用户访问 /documents/1.gif 的时候,匹配configuration E

4、实战:

   以www.brian.com虚拟主机为例,修改brian.conf配置文件:

[root@Nginx www_date]# cat brian.conf
    server {
        listen       80;
        server_name  www.brian.com brian.com;
	root    html/brian;
        location / {
	    return 401;
        }
	location = / {
	    return 402;
	}
	location /documents/ {
            return 403;
        }
	location ^~ /images/ {        
            return 404;        # 匹配任何以/images/开头的查询
        }
	location ~* \.(gif|jpg|jpeg)$ {
            return 405;         # 匹配任何以 gif、jpg、jpeg结尾的请求
        }
        access_log logs/brian.log main gzip buffer=128k flush=5s; 
 }

  检查语法:

[root@Nginx conf]# ../sbin/nginx -t
nginx: the configuration file /opt/nginx//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx//conf/nginx.conf test is successful

  平滑重启:

[root@Nginx conf]# ../sbin/nginx -s reload

  Linux客户端测试:

[root@Nginx conf]# curl www.brian.com/                   # 对应location = /
<html>
<head><title>402 Payment Required</title></head>      # 返回402
<body bgcolor="white">
<center><h1>402 Payment Required</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/index.html      # 对应location /
<html>
<head><title>401 Authorization Required</title></head>    # 返回401
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/documents/           #对应location /documents/
<html>
<head><title>403 Forbidden</title></head>           # 返回403
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/images/1.gif     # 对应location ^~ /images/
<html>
<head><title>404 Not Found</title></head>            # 返回404
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>
[root@Nginx conf]# curl www.brian.com/documents/1.gif     # 对应location ~* \.(gif|jpg|jpeg)$
<html>
<head><title>405 Not Allowed</title></head>          # 返回405
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.6.3</center>
</body>
</html>

  

posted @ 2018-03-22 16:30  Brian_Zhu  阅读(1272)  评论(0编辑  收藏  举报