nginxlocation重要语法讲解及实践检验
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginxlocation重要语法讲解及实践检验相关的知识,希望对你有一定的参考价值。
nginx常见日志收集及分析工具有rsyslog,awstats,flume,elk,storm等
1 nginx location作用:
location指令的作用是可以根据用户请求的URL来执行不同的应用,就是根据用户请求的网站地址URL匹配,匹配成功即进行相关的操作。
2 location语法
location[=|`||`*|]
[[email protected] scripts]# cd /application/nginx/conf/
[[email protected] extra]# vim www.conf server { listen 80; server_name www.etiantian.org etiantian.org; location / { return 401; } location = / { return 402; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } #access_log logs/www_access.log main; }
[[email protected] extra]# /application/nginx/sbin/nginx -t nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful [[email protected] extra]# /application/nginx/sbin/nginx -s reload
加上注释172.16.1.8 web01 www.etiantian.org
[[email protected] extra]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.16.1.5 lb01 172.16.1.6 lb01 172.16.1.7 web02 172.16.1.8 web01 www.etiantian.org 172.16.1.51 de01 db01.etiantian.org 172.16.1.31 nfs01 172.16.1.41 backup 172.16.1.61 m01
[[email protected] extra]# ping www.etiantian.org -c4 PING web01 (172.16.1.8) 56(84) bytes of data. 64 bytes from web01 (172.16.1.8): icmp_seq=1 ttl=64 time=0.031 ms 64 bytes from web01 (172.16.1.8): icmp_seq=2 ttl=64 time=0.033 ms 64 bytes from web01 (172.16.1.8): icmp_seq=3 ttl=64 time=0.030 ms 64 bytes from web01 (172.16.1.8): icmp_seq=4 ttl=64 time=0.031 ms --- web01 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 2999ms rtt min/avg/max/mdev = 0.030/0.031/0.033/0.004 ms
curl 一下
[[email protected] extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org 402
[[email protected] extra]# cp www.conf{,.test.location}
把www.conf 中的402注释掉如下:
#location = / { #return 402; #}
继续curl后出现结果如下:401
[[email protected] extra]# ../../sbin/nginx -s reload [[email protected] extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org 401
[[email protected] extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/index.html 401
[[email protected] extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/document.html 403
[[email protected] extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/1.gif 404
[[email protected] extra]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/test/1.gif 500
匹配顺序:
实战1:
配置好windowns客户端机的本地dns解析,然后输入地址:
http://www.etiantian.org/documents/ 报错:403
如何让不报403?让返回结果为其他地址呢?
[[email protected] extra]# vim [[email protected] extra]# cat www.conf server { listen 80; server_name www.etiantian.org etiantian.org; location / { return 401; } #location = / { #return 402; #} location /documents/ { root html/www; #403修改为指定的地址 index index.html; #403修改为指定的地址 } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } #access_log logs/www_access.log main; }
[[email protected] extra]# ../../sbin/nginx -s reload [[email protected] extra]# cd ../../html/www/ [[email protected] www]# mkdir documents [[email protected] www]#cd documents/ [[email protected] www]# echo doc >index.html
windows 浏览器输入http://www.etiantian.org/documents/回车
这就说明设置成功了。
如果输入http://www.etiantian.org/就是报错默认的错误
本文出自 “sandshell” 博客,请务必保留此出处http://sandshell.blog.51cto.com/9055959/1958210
以上是关于nginxlocation重要语法讲解及实践检验的主要内容,如果未能解决你的问题,请参考以下文章