return指令
return 执行后 后续的执行是不会执行的
语法:return code [text];return code URL;return URL;上下文:server、location、if1XX:消息类2XX:成功3XX:重定向4XX:客户端错误5XX:服务器错误
rewrite指令
语法:rewrite regex replacement [flag];上下文:server、location、if示例:rewrite /images/(.*\.jpg)$/pic/$1;last:这是讲images下面.jpg的文件 都重定到pic路径去查看对应的locationbreak :这是讲images下面.jpg的文件 都重定到pic路径去查看对应的文件路径
flag说明
- last 重写后的URL发起新请求,再此进入server段,尝试location中的匹配
- break 直接使用重写后的URL,不再匹配其他location中语句
- redirect 返回302临时重定向
- permanent 返回301永久重定向
示例
location /search {rewrite ^/(.*)http://www.cctv.com permanent;location /images {rewrite /images/(.*) /pics/$1 break;location /pics {rewrite /pics/(.*)/photos/$1 ;location /photos {}
if语句
$variable 仅为变量时,值为空或以0开头字符串都会被当做false处理 类似布尔值=或!= 相等或不等比较~或!~正则匹配或非正则匹配~*正则匹配,不区分大小写-f或!-f检查文件存在或不存在-d或!-d检查目录存在或不存在-e或!-e检查文件、目录、符号链接等存在或不存在-x或!-X检查文件可执行或不可执行
示例
location /search/ {if ($remote_addr = "192.168.184.1" ) {return 200 "test if OK in URL /search/";}location / {if ($uri = "/images /") {rewrite (.*) /pics/ break;}return 200"test if failed}
autoindex
autoindex on|off 打开关闭 默认offautoindex_exact_size on|off 文件大小是否精确到字节 默认onautoindex_format json|html|xml|jsonp 默认htmlautoindex_localtime 文件时间格式 默认off
示例
location /download/{root /opt/source;index a.html;autoindex on;autoindex exact size on;autoindex format html;autoindex_localtime off;}访问 /opt/source/download/下面的文件如果存在a.html直接访问这个页面
