nginx 04 uri跳转 显示状态码

本文详细解读了网站location模块的配置,如何根据URI处理不同格式文件(图片、视频、文档),以及如何利用rewrite和return指令实现页面跳转,包括临时和永久重定向。通过实例演示,展示了如何利用Nginx配置进行URL重定向和路径调整。
摘要由CSDN通过智能技术生成

一. 网站服务location模块(匹配不同格式文件)

location模块作用说明:根据不同uri信息,进行不同处理
灵活调整站点目录
访问图片信息: /html/www/img/ oldboy.jpg
访问视频信息: /html/www/av/ oldboy.mp4
访问文档信息: /html/www/txt/ oldboy.txt

配置文件中的信息

server {
        server_name www.kk.com;
        root /code;
        listen 80;
        index index.html;

        location  ~ \.jpg$  {
                root  /code/img;
        }
        location  ~ \.mp4$  {
                root  /code/av;
        }
        location  ~ \.txt$  {
                root  /code/txt;
        }

}


/code/
|-- av
|   `-- kk.mp4
|-- img
|   `-- kk.jpg
`-- txt
    `-- kk.txt

准备环境信息
mkdir /html/www/{img,av,txt} -p

Syntax: location [ = | ~ | ~* | ^~ ] uri { 对匹配uri进行哪些处理 }
location @name { … }
Default: —
Context: server, location

= — 进行uri信息精确匹配 = /sa/ www.oldboy.com/sa/
~ — 进行uri信息匹配(区分大小写匹配) ~ /sa/ www.oldboy.com/sa/ www.oldboy/sa/01/oldboy.html
~* — 进行uri信息匹配(不区分大小写匹配) ~* /sa/ www.oldboy.com/sa/ www.oldboy.com/SA/
^~ — 进行uri信息优先匹配 问题不识别正则信息

location  = /sa/  {
  return  302;
}
location  ~ /sa  {
  return  303;
}
location  ~* \.jpg  {
  return  402;
}
location  ^~ /sa/ {
  return  504;
}

root — 可以指定站点目录

配置信息:

location  ~ /sa/  {
  root  /html/www/;
}

www.oldboy.com/sa/ --> /html/www/sa/首页文件

alias — 可以指定站点目录 识别新的路径

配置信息:

location  ^~ /sa/  {
  alias /html/www/sa_new/; 
}

^必有
sa 自动识别成sa_new
作用 临时活动网站 临时匹配new页面
www.oldboy.com/sa/ 页面信息 —> /html/www/sa_new/页面

二. 网站页面实现跳转功能:

rewrite
return

1. rewrite 指令

Syntax: rewrite regex(正则匹配信息) replacement(重写/跳转指定信息) [flag] 跳转方式;
Default: —
Context: server, location, if

跳转方式:
break:将访问uri信息直接进行跳转改变,不会重新再建立新的连接访问
原理过程:
用户访问地址信息时 — 服务端根据uri信息进行location匹配 — 完成跳转(break)
请求报文信息直接发生改变
原有请求报文 请求uri /break 改变 新的请求信息 uri /test — 访问站点目录中test目录/index.html
识别location /test 里面信息 return ok info

出席晚会 匹配是否穿西服 --> 现场给你一套西服

last: 将访问uri信息进行跳转改变,会让客户端重新建立新的访问,从而匹配新的location
原理过程:
用户访问地址信息时 — 服务端根据uri信息进行location匹配 — 完成跳转(last)
重新由客户端发起访问 rewrite.oldboy.com/test/ — 服务端根据uri信息进行location – 匹配/test

出席晚会 匹配是否穿西服 --> 请穿上西服再过来 --> 回家换上西服

2. redirect 临时跳转 302 不会将跳转过程进行保存,每次跳转都有服务端进行控制 ***

permanent 永久跳转 301 会将跳转过程保存在浏览器中

www.jd01.com --> 服务端 --> www.jd02.com

实践配置过程:

rewrite.conf
server {
  listen     80;
  server_name   rewrite.oldboy.com;
  root       /html/rewrite;
  location  ~  ^/break/ {
     rewrite  ^/break/  /test/   break;
  }
  location  ~ ^/last/   {
     rewrite  ^/last/   /test/   last;
  }
  location  /test/  {
     default_type   application/json;
     return 200 'ok info';
  }
}

访问 http://rewrite.oldboy.com/break 显示页面是404页面

方法二:return 指令
作用:

  1. 主要实现url信息跳转
  2. 主要实现固定uri信息跳转

Syntax: return code状态码 [text]文本信息; return code URL; return URL;
Default: —
Context: server, location, if

return指令跳转测试:
www.oldboy.com/sa/oldboy.jpg --> www.oldboy.com/sa_new/oldboy.jpg

配置信息:

server {
   listen       80;
   server_name  www.oldboy.com;
   location  /  {
     root   /html/www;
     index  index.html;
   }
   location  ~ /sa/ {
     return 302 http://www.oldboy.com/sa_new/oldboy.jpg;
   }
}

跳转实际配置说明:
案例一: 将uri信息进行跳转重新
用户访问网站uri信息 /abc/1.html 实际访问 /ccc/bbb/2.html
实现跳转过程:
http://rewrite.oldboy.com/abc/1.html --> http://rewrite.oldboy.com/ccc/bbb/2.html
环境准备:
cd /html/rewrite
mkdir ccc/bbb -p
echo ccc_bbb_2 >ccc/bbb/2.html
跳转配置:
cat /etc/nginx/conf.d/rewrite.conf

server {
  listen     80;
  server_name   rewrite.oldboy.com;
  root       /html/rewrite;
  location  /abc {
     #rewrite  (.*)  /ccc/bbb/2.html  redirect; 
     return 302  /ccc/bbb/2.html;
  } 
}

rewrite (.*) http://www.newurl.com $ 1;

案例二:将uri信息进行跳转重新
用户访问 uri /2019/ccc/bbb/2.html 目录信息进行改变 /2020/ccc/bbb/2.html
实现跳转过程:
http://rewrite.oldboy.com/2019/ccc/bbb/2.html --> http://rewrite.oldboy.com/2020/ccc/bbb/2.html
环境准备:
cd /html/rewrite
mkdir 2020/ccc/bbb/ -p
echo 2020_2 >2020/ccc/bbb/2.html
跳转配置:
location /2019 {
rewrite ^/2019/(.*)$ /2020/$1 redirect;
}

案例三:将uri信息进行跳转重写
用户访问 uri oldboy-11-22-33.html 实际访问 /oldboy/11/22/33/oldboy_33.html
实现跳转:
http://rewrite.oldboy.com/oldboy-11-22-33.html --> http://rewrite.oldboy.com/oldboy/11/22/33/oldboy.html
环境准备:
cd /html/rewrite
mkdir oldboy/11/22/33/ -p
echo oldboy-11-22-33 > oldboy/11/22/33/oldboy.html
跳转配置:
location /
rewrite ^/oldboy-(.)-(.)-(.*).html /oldboy/$1/$2/$3/oldboy.html redirect;

方式一:多个虚拟主机实现跳转 www.oldboy.com —> www.newboy.com

server { 
   listen       80;
   server_name  www.oldboy.com;
   rewrite (.*)  http://www.newboy.com$1;
}
server {
   listen       80;
   server_name  www.newboy.com;
   location /  {
     root   /html/www/;
     index  index.html;
   }
}

方式二:借助if判断指令实现跳转

server {
   listen       80;
   server_name  www.oldboy.com;
   rewrite (.*)  http://www.newboy.com$1;
}
server {
   listen       80;
   server_name  www.oldboy.com www.newboy.com;
   location /  {
     if ($http_host = www.oldboy.com) {
        rewrite (.*) http://www.newboy.com$1;
     }
     root   /html/www/;
     index  index.html;
   }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值