Linux架构19 nginx多server优先级, nginx禁止IP访问, nginx的alias, nginx的try_file, 显示指定错误页面

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如果找不到匹配,就会找这个(如果ip访问,找不到会找这个,但企业里很少ip访问)
    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.如果都没匹配到,也没写default_server,那么就找到匹配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;  # 或者写serser_name _;下划线代表为空,什么都没有就是ip访问
    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定义的路径

alias 只能用在location中,而root可以用在http、server、location中

#注意:
1.如果location后面路径带/,alias匹配的内容也要带/,否则会找不到文件。使用alias时,location不要写文件,否则alias因只指代路径会找不到
2.alias不支持php跳转,如果要支持,还需要配置其他东西

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用的比较少)

try_files file ... uri; #按顺序查询资源是否存在,返回第一个匹配到的,如果没有匹配到,会 内部重定向到最后一个资源
try_files file ... =code; #最后还可以加一个状态码

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/,就是把$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
    }
}
server{
       listen 80;
       server_name www.m99-magedu.com;
       root /var/www/html/www.m99-magedu.com;

       try_files $uri $uri.html $uri/index.html /index.html;
       location /a{
               try_files $uri $uri.html;
       }
       location /b{
               try_files $uri $uri.html =418;
       }
       error_log /var/log/nginx/www.m99-magedu.com.error.log;
}

# 测试
# http://www.m99-magedu.com/xyz 先找 xyz,往后依次是 xyz.html xyz/index.html 
/index.html
# http://www.m99-magedu.com/a 如果有 a 或 a.html 会匹配到,返回状态码是200,如果没有则
返回 500错误页面
# http://www.m99-magedu.com/b 如果有 b 或 b.html 会匹配到,返回状态码是200,如果没有则
返回 418错误页面

 

六、显示指定错误页面

第一种:Nginx自己的错误页面

第二种:反向代理的错误页面

[root@web01 conf.d]# vim error.conf
server {
    listen 80;
    server_name error.linux.com;
    root /code;
    
    location / {
        index index.html;
        #error_page跳转本地文件,/ 代表站点目录
        error_page 404 /404.jpg;    # 如果返回404,就读取返回404.jpg(也可以配在server层)
        #error_page跳转网络地址,配置的必须是完整地址
        #error_page 404 https://www.baidu.com;
    }
}

# 把error_page配置在外面,好处可以再次跳转,404.jpg就不用放在站点目录下
server {
    listen 80;
    server_name error.linux.com;
    root /code;
    error_page 404 /404.jpg;
    
    location = /404.jpg {    # =优先级最高
        root /images;    # 404.jpg到images目录下去找
    }
}

 例:

#客户端重定向
server{
       listen 80;
       server_name www.m99-magedu.com;
       root /var/www/html/www.m99-magedu.com;
       #支持多个状态码
       error_page 400 404 502 503 504 error.html; #自定义指定状态码的错误页面
        
       location = /error.html {
               root /data/errors;
       }
}
[root@ubuntu ~]# ls /data/errors/
error.html

#服务端重定向
server{
       listen 80;
       server_name www.m99-magedu.com;
       root /var/www/html/www.m99-magedu.com;
       
       error_page 400 404 =302 /index.html; #通过302 重定向到 index.hmtl(服务器内部完成跳转)
}

server{
       listen 80;
       server_name www.m99-magedu.com;
       root /var/www/html/www.m99-magedu.com;
       error_page 400 404 =302 http://www.baidu.com; #临时重定向
       error_page 402 503 =301 http://www.baidu.com; #永久重定向
}

 

posted @ 2023-11-14 18:19  战斗小人  阅读(587)  评论(0)    收藏  举报