181人参与 • 2024-08-02 • 正则表达式
目录
nginx 是一个高性能的 web 服务器和反向代理服务器,它广泛用于托管网站、服务 api 和负载均衡。在 nginx 的配置中,location
块是一个非常重要的概念,它允许你根据请求的 uri(统一资源标识符)来匹配和处理客户端请求。
一、location
块的基本概念location
块用于在 nginx 配置文件中定义一个位置匹配规则。当客户端发送请求时,nginx 会根据请求的 uri 来匹配 location
块,并执行相应的处理指令。location
块可以嵌套在 server
块中,也可以嵌套在其他 location
块中。
二、location
块的语法location
块的基本语法如下:
location [修饰符] <匹配模式> {
# 处理指令
}
=
(精确匹配)、~
(区分大小写的正则表达式匹配)、~*
(不区分大小写的正则表达式匹配)等。三、location
块的匹配方式nginx 支持多种 uri 匹配方式:
前缀匹配:默认的匹配方式,不带修饰符,匹配 uri 的前缀。
location /api {
# 处理 /api 及其子路径的请求
}
精确匹配:使用 =
修饰符,仅当请求 uri 与指定字符串完全相等时匹配。
location = / {
# 仅处理根路径 / 的请求
}
正则表达式匹配:使用 ~
或 ~*
修饰符,分别表示区分大小写和不区分大小写的正则表达式匹配。
location ~* \.(jpg|png|gif)$ {
# 处理所有以 .jpg、.png 或 .gif 结尾的请求
}
目录匹配:以斜杠 /
结尾的匹配模式,用于匹配特定的目录。
location /images/ {
# 处理 /images/ 目录下的请求
}
四、location
块的优先级当有多个 location
块匹配同一个请求时,nginx 会根据以下规则来确定使用哪个 location
块:
=
)。~
或 ~*
),并使用第一个匹配的规则。五、location
块的应用场景location
块可以用于多种场景,以下是一些例子:
静态资源服务:
location /static/ {
alias /usr/share/nginx/html/static/;
}
这个 location
块用于服务 /usr/share/nginx/html/static/
目录下的静态文件。
代理转发:
location /api/ {
proxy_pass http://backend_server;
}
所有以 /api/
开头的请求都会被代理到后端服务器。
错误页面重定向:
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
当发生 404 错误时,nginx 会重定向到 /404.html
页面。
基于请求方法的处理:
location /api {
if ($request_method = post) {
rewrite ^ /api/post last;
}
}
location /api/post {
# 处理 post 请求
}
根据请求方法的不同,将请求重写到不同的 location
块进行处理。
限制访问:
location /restricted/ {
allow 192.168.1.0/24;
deny all;
}
这个 location
块限制只有来自特定 ip 范围的请求才能访问 /restricted/
路径。
六、location
块的嵌套location
块可以嵌套,这意味着你可以在一个 location
块内部定义另一个 location
块。这通常用于对特定的路径进行更细粒度的控制。
location / {
# 默认处理
location /admin {
# 对 /admin 路径的特殊处理
location ~ \.php$ {
# 对 /admin 路径下 php 文件的处理
}
}
}
在这个例子中,/admin
路径下的请求首先匹配外层的 location /
,然后进一步匹配内层的 location /admin
,如果请求的文件以 .php
结尾,则还会匹配最内层的 location ~ \.php$
。
七、location
块的指令在 location
块内部,你可以使用各种指令来控制请求的处理方式,例如:
proxy_pass
:将请求代理到后端服务器。rewrite
:重写请求的 uri。try_files
:尝试不同的文件或路径,直到找到一个存在的。root
和 alias
:设置请求文件的基本目录。index
:指定目录索引文件。autoindex
:启用或禁用目录列表。error_page
:定义错误页面。auth_basic
和 auth_basic_user_file
:设置 http 基本认证。下面是一个完整的 nginx 配置示例,展示了如何使用 location
块:
server {
listen 80;
server_name example.com;
# 网站根目录
root /var/www/html;
# 处理根路径
location = / {
index index.html;
}
# 处理静态资源
location /static/ {
alias /var/www/static/;
}
# 代理到后端应用服务器
location /api/ {
proxy_pass http://backend_server;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header x-forwarded-proto $scheme;
}
# 处理 php 文件
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
# 自定义错误页面
error_page 404 /404.html;
location = /404.html {
root /var/www/html;
internal;
}
}
在提供的示例配置中,我们定义了一个 server
块,它包含了多个 location
块,每个 location
块都有特定的用途和配置指令。下面详细讲解每个部分的作用:
server {
listen 80;
server_name example.com;
# 网站根目录
root /var/www/html;
# 处理根路径
location = / {
index index.html;
}
listen 80;
:指定 nginx 监听 http 请求的端口号,这里是 80 端口,即标准的 http 端口。server_name example.com;
:定义服务器名,当客户端请求的 host 头部与这个名称匹配时,就会使用这个 server
块的配置。root /var/www/html;
:设置网站的根目录,所有未命名的 location
块(即默认的 location /
)都会从这个目录下寻找资源。location = / { ... }
:这个 location
块精确匹配根路径 /
,并且配置了 index index.html;
,这意味着当访问根路径时,nginx 会自动寻找 /var/www/html
目录下的 index.html
文件来响应请求。 # 处理静态资源
location /static/ {
alias /var/www/static/;
}
location /static/ { ... }
:这个 location
块用于处理所有以 /static/
开头的请求。alias /var/www/static/;
:将请求的 uri 替换为指定的路径,也就是说,所有对 /static/
的请求都会映射到实际的目录 /var/www/static/
。 # 代理到后端应用服务器
location /api/ {
proxy_pass http://backend_server;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_set_header x-forwarded-proto $scheme;
}
location /api/ { ... }
:这个 location
块用于处理所有以 /api/
开头的请求。proxy_pass http://backend_server;
:将所有匹配的请求代理到名为 backend_server
的后端服务器。通常,backend_server
需要在 http
或 upstream
块中定义。proxy_set_header host $host;
:设置代理请求的 host
头部为客户端的原始请求中的 host
值。proxy_set_header x-real-ip $remote_addr;
:设置 x-real-ip
头部为客户端的 ip 地址。proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
:设置 x-forwarded-for
头部,这是一个用于标识请求来源的链式头部,可以追踪请求穿过多个代理的路径。proxy_set_header x-forwarded-proto $scheme;
:设置 x-forwarded-proto
头部为请求使用的协议(http
或 https
)。 # 处理 php 文件
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ \.php$ { ... }
:这个 location
块使用正则表达式匹配所有以 .php
结尾的文件。include snippets/fastcgi-php.conf;
:包含一个外部配置文件,通常这个文件包含了处理 php 文件所需的 fastcgi 参数。fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
:指定 fastcgi 传递的目标,这里是 php-fpm 监听的 unix socket 文件。 # 自定义错误页面
error_page 404 /404.html;
location = /404.html {
root /var/www/html;
internal;
}
}
error_page 404 /404.html;
:定义当发生 404 错误时,nginx 应该返回哪个页面。location = /404.html { ... }
:这个 location
块精确匹配 /404.html
路径。root /var/www/html;
:设置 /404.html
文件的根目录,因为这是一个内部重定向,所以需要明确指定根目录。internal;
:标记这个 location
块只能从内部重定向访问,而不能直接通过外部请求访问。通过这样的配置,nginx 可以根据请求的不同部分(如 uri、文件扩展名等)来决定如何处理请求,无论是返回静态文件、代理到后端应用服务器,还是处理脚本文件,都能得到妥善的处理。
通过 location
块的灵活配置,nginx 可以高效地处理各种类型的请求,无论是静态资源、动态内容还是代理转发,都能轻松应对。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论