zoukankan      html  css  js  c++  java
  • nginx---return


    return code [text]; #返回客户端指定的状态码和文本说明
    return code URL;
    return URL;
    停止处理,并返回给客户端指定的响应码(包括: 204, 400, 402 — 406, 408, 410, 411, 413, 416, 500 — 504),并对 301, 302, 303, 307, 308跳转到URL
    rewrite_log on | off;
      是否开启重写日志, 发送至error_log(notice level)
    set $variable value;
      用户自定义变量
      注意:变量定义和调用都要以$开头

      

      一、使用return 拒绝特定浏览器访问,如curl

      1、返回一个空

    server {
            listen 80;
            server_name www.a.net;
            root /data/site1/;
            access_log /var/log/nginx/a.net.log443 main;
            location / {
                if ( $http_user_agent ~* curl ){
                    return 444;
                    }
            }
    }

      2、返回状态码405

    server {
            listen 80;
            server_name www.a.net;
            root /data/site1/;
            access_log /var/log/nginx/a.net.log443 main;
            location / {
                if ( $http_user_agent ~* curl ){
                    return 405;
                    }
            }
    }

      二、返回文字

    server {
            listen 80;
            server_name www.a.net;
            root /data/site1/;
            access_log /var/log/nginx/a.net.log443 main;
            location / {
                if ( $http_user_agent ~* curl ){
                    return 405 "Deny curl";
                    }
            }
    }

    2、测试访问:

    [18:20:23 root@localhost ~]#curl www.a.net 
    Deny curl[18:22:53 root@localhost ~]#

      三、跳转其他url,如百度(返回302,临时重定向)

    server {
            listen 80;
            server_name www.a.net;
            root /data/site1/;
            access_log /var/log/nginx/a.net.log443 main;
            location / {
                if ( $http_user_agent ~* curl ){
                    return http://www.baidu.com;
                    }
            }
    }

      2、返回301 ,永久重定向

    server {
            listen 80;
            server_name www.a.net;
            root /data/site1/;
            access_log /var/log/nginx/a.net.log443 main;
            location / {
                if ( $http_user_agent ~* curl ){
                    return 301 http://www.baidu.com;
                    }
            }
    }

    301 永久重定向,可以缓存,302临时重定向,307利用缓存,内部跳转

       使用curl 模拟其他任意浏览器,既可以访问

    curl -A abc www.a.net
    /data/site1index.html

    curl -A 模拟浏览器,abc是模拟的浏览器,不一定存在

    ------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------- 博客均为个人笔记,无所追求,仅供参考~~~ QQ--2382990774
  • 相关阅读:
    C++操作Kafka使用Protobuf进行跨语言数据交互
    聊聊Disruptor 和 Aeron 这两个开源库
    DTrace arg0-kernel mode and arg1-user mode
    top
    how to write your first linux device driver
    how to compile and replace ubuntu kernel
    linux du
    c++ rvo vs std::move
    【Android】wifi开发
    无线局域网络 WIFI/WAPI/WLAN区别浅析
  • 原文地址:https://www.cnblogs.com/alexlv/p/14846462.html
Copyright © 2011-2022 走看看