1. 程式人生 > >nginxlocation重要語法講解及實踐檢驗

nginxlocation重要語法講解及實踐檢驗

linux

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重要語法講解及實踐檢驗