找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 810|回复: 0

Nginx If指令使用

[复制链接]

296

主题

38

回帖

1274

积分

管理员

积分
1274
发表于 2021-4-16 17:41:23 | 显示全部楼层 |阅读模式
#常用结构:
http {
        //全局配置

        server {
                //全局配置
                root html;
                index index.php index.html;

                location {
                        //全局配置

                        if () {
                                //if配置
                        }
                }

                if () {
                        //if配置
                }
        }       
}

#Location和If的区别:
1.location和if是可以平级也或以嵌套.
2.root、index、alias、echo只能在location中使用.
3.location只代表地址,特批的是request_uri,/home/index.php?id=10&pass=123.
4.if可以使用nginx中所有内置变量,location可以控制大的地址,if在大的地址上细化一些工作.

#If语法结构:
if () {
       
}

#If运算符:
=或!=                等于或不等于
~                        正则表达式匹配(区分大小写)
~*                        正则表达式匹配(忽略大小写)
!~                        正则表达式不匹配

-f和!-f                判断文件是否存在
-d和!-d                判断目录是否存在
-e和!-e                判断文件或目录是否存在
-x和!-x                判断文件是否可执行

#If可使用的内置变量:
$args                      请求中的参数
$binary_remote_addr        远程地址的二进制表示
$body_bytes_sent           已发送的消息体字节数
$content_length            HTTP请求信息里的"Content-Length"
$content_type              请求信息里的"Content-Type"
$document_root             针对当前请求的根路径设置值
$document_uri              与$uri相同
$host                      请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$http_cookie               cookie 信息
$http_referer              来源地址
$http_user_agent           客户端代理信息
$http_x_forwarded_for      相当于网络访问路径
$limit_rate                对连接速率的限制         
$remote_addr               客户端地址
$remote_port               客户端端口号
$remote_user               客户端用户名,认证用
$request                   用户请求信息
$request_body              用户请求主体
$request_body_file         发往后端的本地文件名称      
$request_filename          当前请求的文件路径名
$request_method            请求的方法,比如"GET"、"POST"等
$request_uri               请求的URI,带参数   
$server_addr               服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址
$server_name               请求到达的服务器名
$server_port               请求到达的服务器端口号
$server_protocol           请求的协议版本,"HTTP/1.0"或"HTTP/1.1"
$uri                       请求的URI,可能和最初的值有不同,比如经过重定向之类的

#If语句实例:
1.args
if ($args ~* pass) {
    echo '123';
}

curl http://192.168.2.1/index.php -d "name=user1&pass=123" -G

2.http_referer
if ($http_referer !~* 192.168.2.3) {
    return http://www.baidu.com;
}

curl http://192.168.2.1/index.php -H "referer: 3.3.3.3" -Li

3.http_user_agent
判断不同的浏览器类型
if ($http_user_agent ~* trident) {
    echo 'ie';
}

if ($http_user_agent ~* chrome) {
    echo 'google';
}

if ($http_user_agent ~* firefox) {
    echo 'firefox';
}

curl http://192.168.2.1 -A "this is Trident browser"

4.remote_addr
if ($remote_addr !~* 192.168.2.3) {
    return http://www.baidu.com;
}

curl http://192.168.2.1 -LI

5.request_filename
if (!-e $request_filename) {
    return http://www.baidu.com;
}

curl http://192.168.2.1/aaaaa.php -I

6.uri
if ($uri ~* ^/home) {
    echo 'home';
}

if ($uri ~* ^/admin) {
    echo 'admin';
}

curl http://192.168.2.1/home/index.php
curl http://192.168.2.1/admin/index.php

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|外汇论坛 ( 粤ICP备16021788号 )

GMT+8, 2024-5-18 14:34 , Processed in 0.071823 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表