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 语句;

}

Logo

一起探索未来云端世界的核心,云原生技术专区带您领略创新、高效和可扩展的云计算解决方案,引领您在数字化时代的成功之路。

更多推荐