Nginx中的匹配规则

nginx中强大的功能,,如rewrite、proxy_pass都离不开他的匹配规则。

1. nginx location

1.语法规则和优先级

location [=|~|~|!~|!~|^~] /uri/ { ... } = > ^~ > ~|~|!~|!~** > / 精确匹配 > 字符开头 > 正则匹配 > 通配

  • = 表示精确匹配
  • ^~ 表示uri以某个常规字符串开头,大多情况下用来匹配url路径,nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格,即所见即所得)。
  • ~ 正则匹配(区分大小写)
  • ~* 正则匹配(不区分大小写)
  • !~ 和 !~* 分别为区分大小写不匹配及不区分大小写不匹配 的正则
  • / 任何请求都会匹配
server {
listen 192.168.93.136;
root /abcd;
index  index.html;
location = / { index a.html; }
location ~ / { index b.html; }
location   / { index c.html; }
}
#测试页面 根据页面的不同内容来观察优先级

2.匹配顺序

除了优先级以外,还有一种少见的根据优先匹配顺序来决定使用哪个匹配,举个例子

图示:根据不同的匹配转发到不同的tomcat应用,都是用的~正则匹配

此时有一个url,如:http://ip:port/gwaf/report,可以看到,这个url同时满足两个匹配,那么该请求是转发到server2,还是server3呢。

答案是server2。同级别匹配规则,走先匹配到的匹配规则,从配置文件角度来看,就是从上到下的顺序,这里也就是匹配到了 ~ /(console|report),所以转发到server2。

那么就会出现一个问题,如若这个url是请求的server3里面的接口,应该转发到server3处理的,但却转发到server2,这时nginx会返回404,因为server2处理不了这个接口。这里有两种方法:可以尝试将~ /gwaf匹配写到~ /(console|report) 匹配的前面,让~ /gwaf 优先匹配,转发到server3。或者使用^~ /gwaf ,使用开头匹配增加匹配优先级。

3. nginx rewrite

rewrite 将用户访问的url 重定向到一个新的url

下面通过几个rewrite匹配案例来看一下

案例1:rewrite url实现跳转

当访问http://192.168.9.13/aaa/a/1.html 时,重定向至http://192.168.9.13/ccc/bbb/1.html

mkdir -p /usr/share/nginx/html/ccc/bbb
mkdir -p /usr/share/nginx/html/aaa/a
vim /usr/share/nginx/html/aaa/a/1.html
vim /usr/share/nginx/html/ccc/bbb/1.html
vim /etc/nginx/conf.d/default.conf
location /aaa {
rewrite .* /ccc/bbb/1.html permanent;   # url含有/aaa就会被重定向到 /ccc/bbb/1.html

}

测试页面 http://192.168.9.13/aaa/a/1.html 访问的url中含有"/aaa"字段就可以

permanent参数:

如果用了permanent 会直接显示新的url 新的网页内容,如果不使用 还是显示原来的url但是页面内容还是会显示转发后的。

以下匹配方法是否可行:

location = /aaa         http://192.168.9.13/aaa/123.html

不可行。因为需要完全匹配

location ~ /aaa         http://192.168.9.13/aaa/123.html

可行。因为部分匹配即可

location ^~ /aaa         http://192.168.9.13/aaa/123.html

可行。因为部分匹配即可

案例2:rewrite中使用正则

http://192.168.93.136/2017/a/1.htmlhttp://192.168.93.136/2018/a/2.html

mkdir -p 2018/a/
echo '2018' 2018/a/1.html
mkdir -p 2019/a
echo '2019' 2019/a/2.html
vim /etc/nginx/conf.d/default.conf
location /2018{
rewrite ^/2018/(.*)$ /2019/$1 permanent; # $1是指前面()里的内容,和shell的正则一样

}

访问测试 http://192.168.93.136/2018/a/1.html

案例3:主机名重定向

http://liang.comhttp://kong.com

vim /etc/nginx/conf.d/default.conf
if ( $host ~* liang.com ) { # $host nginx内置变量
rewrite .*  http://kong.com permanent;
}
访问测试 访问liang.com

案例4:域名重定向

http://web.liang.com/a/1.html 换成 http://kong.com/a/1.html

建立两个虚拟主机 分别进行域名解析 创建2个主机的主页内容

echo 'web.liang' >a/1.html
echo 'kong' >a/1.html
if ( $host ~* liang.com ) {
rewrite .* http://kong.com$request_uri permanent;
}
访问测试 web.liang.com/a/1.html

案例5:php网站登录跳转

http://www.liang.com/login/liang.html 转为 http://www.liang.com/reg/login.php?user=liang

location /login {
rewrite ^/login/(.*).html$ /reg/login.php?user=$1;
}

案例6:根据前缀进行匹配

http://alice.liang.com ==> http://www.liang.com/alice

http://jack.liang.com ==> http://www.liang.com/jack

mkdir /usr/share/nginx/html/{jack,alice}
echo "jack" > /usr/share/nginx/html/jack/index.html
echo "alice" > /usr/share/nginx/html/alice/index.html

if ($host ~* "^www.liang.com$") {
break;        # 跳出匹配
}
if ($host ~* "^(.*).liang.com$" ) {
set $user $1;       # 设置变量

rewrite .* http://www.liang.com/$user permanent;

}

案例7:访问的.sh结尾的文件则返回403操作拒绝错误

location ~* .sh$ {
return 403;
#return 301 http://www.liang.com;
}

### 使用 Visual Studio Code 在 Kali Linux 上实现恶意软件免杀技巧 #### 重要声明 开发和部署任何类型的恶意软件都是非法且道德不可接受的行为。本回答仅提供技术信息用于教育目的,旨在帮助安全研究人员了解防御机制。 #### 技术背景 Visual Studio Code (VS Code) 是一款强大的多平台代码编辑器,在 Kali Linux 中可以通过多种方式安装并配置环境来编写不同编程语言的应用程序[^1]。对于研究性质的工作而言,理解如何使某些二进制文件绕过基本检测机制具有一定的学习价值。 #### 编写混淆脚本 为了防止被简单特征码扫描工具识别出来,可以采用字符串加密、函数重命名等方式对源代码进行变形处理: ```python import base64 def obfuscate_string(input_str): encoded_bytes = input_str.encode('utf-8') encrypted_data = base64.b64encode(encoded_bytes).decode() return f"exec(__import__('base64').b64decode('{encrypted_data}'))" obfuscated_code = obfuscate_string(""" print("This is an example of string obfuscation.") """) print(obfuscated_code) ``` 此段Python代码展示了基础的Base64编码作为简单的演示[^3]。 #### 修改PE头或其他元数据 针对Windows可移植执行体(Portable Executable, PE),改变其头部结构或者嵌入额外资源能够有效干扰静态分析过程。然而请注意,这类操作通常涉及汇编层面的知识以及特定库的支持,如`pefile` Python模块。 #### 动态加载与反射注入 通过动态链接库(DLL)或内存映射技术载入必要的功能组件而非直接包含于主程序体内;这种方法增加了逆向工程难度的同时也使得传统的基于签名的安全产品难以捕捉到完整的攻击模式[^5]。 #### 创建自定义打包方案 利用PyInstaller等工具将解释型语言转换成独立运行的原生应用,并加入加壳保护措施进一步增强隐蔽效果。不过需要注意的是,过度复杂的封装可能会引起高级防护系统的怀疑。 #### 配置 VS Code 支持上述工作流程 确保已按照官方指南完成VS Code及其扩展插件的设置,以便支持目标编程语言特性及调试需求[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值