return指令

return 执行后 后续的执行是不会执行的

  1. 语法:return code [text];
  2. return code URL;
  3. return URL;
  4. 上下文:serverlocationif
  5. 1XX:消息类
  6. 2XX:成功
  7. 3XX:重定向
  8. 4XX:客户端错误
  9. 5XX:服务器错误

rewrite指令

  1. 语法:rewrite regex replacement [flag];
  2. 上下文:serverlocationif
  3. 示例:rewrite /images/(.*\.jpg)$/pic/$1;
  4. last:这是讲images下面.jpg的文件 都重定到pic路径去查看对应的location
  5. break :这是讲images下面.jpg的文件 都重定到pic路径去查看对应的文件路径

flag说明

  • last 重写后的URL发起新请求,再此进入server段,尝试location中的匹配
  • break 直接使用重写后的URL,不再匹配其他location中语句
  • redirect 返回302临时重定向
  • permanent 返回301永久重定向

示例

  1. location /search {
  2. rewrite ^/(.*)http://www.cctv.com permanent;
  3. location /images {
  4. rewrite /images/(.*) /pics/$1 break;
  5. location /pics {
  6. rewrite /pics/(.*)/photos/$1 ;
  7. location /photos {
  8. }

if语句

  1. $variable 仅为变量时,值为空或以0开头字符串都会被当做false处理 类似布尔值
  2. =或!= 相等或不等比较
  3. ~或!~正则匹配或非正则匹配
  4. ~*正则匹配,不区分大小写
  5. -f或!-f检查文件存在或不存在
  6. -d或!-d检查目录存在或不存在
  7. -e或!-e检查文件、目录、符号链接等存在或不存在
  8. -x或!-X检查文件可执行或不可执行

示例

  1. location /search/ {
  2. if ($remote_addr = "192.168.184.1" ) {
  3. return 200 "test if OK in URL /search/";
  4. }
  5. location / {
  6. if ($uri = "/images /") {
  7. rewrite (.*) /pics/ break;
  8. }
  9. return 200"test if failed
  10. }

autoindex

  1. autoindex on|off 打开关闭 默认off
  2. autoindex_exact_size on|off 文件大小是否精确到字节 默认on
  3. autoindex_format json|html|xml|jsonp 默认html
  4. autoindex_localtime 文件时间格式 默认off

示例

  1. location /download/{
  2. root /opt/source;
  3. index a.html;
  4. autoindex on;
  5. autoindex exact size on;
  6. autoindex format html;
  7. autoindex_localtime off;
  8. }
  9. 访问 /opt/source/download/下面的文件
  10. 如果存在a.html直接访问这个页面