核心指令return
原创2026/3/5小于 1 分钟
return 功能如下:
停止处理请求,直接返回响应码或重定向到其他 URL;
执行 return 指令后,location 中后序指令将不会被执行;
return 语法结构
return code [text]; # 如果返回 2XX 的,text 才有意义,text 会在 body 中;return code URL; #主要用于重定向;return URL; #须以 http 或者 https 开头的;
code + text
server {
listen 80;
server_name www.jiamei.com;
location / {
return 200 'your success';
}
}code + URL
server {
listen 80;
server_name localhost;
location / {
return 302 /bbs;
}
location /bbs {
root html;
index index.html;
}
}302 表示临时性重定向。访问一个 Url 时,被重定向到另一个 url 上。常用于页面跳转。
URL
server {
listen 80;
server_name www.jiamei.com;
location / {
return http://baidu.com;
}
}直接重定向到了百度了
至此,本章节的学习就到此结束了,如有疑惑,可对接技术客服进行相关咨询。