• WEB服务-Nginx之5-Server和Location和Include


    WEB服务-Nginx之5-Server和Location和Include

    Server

    ​ 在开始处理一个http请求时,nginx会取出header头中的Host变量,按优先级与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求。优先级匹配结果如下:

    1. 首先选择所有的字符串完全匹配的server_name。(完全匹配)
    2. 选择通配符在前面的server_name,如 *.oldboy.com www.oldboy.com
    3. 选择通配符在后面的server_name,如 www.oldboy.* www.oldboy.com www.oldboy.cn
    4. 最后选择使用正则表达式匹配的server_name
    5. 如果全部都没有匹配到,那么将选择配置listen port default_server;的server块
    6. 如果没有配置listen port default_server;的server块,那么将选择匹配listen端口的第一个server块

    注意:建议配置相同端口,不同域名,这样不会出现域名访问冲突。


    创建配置文和站点文件测试

    [root@web01 ~]# cat > /etc/nginx/conf.d/server.conf << EOF
    server {
        listen 80;
        server_name linux.test.com;
    
        location / {
            root /code;
            index server1.html;
        }
    }
    
    server {
        listen 80;
        server_name *.test.com;
    
        location / {
            root /code;
            index server2.html;
        }
    }
    
    server {
        listen 80;
        server_name linux.test.*;
    
        location / {
            root /code;
            index server3.html;
        }
    }
    
    server {
        listen 80;
        server_name ~^linux.(.*).com$;
    
        location / {
            root /code;
            index server4.html;
        }
    }
    
    server {
        listen 80 default_server;
        server_name localhost;
    
        location / {
            root /code;
            index server5.html;
        }
    }
    EOF
    
    [root@web01 ~]# echo linux.test.com > /code/server1.html
    [root@web01 ~]# echo *.test.com > /code/server2.html
    [root@web01 ~]# echo linux.test.* > /code/server3.html
    [root@web01 ~]# echo '~^linux.(.*).com$' > /code/server4.html
    [root@web01 ~]# echo default_server > /code/server5.html
    

    重载添加host解析

    [root@web01 ~]# systemctl reload nginx
    [root@web01 ~]# echo 10.0.0.7 linux.test.com www.test.com linux.test.cn linux.aaa.com aaa.test.aaa >> /etc/hosts
    

    访问测试

    [root@web01 ~]# curl linux.test.com
    linux.test.com
    [root@web01 ~]# curl www.test.com
    *.test.com
    [root@web01 ~]# curl linux.aaa.com
    ~^linux.(.*).com$
    [root@web01 ~]# curl aaa.test.aaa
    default_server
    # 注释掉default_server,重载再测试
    [root@web01 ~]# systemctl reload nginx
    [root@web01 ~]# curl aaa.test.aaa
    linux.test.com
    

    Location

    Nginx 使用Location可以控制访问网站的路径

    官方文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#location


    Location语法示例

    Syntax:	    location [ = | ~ | ~* | ^~ ] uri { ... }
                location @name { ... }
    Default:	—
    Context:	server, location
    

    uri是用户请求的字符串,即域名后面的web文件路径。

    Location正则匹配可以使用测试判断语句

    -f # 是否存在文件
    -d # 是否存在目录
    -e # 是否存在文件或目录
    -x # 文件是否可执行
    

    Location语法优先级顺序

    匹配符 匹配规则 优先级
    = 精确匹配,并停止搜索,不再向下匹配。 1
    ^~ 匹配以某个字符串开头的查询,并停止搜索,不再向下匹配。 2
    ~ 区分大小写的正则匹配 3
    ~* 不区分大小写的正则匹配 4
    !~ 区分大小写不匹配的正则匹配 5
    !~* 不区分大小写不匹配的正则匹配 6
    / 匹配任何以指定模式开始的查询,正则表达式与较长的字符串优先。 7

    验证配置:

    server {
        listen 80;
        
        location =/ {
            return 200 "location =/";
        }
        
        location ^~/ {
            return 200 "location ^~/";
        }
        
        location ~/ {
            return 200 "location ~/";
        }
        
        location ~*/ {
            return 200 "location ~*/";
        }
        
        #location / {
        #    return 200 "location /"; # 和^~语法重复,先注释下
        #}
    }
    

    !~!~*属于排除法,符合匹配规则时不会匹配到,也就是轮到它了也不会执行里面的语句。


    验证测试:

    从上往下依次测试,测试一次注释掉一级,重载Nginx:systemctl reload nginx

    # 优先级最高精确匹配=
    [root@web01 ~]# curl 127.0.0.1
    location =/
     
    # 注释掉=,重载Nginx
    [root@web01 ~]# curl 127.0.0.1
    location ^~/
     
    # 注释掉^~,去掉/的注释,重载Nginx
    [root@web01 ~]# curl 127.0.0.1
    location ~/
    
    # 注释掉~,重载Nginx
    [root@web01 ~]# curl 127.0.0.1
    location ~*/
    

    Location应用场景

    # 通用匹配,任何请求都会匹配到
    location / {
        ...
    }
     
    # 严格区分大小写,匹配以.php结尾的都走这个location    
    location ~ .php$ {
        ...
    }
     
    # 不区分大小写匹配,只要用户访问.jpg,gif,png,js,css 都走这条location
    location ~* .png$ {
        ...
    }
    
    # 实现资源动静分离
    location ~ .(php|jsp|asp) {
           root /opt/dynamic;
    }
    location ~* .(jpg|gif|html|txt|js|css)$  {
           root /opt/static;
    }
    

    root与alias

    root的处理结果是:root路径+location路径

    alias的处理结果是:使用alias定义的路径


    • alias是目录别名的定义,如果uri以“/”结束,alias必须要用“/”结束,否则403
    location /img/ {
        alias /var/www/;  # 实际路径是/var/www/
    }
    
    location ~* /img/(.+.(gif|png|jpeg)) { # location用正则表达式则必须包含捕获语句(也就是括号())
        alias /usr/local/images/$1;         # alias则必须要引用这些捕获值
    }
    
    • root是最上层目录的定义,文件的绝对路径等于 root+location
    location /img/ {
        root /var/www;    # 实际路径是/var/www/img/
    }
    
    • proxy_pass是反向代理配置,用“/”结束不附加location配置路径
    location   /test/  {
         proxy_pass http://127.0.0.1:8080; # 实际将请求转发到http://127.0.0.1:8080/test/
    }
    
    location   /test/  {
         proxy_pass http://127.0.0.1:8080/; # 实际将请求转发到http://127.0.0.1:8080/
    }
    
    • 线上配置
    server {
        listen 80;
        server_name image.oldboy.com;
    
        location / {
            root /code;
        }
    
        location ~* ^.*.(png|jpg|gif)$ {
            alias /code/images/;
        }
    }
    

    Include

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

    假设现在希望快速的关闭一个站点,该怎么办?

    1. 如果是写在nginx.conf中,则需要手动注释,比较麻烦
    2. 如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释,Include包含的作用是为了简化主配置文件,便于人类可读。
    inlcude /etc/nginx/online/*.conf; 	# 线上使用的配置
    mv /etc/nginx/online/test.conf /etc/nginx/offline/ 	# 保留配置,不启用(下次使用再移动到online中)
    

    Include还可以包含其他经常重复的配置文件,比如

    inlcude /etc/nginx/conf.c/*.conf;   # 四层负载的配置文件
    inlcude fastcgi_params;             # fastcgi的配置参数,默认在/etc/nginx/下
    inlcude rewrite.conf;               # rewrite的配置参数
    ... ...
    

    方便其他配置文件调用。


  • 相关阅读:
    lombok注解详细介绍
    基于注解的SpringMVC大致开发流程
    Thymeleaf知识点总结
    thymeleaf相关知识
    Windows 命令行查看占用端口,并关闭操作
    身份证号码的正则表达式及验证详解
    mysql的查询过程和SQL语句优化
    MySQL数据库知识点整理
    MySQL数据库
    MySQL数据库
  • 原文地址:https://www.cnblogs.com/backups/p/nginx5.html
Copyright © 2020-2023  润新知