nginx多虚拟主机优先级location匹配规则及tryfiles的使用

nginx多虚拟主机优先级location匹配规则及tryfiles的使用
1.相同server_name多个虚拟主机优先级访问

2.location匹配优先级

3.try_files使用

4.nginx的alias和root区别

5.用什么方法传递用户的真实IP


1.相同server_name多个虚拟主机优先级访问

环境准备

[root@test8_hadoop_kaf conf.d]# cat server01.conf 
server {
    listen 80;
    server_name server01 es.chinasoft.com;
    
    location / {
        root /opt/app/code1;
        index index.html index.htm;
    }

    error_page 500 502 503 504 404 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
[root@test8_hadoop_kaf conf.d]# cat server02.conf 
server {
    listen 80;
    server_name server02 es.chinasoft.com;
    
    location / {
        root /opt/app/code2;
        index index.html index.htm;
    }

    error_page 500 502 503 504 404 /50x.html;

    location = /50x.html {
        root /usr/share/nginx/html;
    }
}


[root@test8_hadoop_kaf conf.d]# diff server01.conf server02.conf 
3c3
<     server_name server01 es.chinasoft.com;
---
>     server_name server02 es.chinasoft.com;
6c6
<         root /opt/app/code1;
---
>         root /opt/app/code2;

[root@test8_hadoop_kaf conf.d]# cat /opt/app/code1/index.html 
<h1>server01</h1>
[root@test8_hadoop_kaf conf.d]# cat /opt/app/code2/index.html 
<h1>server02</h1>

测试
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/index.html
<h1>server01</h1>

修改配置文件,重新加载nginx,再次测试发现访问server02了
[root@test8_hadoop_kaf conf.d]# mv server01.conf server03.conf
[root@test8_hadoop_kaf conf.d]# systemctl reload nginx
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/index.html
<h1>server02</h1>

2.location匹配优先级
= 进行普通字符精确匹配,也就是完全匹配
^~ 表示普通字符匹配,使用前缀匹配
~ \~* 表示执行一个正则匹配()

环境准备:

nginx的配置
[root@test8_hadoop_kaf conf.d]# cat location_test.conf 
server {

    listen 80;

    server_name testserver01 es.chinasoft.com;

    root /opt/app;

    location = /code1/ {
        rewrite ^(.*)$ /code1/index.html break;
    }

    location ~ /code.* {
        rewrite ^(.*)$ /code3/index.html break;
    }

    location ^~ /code {
        rewrite ^(.*)$ /code2/index.html break;
    }
}


代码:
[root@test8_hadoop_kaf conf.d]# mkdir /opt/app/{code1,code2,code3}

[root@test8_hadoop_kaf conf.d]# echo "<h1>code1</h1>" >> /opt/app/code1/index.html
[root@test8_hadoop_kaf conf.d]# echo "<h1>code2</h1>" >> /opt/app/code2/index.html
[root@test8_hadoop_kaf conf.d]# echo "<h1>code3</h1>" >> /opt/app/code3/index.html

测试:
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/code1/
<h1>server01</h1>
<h1>code1</h1>

注释掉code1部分

#location = /code1/ {
    #    rewrite ^(.*)$ /code1/index.html break;
    #}

重新加载nginx,测试发现就匹配到了code2中
[root@test8_hadoop_kaf conf.d]# systemctl reload nginx
[root@test8_hadoop_kaf conf.d]# curl http://es.chinasoft.com/code1/
<h1>server02</h1>
<h1>code2</h1>


3.nginx中try_files的使用

环境准备:
nginx的配置
[root@test8_hadoop_kaf conf.d]# cat tryfiles_test.conf 
server {

    listen 80;

    server_name testserver01 es.chinasoft.com;

    root /opt/app;

    location / {
        root /opt/app/code/cache;
        try_files $uri @java_page;
    }

    location @java_page {
        proxy_pass http://127.0.0.1:9090;
    }

}

/opt/app/code/cache/目录下的html页面
[root@test8_hadoop_kaf conf.d]# cat /opt/app/code/cache/jack.html
cache

tomcat下的html页面
[root@test8_hadoop_kaf conf.d]# cat /data/yunva/test_tomcat8.0.37_9090/webapps/ROOT/jack.html 
<html>
<head>
    <meta charset="utf-8">
    <title>server 1</title>
</head>
<body>
    <h1>java page</h1>
</body>
</html>

测试:
[root@test8_hadoop_kaf cache]# curl http://es.chinasoft.com/jack.html
cache

将/opt/app/code/cache目录下的html页面重命名,模拟页面不存在,可以看到再次访问就到了@java_page
[root@test8_hadoop_kaf cache]# pwd
/opt/app/code/cache
[root@test8_hadoop_kaf cache]# mv jack.html jack.html.bak
[root@test8_hadoop_kaf cache]# curl http://es.chinasoft.com/jack.html
<html>
<head>
    <meta charset="utf-8">
    <title>server 1</title>
</head>
<body>
    <h1>java page</h1>
</body>
</html>


4.nginx的alias和root区别
Root会做拼接路径处理,alias就不拼接,而是直接访问alias目录下的文件
posted @ 2018-01-17 18:55  reblue520  阅读(470)  评论(0编辑  收藏  举报