nginx详细配置

1、location语法

1、功能和请求

  • 用来匹配不同的url请求,从而对这些请求做出不同的处理和响应

  • 网站的请求URL和location配置的URL做匹配

  • 总路径 = root指定的根目录 + location 指定的路径

2、匹配规则

1、=(精确匹配)

        location =/test/  { # /test精确匹配URL的路径
            root   html;  # nginx查找文件的根目录是html
            index  index.html index.htm;
        }

# 浏览器访问
http://localhost/test/ 就访问到了/html/test/index.html

# 必须是/test/的才行,其他的访问不到

2、正则匹配

1、区分大小写(~)

        location ~/test {
            root   html;
            index  index.html index.htm;
        }


# 访问localhost/test 能访问成功

# /Test的时候不能访问成功

2、~*(执行正则匹配,忽略大小写)

        location ~*/test {
            root   html;
            index  index.html index.htm;
        }

# 访问 /test和Test都能进行访问

4、前缀匹配

1、不带有字符的前缀匹配

  • 匹配请求路径的前缀

  • 如果有多个前缀匹配,选择匹配路径最长的那个

location = /exact {
    return 200 "Exact Match\n";
}

location /static/ {
    return 200 "Static Prefix\n";
}

location ~ \.jpg$ {
    return 200 "Regex Match JPG\n";
}

location / {
    return 200 "Default Match\n";
}


# 请求为/static/image.jpg


# 首先前缀匹配,初步匹配到的是/static

# 匹配不会进行停止,正则匹配/static/image.jpg

# 所以最终的结果就会覆盖掉前缀匹配


2、带有字符的前缀匹配(^~)

  • 表示普通字符匹配上以后不再进行正则匹配

  • 控制匹配的优先级

  • 优先级高于,普通的前缀匹配(没有修饰符的location内)

  • 如果存在多个location定义,带^~的块优先匹配,不会回到其他的前缀匹配或者其他正则表达,就是匹配上了,匹配就停止

location = /exact {
    return 200 "Exact Match\n";
}

location ^~ /static/ {
    return 200 "Static Prefix\n";
}

location ~ \.jpg$ {
    return 200 "Regex Match JPG\n";
}

location / {
    return 200 "Default Match\n";
}

# 请求为/static/image.jpg

# 直接匹配到/static 匹配就停止了,不会继续进行匹配了

5、关于访问的时候带不带/的问题

1、根路径(www.baidu.com/)

  • 带不带/无所谓的,不带有/的话,浏览器访问的时候会自动的进行补全

  • nginx会将其解析为/

  • http请求始终会包含尾部的/这个是浏览器的默认行为

2、目录路径(localhost/some-dir/)

  • 尾部/表示目录,缺少/表示文件

  • 根据URL语法定义的,没有/表示文件,有/表示目录

  • 这个约定可以根据URL的格式决定是否查找文件或者目录

  • nginx的处理逻辑

    • 访问的是/some/dir,nginx会认为这个是文件

    • 如果找不到some-dir的话,nginx会将其视为一个目录并找到/some-dir/,返回一个301重定向到/some-dir/

        location /test {
            root   html;
            index  index.html index.htm;
        }

# nginx认为 html/test文件,找不到的就寻找html/test/index.html文件

# linux的访问
# 不带有/的访问,没有这个文件的话,就是重定向

[root@test nginx]# curl localhost:/test
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.25.0</center>
</body>
</html>


# 带有/的访问,访问的就是 html/test/index.html文件
[root@test nginx]# curl localhost:/test/
123


# 如果html/test文件存在的话,就匹配
[root@test nginx]# cat html/test 
test

[root@test nginx]# curl localhost:/test
test

3、location匹配的规则(谁先匹配的规则)

  • 首先是精确匹配 =

  • 然后就是^~的前缀匹配

  • 正则匹配~ 和 ~*

  • 普通的前缀匹配(最长的前缀优先)

  • 默认匹配 /

  • 以上就是匹配的规则,对使用^~又会性能,避免正则匹配的开销

2、其他参数

1、root参数

  • 定义访问网页的根目录的

  • 默认是html,这个是个相对路径,相较于nginx的配置文件目录下面的html目录这个

  • 可以自定义的

        location / {
            root /page;  # 网页的目录在/page下面
            index  index.html index.htm;  # 首页文件为/page/index.html这个文件
        }


[root@test nginx]# ls /page/index.html 
/page/index.html

2、listen参数

  • 可以自定义端口

3、server_name参数

  • 定义主机名

  • 多个server,就是多个虚拟机主机,然后根据不同的server_name就可以实现一个主机虚拟出多个主机,多个网站出来

、nginx案例

1、访问页面是列表而不是文件内容

        location /test/ {  # 定义匹配规则 /test/
            alias   html/test/;  # 请求为/test/的时候,映射到html/test/
            autoindex on; # 启用目录浏览功能
            autoindex_exact_size off;  # 设置文件大小可读性
            autoindex_format html;  # 设置目录列表格式为html格式
            index  index.html index.htm;  # 首页文件
        }

posted @ 2025-11-10 14:35  老乔的港口  阅读(2)  评论(0)    收藏  举报