Nginx是一款自由的、开源的、高性能的HTTP服务器和反向代理服务器,此文主要是记录一下在Nginx配置时,location应该如何配置。html
Nginx的HTTP配置主要包括三个区块,结构以下:nginx
http { //这个是协议级别 include mime.types; default_type application/octet-stream; keepalive_timeout 65; gzip on; server { //这个是服务器级别 listen 80; server_name localhost; location / { //这个是请求级别 root html; index index.html index.htm; } } }
location是Nginx服务器很是核心的配置,用于匹配指定的uri(请求uri不包含查询字符串,如http://localhost:8080/test?id=10
,请求uri是/test)。通常在修改Nginx配置时,大部分也是围绕着location这个配置进行修改。正则表达式
不部分状况下实际上是不须要很复杂的配置的,作个动静分离已经能知足绝大多数单体服务的;而在分布式环境下,某个server中一般须要配置不少location,来将请求分发到不一样的微服务下,此时你必须很是熟悉location 的配置和匹配规则,不然真是无从下手。express
下面来看一下一个简单的location配置:服务器
location / { root home/; index index.html; }
location [ = | ~ | ~* | ^~ | @] uri {...}
app
意思是能够用 =
或 ~
或 ~
*或 ^~
或@
符号为前缀,固然也能够没有前缀(由于 [A] 是表示可选的 A ; A|B 表示 A 和 B 选一个,上面的样例就属于没有符号前缀的例子),紧接着是 uri ,再接着是{…} 指令块,整个意思是对于知足这样条件的 uri 适用指令块 {…} 的指令。分布式
全部的模式能够分为两种,匹配顺序也是基于这两种模式来进行处理的:微服务
=
、 ^~
三种模式的 uri; ~
或 ~*
前缀两种模式的uri; =
精确匹配。若是发现精确匹配,nginx中止搜索其余匹配 ^~
命中的,则跳过第4步,直接到第5步;总结一下就是: 普通命中顺序无所谓,由于是按命中精准度来肯定的 ;正则命中区分顺序,由于是从前日后命中的,命中一个后就不会继续匹配下一个正则。性能
无前缀
表示:必须以指定模式开始:代理
server { listen 80; server_name localhost; location /abc { …… } } 那么,以下是对的: http://baidu.com/abc http://baidu.com/abc?p1 http://baidu.com/abc/ http://baidu.com/abcde
^~
表示:相似于无前缀修饰符的行为,区别是,若是此模式匹配,是会中止搜索正则匹配的,可是会继续搜索普通模式。=
表示:必须与指定的uri精确匹配
server { listen 80; server_name localhost; location = /abc { …… } } 那么,以下是对的: http://baidu.com/abc http://baidu.com/abc?p1 以下是错的: http://baidu.com/abc/ http://baidu.com/abcde
~
表示:指定的正则表达式要区分大小写
server { listen 80; server_name localhost; location ~ ^/abc$ { …… } } 那么,以下是对的: http://baidu.com/abc http://baidu.com/abc?p1=11&p2=22 以下是错的: http://baidu.com/ABC http://baidu.com/abc/ http://baidu.com/abcde
~*
表示:指定的正则表达式不区分大小写
server { listen 80; server_name localhost; location ~* ^/abc$ { …… } } 那么,以下是对的: http://baidu.com/abc http://baidu..com/ABC http://baidu..com/abc?p1=11&p2=22 以下是错的: http://baidu..com/abc/ http://baidu..com/abcde
@
表示:这些location区段客户端不能访问,只能够由内部产生的请求来访问,如try_files或error_page等,以error_page为例:
server { listen 80; server_name localhost; error_page 404 @fallback location /abc { //检测文件4.html和5.html,若是存在正常显示,不存在就去查找@qwe值 try_files /4.html /5.html @fallback; } location @fallback { proxy_pass http://www.baidu.com; --跳转到百度页面 } }
当uri匹配上/abc
时,会按顺序检测文件的存在性,而且返回找到的第一个文件,最后一项就是跳转到百度,这种写法能够替代本来经常使用的rewrite,貌似能够提升解析效率;
Nginx指定文件路径有两种方式root和alias,两种指令有不一样的使用方法和做用域,root能够配置在http、server、location、if区块中,可是alias只能配置在location区块中。
root与alias主要区别在于Nginx如何解释location后面的uri,这会使二者分别以不一样的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
location ^~ /appImg/{ root /home/nginx; }
这个location至关于访问服务器上的文件路径: /home/nginx/appImg/abc.jpg
alias的处理结果是:使用alias路径替换location路径
location ^~ /appImg/{ alias /home/nginx/; }
这个location至关于访问服务器上的文件目录:/home/nginx/abc.jpg(即alias不会使用location后面配置的路径),并且若是alias 指定的是目录,后面必定要加上 /
,不然会找不到文件。