nginx配置中的location使用详解

【1】 location介绍

location简单实例如下:

location / {
        root   html;
        #首页文件。以下按顺序匹配
        index  index.html index.htm;
    }

location语法格式:

location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }

在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射。ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置。

  • = :精确匹配。用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配成功,就停止继续向下搜索并立即处理该请求。
  • ~:用于表示 uri 包含正则表达式,并且区分大小写。
  • ~*:用于表示 uri 包含正则表达式,并且不区分大小写,即看到~就表示正则表达式。
  • ^~:前缀匹配。用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location 块中的正则 uri 和请求字符串做匹配。
  • 不带符号:匹配起始于此uri的所有的url;

实例如下

location = / {
    [ configuration A ]
}

location / {
    [ configuration B ]
}

location /documents/ {
    [ configuration C ]
}

location ^~ /images/ {
    [ configuration D ]
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

/匹配规则A,“/index.html”匹配规则B,“/documents/document.html”匹配规则C, “/images/1.gif”匹配规则D,“/documents/1.jpg”匹配规则E。

注意:如果 uri 包含正则表达式,则必须要有 ~ 或者 ~* 标识

匹配优先级=, ^~, ~/~*,不带符号

  • 精确匹配=

  • 前缀匹配^~(立刻停止后续的正则搜索)

  • 按文件中顺序的正则匹配~或~*

  • 匹配不带任何修饰的前缀匹配。


【2】 location中root与alias

location ${path} {
    root html; # 会将${path}一起加入到代理的(/html/${path})路径中
    alias html; # 不会将${path}一起加入到代理的(html/)路径中
}

root

设置web资源路径映射,用于指明用户请求的url所对应的本地文件系统上的文档所在目录路径,可指定相对路径也可使用绝对路径。可用的位置:http, server, location, if in location。如请求/i/top.gif,则按照如下配置目标文件为/data/w3/i/top.gif:

location /i/ {
    root /data/w3;
}

alias path

为指定路径定义一个替代值,仅能用于location上下文。如访问/i/top.gif,按照如下location配置,则目标文件为/data/w3/images/top.gif

location /i/ {
    alias /data/w3/images/;
}

location中使用root指令和alias指令的意义不同:

  • root,目标文件路径为root指定的value+URI;

  • alias,给定的路径对应于location中的/uri/右侧的/

参考官网文档示例

A path to the file is constructed by merely adding a URI to the value of the root directive. 
If a URI has to be modified, the alias directive should be used.

也就是说当使用root指令时,文件的路径构造只是把URI的值拼接在了root指令值的后面!


【3】 location与proxy_pass中的/

3.1 location中的/

location如果没有“/”时,请求就可以模糊匹配以字符串开头的所有字符串,而有“/”时,只能精确匹配字符本身。

目标访问地址:http://192.168.88.129/edu/index.html

实例如下

  location ~  /edu/ {
	proxy_pass http://127.0.0.1:8080;
	}
	
 location ~  /edu {
	proxy_pass http://127.0.0.1:8080;
	}

/edu 可以匹配/edu*/*格式的路径URL,而/edu/ 只能匹配/edu/*格式的URL。这里注意到proxy_pass后面没有加/也没有加目录,则会把location的值直接拼接到proxy_pass后面。

3.2 proxy_pass中的/

目标URL地址:http://192.168.88.129/edu/index.jsp

3.2.1 location加/,proxy_pass加/不加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/index.jsp
location   /edu/ {
	proxy_pass http://127.0.0.1:8080/;
	#8080后面加了/ 则将URI中index.jsp拼接到后面
}

3.2.2 location加/,proxy_pass不加/不加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/edu/index.jsp
location   /edu/ {
	proxy_pass http://127.0.0.1:8080;
	#这里8080后面没有加/ 也没有加目录,则直接将请求URI拼接到后面
}

3.2.3 location加/,proxy_pass不加/加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/aaindex.jsp
location   /edu/ {
	proxy_pass http://127.0.0.1:8080/aa;
}

proxy_pass加了一级目录/aa但是末尾没有加/,而且location中末尾加了/,则将请求URI进行切割得到index.jsp拼接到proxy_pass后面。

3.2.4 location加/,proxy_pass加/加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/aa/index.jsp
location   /edu/ {
	proxy_pass http://127.0.0.1:8080/aa/;
}

也就是说如果proxy_pass加了目录,则会从URI-location得到的结果进行拼接。


3.2.5 location末尾不加/,proxy_pass加/不加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080//index.jsp 
# 注意//
location   /edu {
	proxy_pass http://127.0.0.1:8080/;
	#8080后面加了/ 则将URI中/index.jsp拼接到后面
}

3.2.6 location末尾不加/,proxy_pass不加/不加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/edu*/index.jsp
location   /edu {
	proxy_pass http://127.0.0.1:8080;
	#这里8080后面没有加/ 也没有加目录,则直接将请求URI拼接到后面
}

3.2.7 location末尾不加/,proxy_pass不加/加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/aa/index.jsp
location   /edu {
	proxy_pass http://127.0.0.1:8080/aa;
}

3.2.8 location末尾不加/,proxy_pass加/加目录

#  http://127.0.0.1:8080/edu/index.jsp --->http://127.0.0.1:8080/aa//index.jsp
location   /edu {
	proxy_pass http://127.0.0.1:8080/aa/;
}

也就是说如果proxy_pass加了目录,则会从URI-location得到的结果进行拼接。

但是如下配置将会提示错误:

  location  ~ /edu {
	proxy_pass http://127.0.0.1:8080/;
	}
	
  location  ~ /edu/ {
	proxy_pass http://127.0.0.1:8080/;
	}

nginx: [emerg] “proxy_pass” cannot have URI part in location given by regular expression, or inside named location, or inside “if” statement, or inside “limit_except” block in /usr/local/nginx/conf/nginx.conf:54


【4】 动静分离之静态资源映射配置

这里以图片实例,将图片存储在/usr/local/nginx/image路径下。

nginx配置文件实例如下:

 location ~* \.(jpg|png|jpeg)$ {
        root /usr/local/nginx/;
        expires 3d;
}

通过 location 指定不同的后缀名实现不同的请求转发。通过 expires 参数设置,可以使浏览器缓存过期时间,减少与服务器之前的请求和流量。具体 Expires 定义:是给一个资源设定一个过期时间,也就是说无需去 服务端验证,直接通过浏览器自身确认是否过期即可,所以不会产生额外的流量。此种方法非常适合不经常变动的资源。(如果经常更新的文件,不建议使用 Expires 来缓存)。

如这里设置 3d ,表示在这 3 天之内访问这个 URL ,发送一个请求,比对服务器该文件最后更新时间没有变化,则不会从服务器抓取,返回状态码 304如果有修改,则直接从服务器重新下载,返回状态码 200 。

浏览器访问如下http://192.168.88.129/image/1.jpg
在这里插入图片描述

这里需要注意浏览器访问uri是/image/1.jpg,这个location拦截后会拼接到root后去服务器找,也就是 /usr/local/nginx/image/1.jpg


【5】 autoindex展示文件路径下内容

如下所示,访问image路径然后展示其下(location里面映射的路径)的文件列表。
在这里插入图片描述
这里需要用到autoindex 指令,也就是ngx_http_autoindex_module 模块。其往往用于处理以/结尾的请求并展示一个路径下内容列表。配置实例如下:

 location /image/ {
	root /usr/local/nginx/;
	autoindex on;
}

也可以返回json格式:

  location /image/ {
	root /usr/local/nginx/;
	autoindex on;
	autoindex_format json;
 }

在这里插入图片描述

更多内容可以参考官网:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html

【6】location中的try_files

官网文档地址:点击查看
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

按指定顺序检查文件是否存在,并使用找到的第一个文件进行请求处理,该处理在当前上下文中执行。文件的路径是file根据root和alias指令从参数 构造的 。可以通过在名称末尾指定斜杠(例如“ $uri/”)来检查目录的存在。如果未找到任何文件,则进行内部重定向到uri最后一个参数中指定的文件 。

几点说明:

  • 按指定的file顺序查找存在的文件,并使用第一个找到的文件进行请求处理
  • 查找路径是按照给定的root或alias为根路径来查找的
  • 如果给出的file都没有匹配到,则重新请求最后一个参数给定的uri,就是新的location匹配
  • 如果最后一个参数是 = 404 ,若给出的file都没有匹配到,则最后返回404的响应码

$uri 是请求文件的路径,$uri/ 是请求目录的路径。

最后一个参数也可以指向一个命名的位置,如下面的示例所示。从版本0.7.51开始,最后一个参数也可以是code如404。

#参数: $uri 表示当前请求的URI,不带任何参数
location /mall/ {
  root   htmlmall;
  index  index.html index.htm;
  try_files $uri $uri/ /mall/index.html =404;
}

假设说请求是/mall/images/1.jpg

  • ①nginx会先去找/htmlmall/mall/images/1.jpg这个文件,
  • ② 如果①找不到则会找/htmlmall/mall/images/1.jpg/这个目录下的index索引页
  • ③ 如果②也没找到,则会去请求/mall/index.html—一个新的请求
  • ④ 如果③也没找到,返回404

注意,如果try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页。 即 访问127.0.0.1/mall/images不会去访问 127.0.0.1/mall/images/index.html 。

如果想实现项目直接解压部署在htmlmall下,不再创建一个mall文件夹的话,root换成alias即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值