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

[记录]Nginx配置实现&&和||的方法实例的更多相关文章

  1. Nginx配置代理gRPC的方法

    Nginx配置代理gRPC的方法_nginx_脚本之家 https://www.jb51.net/article/137330.htm

  2. Nginx记录-Nginx配置

    1. 启动,停止和重新加载Nginx配置 要启动nginx,请运行可执行文件. 当nginx启动后,可以通过使用-s参数调用可执行文件来控制它. 使用以下语法: nginx -s signal 信号( ...

  3. PHP+FastCGI+Nginx配置PHP运行环境方法

    PHP+FastCGI+Nginx配置PHP运行环境 Nginx不支持对外部程序的调用,所以必须通过FastCGI接口实现对外部程序的调用从而实现对客户端动态页面请求的处理. CGI的英文全称为Com ...

  4. [记录]NGINX配置HTTPS性能优化方案一则

    NGINX配置HTTPS性能优化方案一则: 1)HSTS的合理使用 2)会话恢复的合理使用 3)Ocsp stapling的合理使用 4)TLS协议的合理配置 5)False Start的合理使用 6 ...

  5. nginx配置ssl证书的方法

    Nginx (读音"engine x") 是一个高性能的HTTP和反向代理服务器,比Apache占用更少的内存,同时也像Apache一样支持HTTPS方式访问(SSL加密).本教程 ...

  6. Apache和Nginx配置支持苹果ATS方法

    什么是ATS功能? ATS是iOS9和OS X El Capitan的一个新特性.开启该功能后,ATS对使用NSURLConnection, CFURL或NSURLSession 等APIs 进行的网 ...

  7. nginx配置多域名映射方法(本地hosts)

    本地测试网站的时候如果不想用localhost/xxxx的形式访问,可能就需要修改hosts文件来映射了,但是一个网站还好,假如有多个网站的话就不行了. 这时就需要配置多域名映射 比如hosts中配置 ...

  8. nginx配置二级域名

    我在我的服务器上面跑了两个node应用程序,分别一个端口2368跑的是ghost博客,一个端口8000跑的是我的demo程序.想要一级域名zhangruojun.com用来访问博客,二级域名demo. ...

  9. Nginx配置日志格式记录cookie

    Nginx配置日志格式记录cookie1. 一般用来做UV统计,或者获取用户token等. 配置方式:  在nginx的配置文件中有个变量:$http_cookie来获取cookie的信息.配置方式很 ...

随机推荐

  1. ORACLE RAC+OGG配置

    实验环境主机名   IP地址rac01     192.168.56.10rac02     192.168.56.20rac-scan 192.168.56.30 目标库:ora-ogg  192. ...

  2. DOTNET CORE DATETIME在LINUX与WINDOWS时间不一致

    .net core项目,部署到CentOS上的时候,发现DateTime.Now获取的时间与Windows不一致,主要是时区不一致. static void Main(string[] args) { ...

  3. 毕设(三)NotifyIcon

    NotifyIcon是一个比较特殊的组件,其特殊之处是既可以把它归类到控件中,也可以把它归类到组件中.这是因为将其拖放到设计窗体后,我们并不能马上看到它的界面(像组件),而是在运行时才能看到它(像控件 ...

  4. Linux运维工程师成长路线及应实现的目标

    作为一名运维工程师,需要学习的东西非常多,在学习的过程中也没有任何捷径可言,必须一步一个脚印地学习.积累才能把个人技能提升到相应的高度.根据目前流行的发行版及国际流行的Linux认证,红帽认证和LPI ...

  5. vs2008在win7系统中安装不问题

    据说是office软件冲突问题. 解决方案是卸载了office软件,不管是2007还是其它版本,先安装vs2008,再安装其它的.

  6. Qt之界面数据存储与获取(userData)

    http://blog.csdn.net/u011012932/article/details/52413012#comments

  7. Realm_King 之 .NET操作XML完整类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;u ...

  8. CMD 从文件中截取匹配规则字符串并输出到文件

    *******************command**********************git diff 8d71d92b2d957fd1b697b4cf785fb984f190e5d2 or ...

  9. Java的String类字符串的拆分

    在java编程中,有时候我们需要把一个字符串按照某个特定字符.字母等作为截点分割这个字符串, 这样我们就可以使用这个字符串的一部分或者把所有截取的内容保存到数组里等操作. public class S ...

  10. CDMA子钟

    SYN6103型 CDMA子钟 产品概述 SYN6103型CDMA子钟是由西安同步电子科技有限公司精心设计.自行研发生产的一套从CDMA网络获取标准时间信号信息的子钟,能方便部署在任何有CDMA信号的 ...