[记录]Nginx配置实现&&和||的方法实例
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配置实现&&和||的方法实例的更多相关文章
- Nginx配置代理gRPC的方法
Nginx配置代理gRPC的方法_nginx_脚本之家 https://www.jb51.net/article/137330.htm
- Nginx记录-Nginx配置
1. 启动,停止和重新加载Nginx配置 要启动nginx,请运行可执行文件. 当nginx启动后,可以通过使用-s参数调用可执行文件来控制它. 使用以下语法: nginx -s signal 信号( ...
- PHP+FastCGI+Nginx配置PHP运行环境方法
PHP+FastCGI+Nginx配置PHP运行环境 Nginx不支持对外部程序的调用,所以必须通过FastCGI接口实现对外部程序的调用从而实现对客户端动态页面请求的处理. CGI的英文全称为Com ...
- [记录]NGINX配置HTTPS性能优化方案一则
NGINX配置HTTPS性能优化方案一则: 1)HSTS的合理使用 2)会话恢复的合理使用 3)Ocsp stapling的合理使用 4)TLS协议的合理配置 5)False Start的合理使用 6 ...
- nginx配置ssl证书的方法
Nginx (读音"engine x") 是一个高性能的HTTP和反向代理服务器,比Apache占用更少的内存,同时也像Apache一样支持HTTPS方式访问(SSL加密).本教程 ...
- Apache和Nginx配置支持苹果ATS方法
什么是ATS功能? ATS是iOS9和OS X El Capitan的一个新特性.开启该功能后,ATS对使用NSURLConnection, CFURL或NSURLSession 等APIs 进行的网 ...
- nginx配置多域名映射方法(本地hosts)
本地测试网站的时候如果不想用localhost/xxxx的形式访问,可能就需要修改hosts文件来映射了,但是一个网站还好,假如有多个网站的话就不行了. 这时就需要配置多域名映射 比如hosts中配置 ...
- nginx配置二级域名
我在我的服务器上面跑了两个node应用程序,分别一个端口2368跑的是ghost博客,一个端口8000跑的是我的demo程序.想要一级域名zhangruojun.com用来访问博客,二级域名demo. ...
- Nginx配置日志格式记录cookie
Nginx配置日志格式记录cookie1. 一般用来做UV统计,或者获取用户token等. 配置方式: 在nginx的配置文件中有个变量:$http_cookie来获取cookie的信息.配置方式很 ...
随机推荐
- 请求通道在等待 00:00:58.9616639 以后答复时超时。增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值。分配给此操作的时间可能是更长超时的一部分。
异常信息:请求通道在等待 00:00:58.9616639 以后答复时超时.增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值.分配给此操作的时间可能是更长超时的一部分. 开发背 ...
- SQL Server 2008收缩日志文件--dbcc shrinkfile参数说明
原文:SQL Server 2008收缩日志文件--dbcc shrinkfile参数说明 DBCC SHRINKFILE 收缩相关数据库的指定数据文件或日志文件大小. 语法 DBCC SHRINKF ...
- 零元学Expression Blend 4 - Chapter 11 用实例了解布局容器系列-「Border」
原文:零元学Expression Blend 4 - Chapter 11 用实例了解布局容器系列-「Border」 将教大家以实做案例认识Blend 4 的布局容器,此章介绍的布局容器是Blend ...
- Oracle 存储过程创建及调用
--------创建存储过程------- create or replace procedure TestSPas begin update table_name set CREATE_TIMEST ...
- DataTable,DataView 排序和使用
我们都知道在Sql Server可以用order by来排序,所以很多朋友在DataTable中排序也想到了用order by关键字.但这样实现是比较困难的,下面,我们讲解一种比较简单的方法: 控制台 ...
- Caliburn.Micro 自定义View和ViewModel的匹配规则
使用TypeMappingConfiguration类 //Override the default subnamespaces var config = new TypeMappingConfigu ...
- Tensorflow进阶
第一章 图像领域,第\(i\)类图片提取到的特征: \[ feature_i=\sum_jw_{i,j}x_j+b_i \] 其中,\(j\)表示一张图片的第\(j\)个像素,\(b_i\)是偏置值( ...
- 为javasript中对象与json串之间转换的总结。
jQuery.json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象.数值.字符串和数组到 JSON 字符串,同时可转换 JSON ...
- nodejs redis遇到的一个问题解决
v ar redis = require("redis"), client = redis.createClient({host:'tc-arch-osp33.tc', port: ...
- MotionManager 陀螺仪实现方式
题外话: 权利的游戏开播了,引用一句话: 布兰:一个人如果还把,他还能勇敢么? 奈德:人在害怕时候的勇敢,才是真的勇敢. 回归正题: 说下关于 CMMotionManage ...