欢迎光临
我们一直在努力

nginx虚拟机配置和location的使用方法

今天小编就为大家带来一篇介绍nginx虚拟机配置和location使用方法的文章。小编觉得挺实用的,为此分享给大家做个参考。一起跟随小编过来看看吧。

一 虚拟主机

server {
        listen       80;
        server_name  www.yang.com;
        #虚拟机主机重点,一样的ip端口,虚拟机主机就是靠这边的域名来路由内容的
        root         /yang/;
        #根目录 
        index index.html index.php;
        access_log  /yang/yang_com_access.log  main;

        location / {
        }
}   

#虚拟机主机只需要在conf.d目录里 另起一个.conf 文件,里面把server段配好就行了,如果是相同ip端口 用域名来区分,就像上面代码一样

二.location

优先级

=    精确匹配:用于标准uri前,要求请求字符串和uri严格匹配。如果匹配成功就停止匹配,立即执行该location里面的请求。
~    正则匹配:用于正则uri前,表示uri里面包含正则,并且区分大小写。
~*  正则匹配:用于正则uri前,表示uri里面包含正则,不区分大小写。
^~  非正则匹配;用于标准uri前,nginx服务器匹配到前缀最多的uri后就结束,该模式匹配成功后,不会使用正则匹配。
无    普通匹配(\);与location顺序无关,是按照匹配的长短来取匹配结果。若完全匹配,就停止匹配。
PS: 优先级从高到低

1  “=”精准匹配

location = /news/ {
            echo "test1";
        }
[root@www quail]# curl 192.168.249.132/news/
test1

2  "~"区分大小写正则匹配

location ~ \.(html) {
    echo 'test2';
}
location ~ \.(htmL) {
    echo 'test3';
}
[root@www quail]# curl 192.168.249.132/index.html
test2
[root@www quail]# curl 192.168.249.132/index.htmL
test3

3  “~*”不区分大小写的正则匹配

location ~* \.(html){
            echo 'test4';
}
[root@www quail]# curl 192.168.249.132/index.htmL
test4
[root@www quail]# curl 192.168.249.132/index.html
test4

4 “^~”不进行正则匹配的标准匹配,只匹配前缀

location ^~ /index/ {
            echo 'test5';
}
[root@www quail]# curl 192.168.249.132/index/
test5
[root@www quail]# curl 192.168.249.132/index/heihei
test5
[root@www quail]# curl 192.168.249.132/index/asdnmkalsjd
test5

5 普通匹配

location / {
            echo 'test6';
}
[root@www quail]# curl 192.168.249.132
test6

以上就是nginx虚拟机配置和location使用方法的详细内容了,看完之后是否有所收获呢?如果想了解更多相关内容,欢迎关注云行业资讯!

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。