nginx的return语法使用

nginx的return语法使用

该指令一般用于对请求的客户端直接返回响应状态码。在该作用域内return后面的所有nginx配置都是无效的。

可以使用在server、location以及if配置中。除了支持跟状态码,还可以跟字符串或者url链接。

1).return直接返回状态码:

a).直接返回状态码的return案例:

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

    root /data/wwwroot/www.1.com;

    index index.html;

    return 403;

    #xxxx            #return下面有啥也不会被执行

}

[root@localhost ~]# echo index > /data/wwwroot/www.1.com/index.html

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com

<html>

<head><title>403 Forbidden</title></head>

<body>

<center><h1>403 Forbidden</h1></center>

<hr><center>nginx/1.15.9</center>

</body>

</html>

b).直接返回状态码的return案例—匹配访问的url包含相关内容的地址访问

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

    root /data/wwwroot/www.1.com;

    index index.html;

    if ($request_uri ~ "\.htpasswd|\.bak")

    {

        return 405;

    }

}

[root@localhost ~]# echo index > /data/wwwroot/www.1.com/index.html

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com/xxx/.htpasswd/xxx.html

<html>

<head><title>405 Not Allowed</title></head>

<body>

<center><h1>405 Not Allowed</h1></center>

<hr><center>nginx/1.15.9</center>

</body>

</html>

[root@localhost ~]# curl www.1.com/xxx/.bak/

<html>

<head><title>405 Not Allowed</title></head>

<body>

<center><h1>405 Not Allowed</h1></center>

<hr><center>nginx/1.15.9</center>

</body>

</html>

[root@localhost ~]# curl www.1.com/xxx/xxx.html

<html>

<head><title>404 Not Found</title></head>

<body>

<center><h1>404 Not Found</h1></center>

<hr><center>nginx/1.15.9</center>

</body>

</html>

2).return返回字符串:

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

    root /data/wwwroot/www.1.com;

    index index.html;

    return 200 "hello";       #return 200 "$host $request_uri"; 或者返回变量的值:www.1.com /

    #如果要想返回字符串,必须要加上状态码,否则会报错

}

[root@localhost ~]# echo index > /data/wwwroot/www.1.com/index.html

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com/index.html

hello

3).return返回url

主配置文件http模块下添加:/usr/local/nginx/conf/nginx.conf   mkdir /usr/local/nginx/conf/vhost

include vhost/*.conf;        #添加引用一个新的目录下的配置文件

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

    root /data/wwwroot/www.1.com;

    index index.html;

    return 301 http://www.baidu.com;

}

[root@localhost ~]# echo index > /data/wwwroot/www.1.com/index.html

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

[root@localhost ~]# cat /etc/hosts

127.0.0.1 www.1.com

[root@localhost ~]# curl www.1.com -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.15.9

Date: Sat, 19 Dec 2020 08:49:05 GMT

Content-Type: text/html

Content-Length: 169

Connection: keep-alive

Location: http://www.baidu.com

注意:

如果写成:return http://$host$request_uri; 在浏览器中会提示“重定向的次数过多”。

状态码如果写成200,则会返回给一个字符串,不会跳转,如果要跳转页面则需要写成301或302或不写(默认302)

[root@localhost ~]# cat  /usr/local/nginx/conf/vhost/www.1.conf

server{

    listen 80;

    server_name www.1.com;

    root /data/wwwroot/www.1.com;

    index index.html;

    return 200 http://www.baidu.com;

}

[root@localhost ~]# curl www.1.com

百度一下,你就知道

return相关示例:

示例1

if ($request_method = POST)             //当请求的方法为POST时,直接返回405状态码

{

    return 405;                        //在该示例中并未用到rewrite规则,if中支持用return指令。

}

示例2

if ($http_user_agent ~ MSIE)             //user_agent带有MSIE字符的请求,直接返回403状态码

{

    return 403;

}

//如果想同时限制多个user_agent,还可以写成这样

if ($http_user_agent ~ "MSIE|firefox|spider")

{

    return 403;

}

示例3

if(!-f $request_filename)                 //当请求的文件不存在,将会执行下面的rewrite规则

{

    rewrite 语句;

}

示例4

if($request_uri ~* 'gid=\d{9,12}/')   //\d表示数字,{9,12}表示数字出现的次数是9到12次,如gid=123456789/就是符合条件的。

{

    rewrite 语句;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

运维实战课程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值