Linux基础43 nginx多server优先级, nginx禁止IP访问, nginx的alias, nginx的try_file

发布时间 2023-11-14 18:23:30作者: 战斗小人

Nginx常见问题

一、nginx多server优先级

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

1.准备多个配置文件

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

    location / {
        root /code/test1;
        index index.html;
    }
}
[root@web01 conf.d]# vim server2.conf
server {        # 修改server2内容
    listen 80;
    # listen 80 default_server; # 配了default_server如果找不到匹配,就会找这个
    server_name localhost test2.com;

    location / {
        root /code/test2;
        index index.html;
    }
}
[root@web01 conf.d]# vim server3.conf
server {        # 修改server3内容
    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]# echo "test1" > /code/test1/index.html
[root@web01 conf.d]# echo "test2" > /code/test2/index.html
[root@web01 conf.d]# echo "test3" > /code/test3/index.html
[root@web01 conf.d]# chown -R www.www /code/    # 避免权限问题

3.启动nginx

[root@web01 conf.d]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 提醒有同样的配置内容

[root@web01 conf.d]# systemctl start nginx

4.配置hosts访问测试

10.0.0.7 test1.com test2.com test3.com

5.多server优先级总结

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

#如果通过ip访问,会直接到5,判断是否有default_server的server块,就走6

6.总结测试

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

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

    location / {
        root /code/test2;
        index index.html;
    }
}
[root@web01 conf.d]# vim server3.conf
server {
    listen 80;
    server_name localhost www.test.*;

    location / {
        root /code/test3;
        index index.html;
    }
}
[root@web01 conf.d]# vim server4.conf
server {
    listen 80;
    server_name localhost ~^www\.(.*)\.com$;

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

二、nginx禁止IP访问网站

1.禁止ip访问直接返回错误

# 根据上面多server的匹配规则,ip方格纹会直接走到第5步(理论上可以写一个排第一的配置,但不太好控制)
[root@web01 conf.d]# vim server4.conf
server {
    listen 80 default_server;
    server_name localhost;
    return 500;
}

2.引流的方式,访问IP跳转到主页

server {
    listen 80 default_server;
    server_name localhost;
    return 302 https://www.baidu.com;
}

3.返回指定的内容

server {
    listen 80 default_server;
    server_name localhost;
    default_type text/plain;    # 定义返回的类型,否则nginx无法识别,会下载文件
    return 200 "页面错误...";
}

三、nginx的包含 include

一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。

假设现在希望快速的关闭一个站点,该怎么办?
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于人类可读。
3.两个目录
include /etc/nginx/online/*.conf    # 线上使用
mv .conf /ect/nginx/offline            # 下线的配置,临时撤站

 

四、nginx路径的root与alias

root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件删,alias是一个目录别名的定义,
root则是最上层目录的定义 root的处理结果是: root路径
+location路径 alias的处理结果是: 使用alias定义的路径

1.root和alias配置

[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;
    
    location /picture {
        root /code;        # 相当于访问 /code/picture
    }
}
#使用root的时候,访问 http://image.com/picture/1.jpg,实际上会到服务器的/code/picture/ 目录下面寻找1.jpg文件
http://image.com/picture/2020/02/1.jpg   实际上会到服务器的/code/picture/2020/02/目录下面寻找

[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;
    
    location /picture {
        alias /code;    # 定义一个别名, 访问/picture变为访问 /code下找
    }
}
#如果使用的是alias, 访问 http://image.com/picture/1.jpg,实际上是到服务器的/code/目录下寻找1.jpg文件
http://image.com/picture/bar/hello.html     实际上访问路径为/code/bar/hello.html

2.生产中配置方式

server {
    listen 80;
    server_name image.com;
    
    location / {
        alias /code;
    }
    
    location ~* \.(jpg|png|gif)$ {    # 动静分离
        alias /code/images;
    }
}

五、Nginx的 try_file路径匹配

nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。
在元素名后面添加斜杠 / 表示这个是目录。如果文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。 (try_files用的比较少)

1.配置try_file

# 配置nginx
[root@web01 conf.d]# vim try.com.conf
server {
    listen 80;
    server_name try.com;
    
    location / {
        root /code/try;
        try_files $uri /404.html;    # 找$uri对应的文件,如果没有匹配到就访问后面的404文件(/指上面的/code/try),如果404.html也找不到,就报500错误
    }
}
# 创建目录与文件
[root@web01 conf.d]# mkdir /code/try
[root@web01 conf.d]# echo 4040404 > /code/try/404.html
[root@web01 conf.d]# echo "try_file.index" > /code/try/index.html

# 访问try.com,匹配$uri,而域名后面为空,匹配不到内容,所以匹配/404.html,返回的内容是/code/try/404.html
[root@web01 conf.d]# curl try.com
4040404
# 访问try.com/index.html,匹配$uri匹配到了/index.html,/code/try/index.html
[root@web01 conf.d]# curl try.com/index.html
try_file.index

#注:配try_files,如果域名后面什么不加,访问try.com,不会默认加上index.html,匹配不到就调转到404.html  
#如果一定要访问 try.com ,要能访问到index.html,就要加$uri/,修改配置如下
[root@web01 conf.d]# vim try.com.conf
server {
    listen 80;
    server_name try.com;

    location / {
        root /code/try;
        try_files $uri $uri/ /404.html;    # 找$uri/就找到/code/try,就会找下面的index.html
    }
}
# 访问try.com, $uri匹配不到内容,交给$uri/匹配,匹配到的是"空/",所以访问的链接是 try.com/,则能访问到/code/try/index.html

2.实例配置

#1. 配置nginx
[root@web01 conf.d]# cat try.conf
server {
    listen 80;
    server_name try.com;
    root /code;
    
    location / {
        try_files $uri $uri/ @java;    # 当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,当一定要加@,内部子请求(内部307跳转)
    }
    
    location @java {
        proxy_pass http://172.16.1.8:8080;    # 配置后端tomcat
    }
}