nginx location指令详解

来源:https://www.cnblogs.com/xiaoliangup/p/9175932.html

Nginx的HTTP配置主要包括三个区块,结构如下:
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区段

通过指定模式来与客户端请求的URI相匹配,基本语法如下:location [=|~|~*|^~|@] pattern{……}

1、没有修饰符 表示:必须以指定模式开始,如:

server {
  server_name baidu.com;
  location /abc {
    ……
  }
}

那么,如下是对的:
http://baidu.com/abc
http://baidu.com/abc?p1
http://baidu.com/abc/
http://baidu.com/abcde

2、=表示:必须与指定的模式精确匹配

server {
server_name sish
  location = /abc {
    ……
  }
}
那么,如下是对的:
http://baidu.com/abc
http://baidu.com/abc?p1
如下是错的:
http://baidu.com/abc/
http://baidu.com/abcde

3、~ 表示:指定的正则表达式要区分大小写

server {
server_name baidu.com;
  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

4、~* 表示:指定的正则表达式不区分大小写

server {
server_name baidu.com;
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

5、^~ 类似于无修饰符的行为,也是以指定模式开始,不同的是,如果模式匹配,
那么就停止搜索其他模式了。
6、@ :定义命名location区段,这些区段客户段不能访问,只可以由内部产生的请
求来访问,如try_files或error_page等

查找顺序和优先级
1:带有“=“的精确匹配优先
2:没有修饰符的精确匹配
3:正则表达式按照他们在配置文件中定义的顺序
4:带有“^~”修饰符的,开头匹配
5:带有“~” 或“~*” 修饰符的,如果正则表达式与URI匹配
6:没有修饰符的,如果指定字符串与URI开头匹配

Location区段匹配示例

location = / {
  # 只匹配 / 的查询.
  [ configuration A ]
}
location / {
  # 匹配任何以 / 开始的查询,但是正则表达式与一些较长的字符串将被首先匹配。
  [ configuration B ]
}
location ^~ /images/ {
  # 匹配任何以 /images/ 开始的查询并且停止搜索,不检查正则表达式。
  [ configuration C ]
}
location ~* \.(gif|jpg|jpeg)$ {
  # 匹配任何以gif, jpg, or jpeg结尾的文件,但是所有 /images/ 目录的请求将在Configuration C中处
  理。
  [ configuration D ]
} 各
请求的处理如下例:
■/ → configuration A
■/documents/document.html → configuration B
■/images/1.gif → configuration C
■/documents/1.jpg → configuration D

root 、alias指令区别

location /img/ {
    alias /var/www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/var/www/image/目录找文件
location /img/ {
    root /var/www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/var/www/image/img/目录下找文件。] 

alias是一个目录别名的定义,root则是最上层目录的定义。

还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~

原文地址:https://www.cnblogs.com/laijinquan/p/11227319.html

时间: 2024-08-27 13:03:53

nginx location指令详解的相关文章

nginx location优先级详解

nginx中location有几种: 1.前缀,可以有=或^~修饰,比如 location  /       /开头的 location /img/     /img/开头的 location = /a.htm      刚好/a.htm location ^~ /d    匹配后不再检查正则表达式location.注意这个意思不是非正则表达式!除了~开头其他都是非正则表达式,也就是前缀匹配 2.正则表达式,固定~或~*(不区分大小写)开头,比如: location ~  \.html$ loc

ngnix location指令详解

Nginx的HTTP配置主要包括三个区块,结构如下: 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;

nginx location 配置详解

指令作用 匹配指定的请求uri(请求uri不包含查询字符串,如http://localhost:8080/test?id=10,请求uri是/test) 语法形式 location [ = | ~ | ~* | ^~ | @] /uri/ { configuration } 匹配模式及顺序 匹配字符串分为两种:普通字符串(literal string)和正则表达式(regular expression),其中 ~ 和 ~* 用于正则表达式, 其他前缀和无任何前缀都用于普通字符串. 1.先匹配普通

nginx.conf指令详解

#redis.conf # Redis configuration file example. # ./redis-server /path/to/redis.conf ################################## INCLUDES ################################### #这在你有标准配置模板但是每个redis服务器又需要个性设置的时候很有用. # include /path/to/local.conf # include /path/t

九爷带你了解 nginx 日志配置指令详解

nginx日志配置指令详解 日志对于统计排错来说非常有利的. 本文总结了nginx日志相关的配置如 access_log.log_format.open_log_file_cache.log_not_found.log_subrequest.rewrite_log.error_log. nginx有一个非常灵活的日志记录模式.每个级别的配置可以有各自独立的访问日志.日志格式通过log_format命令来定义.ngx_http_log_module是用来定义请求日志格式的. 1. access_l

Nginx配置文件nginx.conf中文详解(转)

######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ] error_log /usr/local/nginx/logs/error.log info; #进程pid文件 pid /usr/local/nginx

Nginx配置文件优化详解

Nginx配置文件优化详解 对nginx进行优化是重点也是难点,这里给出一些常用的优化措施,以及相关参数的所代表的意思.有些参数需要结合公司服务器进行设置. 全局变量的优化: #定义Nginx运行的用户和用户组user  www  www: #启动进程,通常设置成和cpu的数量相等 worker_processes  8: #为每个进程分配cpu. worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 

Nginx配置文件nginx.conf中文详解(转载)

请参考:http://wiki.nginx.org/Main #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ] error_log ar/loginx/error.log info; #进程文件 pid ar/runinx.pid; #一个nginx进程打开的最多文件描述符

nginx学习三 nginx配置项解析详解及代码实现

nginx配置项解析详解及代码实现 0回顾 在上一节,用nginx简单实现了一个hello world程序:当我们在浏览器中输入lochost/hello ,浏览器就返回:hello world.为什么会这样呢,简单一点说就是当我们请求访问hello这个服务,nginx就会看配置文件中是否有,如果有,根据具体的handler处理后把处理的结果返回给用户,没有就返回not found. location /hello { test_hello ;//无参数的配置 这其实是一个简单的配置.这节我们来