第一节:nginx常见问题一

Nginx常见问题

一、nginx多server优先级

在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如何配置多个相同的server_name,会导致server_name出现优先级访问冲突。

1.准备多个配置文件

[root@web01 conf.d]#  cat server1.conf 
server {
    listen 80;
    server_name localhost test1.com;

    location / {
        root /code/test1;
        index index.html;
    }
}

[root@web01 conf.d]# cat server2.conf 
server {
    listen 80;
    server_name localhost test2.com;

    location / {
        root /code/test2;
        index index.html;
    }
}

[root@web01 conf.d]# cat server3.conf 
server {
    listen 80;
    server_name localhost test3.com;

    location / {
        root /code/test3;
        index index.html;
    }
}

2.创建站点文件

[root@web01 conf.d]# mkdir /code/test{1..3}
[root@web01 conf.d]# chown -R www.www /code/test*

[root@web01 conf.d]# echo server1 > /code/test1/index.html
[root@web01 conf.d]# echo server2 > /code/test2/index.html
[root@web01 conf.d]# echo server3 > /code/test3/index.html

3.访问测试

1.重启,访问IP
[root@web01 conf.d]# systemctl restart nginx

#根据ip访问
    1)用户第一次访问,读取server1.conf配置返回结果
    [root@web01 ~]# curl 10.0.0.7
    test1

    2)此时将server1.conf修改为server4.conf重启nginx
    [root@web01 conf.d]# mv server1.conf server4.conf
    [root@web01 conf.d]# systemctl restart nginx

    3)再次访问时,读取server2.conf配置返回结果
    [root@web01 conf.d]# curl 10.0.0.7
    test2

2.配置hosts,访问域名
10.0.0.7 test1.com test2.com test3.com

4.多server优先级总结

1.首先选择所有的字符串完全匹配的server_name。(完全匹配  www.mumusir.com)
2.选择通配符在前面的server_name,如 *.mumusir.com
3.选择通配符在后面的server_name,如 www.mumusir.*
4.最后选择使用正则表达式匹配的server_name,如:~^www\.(.*)\.com$
5.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件

5.多server优先级总结验证

1)配置完全匹配的配置文件

[root@web01 conf.d]# vim server.conf
server {
    listen 80;
    server_name www.test.com;

    location / {
        root /code/test;
        index index.html;
    }
}
[root@web01 conf.d]# echo "完全匹配" > /code/test/index.html

2)配置通配符在前面的配置文件

[root@web01 conf.d]# vim server1.conf
server {
    listen 80;
    server_name *.test.com;

    location / {
        root /code/test1;
        index index.html;
    }
}
[root@web01 conf.d]# echo "通配符在前面" > /code/test1/index.html

3)配置通配符在后面的配置文件

[root@web01 conf.d]# vim server2.conf
server {
    listen 80;
    server_name www.test.*;

    location / {
        root /code/test2;
        index index.html;
    }
}
[root@web01 conf.d]# echo "通配符在后面" > /code/test2/index.html

4)正则表达式的配置文件

[root@web01 conf.d]# vim server3.conf 
server {
    listen 80;
    server_name ~^www\.(.*)\.com$;

    location / {
        root /code/test3;
        index index.html;
    }
}
[root@web01 conf.d]# echo "正则表达式" > /code/test3/index.html

5)配置default_server的配置文件

[root@web01 conf.d]# vim server4.conf 
server {
    listen 80 default_server;
    server_name localhost;

    location / {
        root /code/test4;
        index index.html;
    }
}
[root@web01 conf.d]# echo "default_server" > /code/test4/index.html

6)放在第一个的配置文件

[root@web01 conf.d]# vim a.conf 
server {
    listen 80;
    server_name localhost;

    location / {
        root /code/test5;
        index index.html;
    }
}
[root@web01 conf.d]# echo "第一个" > /code/test5/index.html

7)重启nginx测试

#配置hosts
10.0.0.7 www.test.com

二、nginx禁止IP访问

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术