location配置 rewrite配置
5.1 作用
配置路径
location [ = | ~ | ~* | ^~ ] uri {
...
}
##在浏览器中输入的:http://www.jx.com/s?id=1&test=123,称作URL,即:统一资源定位符
##在WEB服务器中,对资源进行标识,URI,即统一资源标识符
写在server字段中。
5.2 匹配规则
= 精确匹配;
~ 正则匹配,区分大小写;
~* 正则匹配,不区分大小写;
^~ 匹配到即停止搜索;
5.3 匹配优先级
= > ^~ > ~ > ~* > 不带任何字符 “/”。
5.4 配置案例
server {
listen 80;
server_name www.jx.com;
# 只有当访问 www.jx.com/info.html 时才会匹配到/usr/share/nginx/html/info.html
location = /info.html { # 必须是匹配文件
root /usr/share/nginx/html;
}
# 当访问 www.jx.com/1.jpg 等路径时会去 /usr/share/nginx/images/1.jpg 找对应的资源
location ~ \.(jpeg|jpg|png|svg)$ {
root /usr/share/nginx/html/images;
}
# 当访问 www.jx.com/bbs/ 时会匹配上 /usr/share/nginx/html/bbs/index.html
location ^~ /bbs/ {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
1、location 中的反斜线,代表后面的名字是一个文件夹名
location /test {... } ##以上写法会优先查找目录,若目录不存在则查找同名文件 location /test/ {... }
2、return
停止处理请求,直接返回响应码或重定向到其他 URL ;执行 return 指令后, location 中后续指令将不会被执行。
return code [text]; return code URL; return URL;例如: location / {return 404; # 直接返回状态码 }location / {return 404 "pages not found"; # 返回状态码 + 一段文本 }location / {return 302 http://192.168.158.46/bbs ; # 返回状态码 + 重定向地址 }location / {return https://www.baidu.com ; # 返回重定向地址 }
六、rewrite配置
根据指定正则表达式匹配规则,重写 URL 。应用场景: 新老域名的更替!!!
6.1 语法
rewrite 正则表达式 要替换的内容 [flag];
6.2 可写入字段
server、location、if
示例:
rewirte /images/(.*\.jpg)$ /pic/$1; # $1是前面括号(.*\.jpg)的反向引用
flag 可选值的含义:
last: 重写后的 URL 发起新请求,再次进入 server 段,重试 location 的中的匹配;
break :直接使用重写后的 URL ,不再匹配其它 location 中语句;
redirect :返回 302 临时重定向;
permanent :返回 301 永久重定向。
6.3 配置案例
server{listen 80;server_name www.jx.com; # 要在本地hosts文件进行配置root /usr/share/nginx/html;location /search {rewrite ^/(.*) https://www.baidu.com redirect;}location /images {rewrite /images/(.*) /pics/$1;}location /pics {rewrite /pics/(.*) /photos/$1;}location /photos {} } ###生产环境不能使用.*,表示匹配任意!!! #解析: #当访问 www.jx.com/search 时,会自动帮我们重定向到 https://www.baidu.com; #当访问 www.jx.com/images/1.jpg 时,第一步重写 URL 为 www.jx.com/pics/1.jpg ,找到 pics 的 location ,继续重写 URL 为 www.jx.com/photos/1.jpg ,找到 /photos 的 location 后,去 html/photos 目录下寻找 1.jpg 静态资源。
6.4 if 指令
语法:if (condition) {...}
可写字段:server、location
示例:
location / {
if ($http_user_agent ~ Chrome) {
rewrite /(.*) /Chrome/$1 break;
}
if ($http_user_agent ~ Firefox) {
rewrite /(.*) /Firefox/$1 break;
}
}
##测试192.168.115.111/index.html
set $var value; #设置变量
condition 判断条件:
$variable 仅为变量时,值为空或以0开头字符串都会被当做 false 处理;
= 或 != 相等或不等;
~ 正则匹配;
! ~ 非正则匹配;
~* 正则匹配,不区分大小写;
-f 或 ! -f 检测文件存在或不存在;
-d 或 ! -d 检测目录存在或不存在;
-e 或 ! -e 检测文件、目录、符号链接等存在或不存在;
-x 或 ! -x 检测文件可以执行或不可执行;
**配置实例**
server {
listen 8080;
server_name www.jx.com;
root /usr/share/nginx/html;
location / {
if ( $uri = "/images/" ){
rewrite (.*) /pics/ break;
}
}
}
#当访问 www.jx.com/images/ 时,会进入 if 判断里面执行 rewrite 命令。
.5 autoindex
用户请求以 / 结尾时,列出目录结构,可以用于快速搭建静态资源下载网站。
注意: 不要在有混合业务的站点开启该功能,容易被攻击。
autoindex.conf 配置信息:
server {listen 80;server_name www.jx.com;location /download/ {root /usr/share/nginx/html;autoindex on; # 打开 autoindex,,可选参数有 on | offautoindex_exact_size on; # 修改为off,以KB、MB、GB显示文件大小,默认为on,以bytes显示出⽂件的确切⼤⼩autoindex_format html; # 以html的方式进行格式化,可选参数有 html | json | xmlautoindex_localtime off; # 显示的⽂件时间为⽂件的服务器时间。默认为off,显示的⽂件时间为GMT时间} } #当访问 www.jx.com/download/ 时,会把服务器 /usr/share/nginx/html/download/ 路径下的文件展示出来.
6.6 nginx配置中的常用变量
变量名 | 含义 |
---|---|
remote_add | 客户端IP地址 |
remote_port | 客户端端口 |
server_addr | 服务端IP地址 |
Server_port | 服务端端口 |
server_protocol | 服务端协议 |
binary_remote_addr | 二进制格式的客户端IP地址 |
connection | TCP连接的序号,递增 |
connection_request | TCP连接当前的请求数量 |
uri | 请求的URL,不包含参数 |
request ur | 请求的URL,包含参数 |
scheme | 协议名,http或https |
request metho | 请求方法 |
request_length | 全部请求的长度,包含请求行、请求头、请求体 |
args | 全部参数字符串 |
arg_参数名 | 获取特定参数值 |
is_args | URL中是否有参数,有的话返回?,否则返回空 |
query_string | 与args相同 |
host | 请求信息中的Host,如果请求中没有Host行,则在请求头中找,最后 使用nginx中设置的server_name。 |
http_user_agent | 用户访问方式 |
http_referer | 从哪些链接过来的请求 |
http_via | 每经过一层代理服务器,都会添加相应的信息 |
http_cookie | 获取用户cookie |
request time | 处理请求已消耗的时间 |
https | 是否开启了https,是则返回on,否则返回空 |
request_filename | 磁盘文件系统待访问文件的完整路径 |
document_root | 由URI和root/alias规则生成的文件夹路径 |
limit_rate | 返回响应时的速度上限值 |