nginx 常见问题一

Nginx常见问题

一、nginx多server优先级

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

1.准备多个配置文件

[root@web01 /etc/nginx/conf.d]# vim test1.conf
server {
    listen 80;
    server_name localhost test1.com;

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

[root@web01 /etc/nginx/conf.d]# vim test2.conf
server {
    listen 80;
    server_name localhost test2.com;

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

[root@web01 /etc/nginx/conf.d]# vim test3.conf
server {
    listen 80;
    server_name localhost test3.com;

    location / {
        root /code/test3;
        index index.html;
    }
}
[root@web01 conf.d]#

2.配置多个站点文件

[root@web01 /etc/nginx/conf.d]# mkdir /code/test{1..3}
[root@web01 /etc/nginx/conf.d]# echo test1111111111111 > /code/test1/index.html
[root@web01 /etc/nginx/conf.d]# echo test2222222222222 > /code/test2/index.html
[root@web01 /etc/nginx/conf.d]# echo test3333333333333 > /code/test3/index.html

3.配置hosts请求页面

10.0.0.7 test1.com test2.com test3.com

#请求域名时,可以请求到域名对应的页面,请求IP时,返回的页面时配置文件中第一个配置的站点

4.多server优先级总结

# 在开始处理一个HTTP请求时,Nginx会读取header(请求头)中的host(域名),与每个server中的server_name进行匹配,
# 来决定用哪一个server标签来完成处理这个请求,有可能一个Host与多个server中的server_name都匹配,
# 这个时候就会根据匹配优先级来选择实际处理的server。优先级匹配结果如下:

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

5.多server优先级配置测试

#配置
[root@web01 /etc/nginx/conf.d]# cat server1.conf 
server {
    listen 80;
    server_name localhost linux.test.com;

    location / {
        root /code/server1;
        index index.html;
    }
}
[root@web01 /etc/nginx/conf.d]# cat server2.conf 
server {
    listen 80;
    server_name localhost *.test.com;

    location / {
        root /code/server2;
        index index.html;
    }
}
[root@web01 /etc/nginx/conf.d]# cat server3.conf 
server {
    listen 80;
    server_name localhost linux.test.*;

    location / {
        root /code/server3;
        index index.html;
    }
}
[root@web01 /etc/nginx/conf.d]# cat server4.conf 
server {
    listen 80;
    server_name localhost ~^linux\.(.*)\.com$;

    location / {
        root /code/server4;
        index index.html;
    }
}
[root@web01 /etc/nginx/conf.d]# cat server5.conf 
server {
    listen 80 default_server;
    server_name localhost;

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

#站点文件
[root@web01 /etc/nginx/conf.d]# echo linux.test.com > /code/server1/index.html
[root@web01 /etc/nginx/conf.d]# echo *.test.com > /code/server2/index.html
[root@web01 /etc/nginx/conf.d]# echo linux.test.* > /code/server3/index.html
[root@web01 /etc/nginx/conf.d]# echo '~^linux\.(.*)\.com$' > /code/server4/index.html
[root@web01 /etc/nginx/conf.d]# echo default_server > /code/server5/index.html

6.测试优先级

#配置hosts
10.0.0.7 linux.test.com

#访问测试
压缩配置,测试
压缩配置,测试
posted @ 2020-09-09 15:05  nick_xm  阅读(165)  评论(0编辑  收藏  举报