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;
}

扩展:nginx+lua,实现upstream是由lua从redis中读取配置动态生成的。

server {
listen 80;
server_name _;
server_name_in_redirect off;
port_in_redirect off;
root /root/html; location / {
set $upstream "";
rewrite_by_lua '
-- load global route cache into current request scope
-- by default vars are not shared between requests
local routes = _G.routes -- setup routes cache if empty
if routes == nil then
routes = {}
ngx.log(ngx.ALERT, "Route cache is empty.")
end -- try cached route first
local route = routes[ngx.var.http_host]
if route == nil then
local redis = require "redis"
local client = redis.connect("localhost", 6379)
route = client:get(ngx.var.http_host)
end -- fallback to redis for lookups
if route ~= nil then
ngx.var.upstream = route
routes[ngx.var.http_host] = route
_G.routes = routes
else
ngx.exit(ngx.HTTP_NOT_FOUND)
end
'; proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_connect_timeout 10;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://$upstream;
}
}

最新文章

  1. java语言 打印素数实例
  2. 如何解决sublime text 2总是在新窗口中打开文件及文件夹
  3. SQL Server附加数据库问题
  4. Centos 6.7 安装smokeping (最完整教程)
  5. 帝国cmsV6.6版数据表
  6. CI框架源码阅读笔记5 基准测试 BenchMark.php
  7. BlueTooth: 蓝牙基础知识进阶——链路控制操作
  8. 复制代理JOB
  9. Message Queue vs. Web Services?
  10. dapper 操作类封装
  11. [codevs3296]有序数组合并
  12. Central Europe Regional Contest 2012 Problem I: The Dragon and the Knights
  13. Acrobat 转换pdf到png的另一种方法
  14. JavaScript 基础排序的实现(一)
  15. Android Studio导入项目一直卡在Building gradle project info的解决方案
  16. Hadoop集群-HDFS集群中大数据运维常用的命令总结
  17. luogu 2294 狡猾的商人 带权并查集
  18. Codeforces 739C Alyona and towers 线段树
  19. CAD常用的快捷键命令
  20. CSS3盒子模型(上)

热门文章

  1. 安装 VirtualBox 出现回滚,无法安装及解决方法
  2. [转] Protobuf高效结构化数据存储格式
  3. RESTful API设计原则与规范
  4. git(二)
  5. 通过OSG实现对模型的日照模拟
  6. 如何使用Vue.js来搭建一个后台管理系统
  7. 重定向Redirect 的知识
  8. java获取系统类型与版本
  9. 【Shell学习笔记3》实践项目自动部署脚本】shell中获取返回值、获取当前sh文件路径
  10. git push 时:报missing Change-Id in commit message footer的错误