nginx学习(十一)——nginx的配置系统7之if
来源:互联网 发布:淘宝评价了还能退款吗 编辑:程序博客网 时间:2024/05/21 14:01
if
The specified condition is evaluated. If true, this module directives specified inside the braces are executed, and the request is assigned the configuration inside the if directive. Configurations inside the if directives are inherited from the previous configuration level.
A condition may be any of the following:a variable name; false if the value of a variable is an empty string or “0”;
Before version 1.0.1, any string starting with “0” was considered a false value.
comparison of a variable with a string using the “=” and “!=” operators;
matching of a variable against a regular expression using the “~” (for case-sensitive matching) and “~*” (for case-insensitive matching) operators. Regular expressions can contain captures that are made available for later reuse in the $1..$9 variables. Negative operators “!~” and “!~*” are also available. If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
checking of a file existence with the “-f” and “!-f” operators;
checking of a directory existence with the “-d” and “!-d” operators;
checking of a file, directory, or symbolic link existence with the “-e” and “!-e” operators;
checking for an executable file with the “-x” and “!-x” operators.
Examples:
if ($http_user_agent ~ MSIE) { rewrite ^(.*)$ /msie/$1 break;}if ($http_cookie ~* "id=([^;]+)(?:;|$)") { set $id $1;}if ($request_method = POST) { return 405;}if ($slow) { limit_rate 10k;}if ($invalid_referer) { return 403;}
举例
location /name { #proxy_pass http://provider; if ($request_method = GET) { return 405; } proxy_pass http://192.168.237.186:10086/; proxy_set_header Host $host; proxy_set_header X-Real-Scheme $scheme; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }这里访问http://xxcupid2.qunar.com/name/test的话,因为是GET请求,所以会进入if块儿,然后返回405。
- nginx学习(十一)——nginx的配置系统7之if
- nginx学习(六)——nginx的配置系统3之upstream_module(上)
- nginx学习(七)——nginx的配置系统3之upstream_module(下)
- nginx学习(八)——nginx的配置系统4之healthcheck module
- nginx学习(九)——nginx的配置系统5之rewrite
- nginx学习(十)——nginx的配置系统6之server_name
- nginx学习(五)——nginx的配置系统1
- nginx学习(五)——nginx的配置系统2
- Nginx学习总结(3)——Nginx配置及应用场景之高级配置
- Nginx学习——Nginx基本配置
- Nginx学习笔记1—Nginx(Win32) 配置详解
- Nginx学习笔记1—Nginx(Win32) 配置详解
- nginx的配置系统
- nginx的配置系统
- nginx反向代理配置----nginx的学习之路
- nginx负载均衡配置---nginx的学习之路
- Nginx学习之十一-Nginx启动框架处理流程
- Nginx学习之十一-Nginx启动框架处理流程
- 并查集
- MongoDB 的用户认证
- sass实战演练06 - 把布局拆分为12列(2):拆分和偏移
- 行内元素和块元素
- Android Volley使用之二:Volley请求网络图片
- nginx学习(十一)——nginx的配置系统7之if
- 用amcharts动态生成饼图
- POJ 2065 SETI(高斯消元解同余方程组)
- JavaScript学习笔记之DOM对象操作html元素
- 组原 之 数字
- [bzoj3729]Gty的游戏
- ADL:社交网络分析与数据挖掘(DAY2)
- Cookie和Session区别(2)
- LeetCode 389:Find the Difference