[记录]Nginx配置实现&&和||的方法实例
2018-01-31 20:39
621 查看
Nginx配置文件中if的&&和||的实现(nginx不支持&&和||的写法)
1.与(&&)的写法:
set $condiction '';
if ($http_user_agent ~ "Chrome"){
set $condiction a;
}
if ($args ~ "r=hao123"){
set $condiction "${condiction}b";
}
if ($condiction = ab){
rewrite ^/(.*)$ https://www.hao123.com/?tn=94408350_hao_pg; }
说明:当浏览器是Chrome并且url的参数是r=hao123的时候做重定向。
rewrite有四个flag,不带flag时默认是redirect(302),如下:
1)last(重写后的规则,会继续用重写后的值去匹配下面的location。)
2)break(重写后的规则,不会去匹配下面的location。使用新的规则,直接发起一次http请求了。)
3)permanent(301永久重定向,搜索引擎在抓取新内容的同时也将旧的网址替换为重定向之后的网址)
4)redirect(302临时重定向,搜索引擎会抓取新的内容而保留旧的网址)(网站换量的场景下使用)
2.或(||)的写法:
set $condiction 0;
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx1$"){
set $condiction 1;
}
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx2$"){
set $condiction 1;
}
if ($condiction){
rewrite ^/(.*)$ https://www.hao123.com/?tn=94408350_hao_pg; }
说明:当ip是xxx.xxx.xxx.xx1或xxx.xxx.xxx.xx2的时候做重定向。
3.结合上面两段代码,实现禁止IP访问,禁止Chrome浏览器并且url参数是r=hao123的访问。
set $condiction1 true;
set $condiction2 '';
if ($http_user_agent ~ "Chrome") {
set $condiction2 a;
}
if ($args ~ "r=hao123") {
set $condiction2 "${condiction2}b";
}
if ($condiction2 = ab) {
set $condiction1 false;
}
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx1$") {
set $condiction1 false;
}
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx2$") {
set $condiction1 false;
}
if ($condiction1 = false) {
return 403;
}
1.与(&&)的写法:
set $condiction '';
if ($http_user_agent ~ "Chrome"){
set $condiction a;
}
if ($args ~ "r=hao123"){
set $condiction "${condiction}b";
}
if ($condiction = ab){
rewrite ^/(.*)$ https://www.hao123.com/?tn=94408350_hao_pg; }
说明:当浏览器是Chrome并且url的参数是r=hao123的时候做重定向。
rewrite有四个flag,不带flag时默认是redirect(302),如下:
1)last(重写后的规则,会继续用重写后的值去匹配下面的location。)
2)break(重写后的规则,不会去匹配下面的location。使用新的规则,直接发起一次http请求了。)
3)permanent(301永久重定向,搜索引擎在抓取新内容的同时也将旧的网址替换为重定向之后的网址)
4)redirect(302临时重定向,搜索引擎会抓取新的内容而保留旧的网址)(网站换量的场景下使用)
2.或(||)的写法:
set $condiction 0;
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx1$"){
set $condiction 1;
}
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx2$"){
set $condiction 1;
}
if ($condiction){
rewrite ^/(.*)$ https://www.hao123.com/?tn=94408350_hao_pg; }
说明:当ip是xxx.xxx.xxx.xx1或xxx.xxx.xxx.xx2的时候做重定向。
3.结合上面两段代码,实现禁止IP访问,禁止Chrome浏览器并且url参数是r=hao123的访问。
set $condiction1 true;
set $condiction2 '';
if ($http_user_agent ~ "Chrome") {
set $condiction2 a;
}
if ($args ~ "r=hao123") {
set $condiction2 "${condiction2}b";
}
if ($condiction2 = ab) {
set $condiction1 false;
}
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx1$") {
set $condiction1 false;
}
if ($http_x_forwarded_for ~ " ?xxx\.xxx\.xxx\.xx2$") {
set $condiction1 false;
}
if ($condiction1 = false) {
return 403;
}
相关文章推荐
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- 实现php&nginx服务器配置的非主流配置方法
- 实现nginx&php服务器配置的非主流配置方法
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- 实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
- nginx+rsync+inotify实现负载均衡配置方法
- 反向代理概念以及apache,nginx配置反向代理实现负载均衡方法
- quartz实现定时功能实例详解(servlet定时器配置方法)
- Nginx配置统计流量带宽请求及记录实时请求状态的方法
- Nginx和Apache几种防盗链配置方法实例
- nginx配置:location配置方法及实例详解
- Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
- nginx实现防盗链配置方法介绍
- quartz实现定时功能实例详解(servlet定时器配置方法)
- 菜鸟nginx源码剖析 配置与部署篇(一) 手把手实现nginx "I love you"
- apache服务器如何实现多IP配置的方法实例
- spring 静态工厂 &实例工厂实例方法 & 实现factorybean接口
- nginx实现防盗链配置方法介绍
- Nginx实现异步访问mysql的配置方法