nginx的http模块
html的页面
1 2 3 4 5 | [root@python html] # echo first > first/1.txt [root@python html] # echo second > second/1.txt [root@python html] # echo third > second/1.txt [root@python html] # echo second > second/1.txt [root@python html] # echo third > third/1.txt |
url地址重写
rewrite模块:rewrite指令
sysntax : rewrite regex replacement [flag];
Default: -
Context: server,location,if
功能:
将regex指定的URL替换成replacement这个新的url可以使用正则表达式
当replacement以http://或者https://或$schema开头,则直接返回302重定向
替换后的url根据flag指定的方式进行处理
last:用replacement这个url进行新的location匹配
break:break指令是停止当前脚本指令的执行,等价于独立的break指令
redirect:返回302重定向(临时重定向)
permanent:返回301重定向(永久重定向)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | server { server_name haha.com; listen 8080 ; error_page 404 / 403.html ; #return 405; location / { #return 404 "find nothing!\n"; } root html / ; location / first { rewrite / first(. * ) / second$ 1 last; return 200 1first !\n'; } location / second { #rewrite /second(.*) /third$1 break; rewrite / second(. * ) / third$ 1 ; return 200 'second!\n' ; } location / third { return 200 'third!\n' ; } } |
测试
1 2 | [root@python vhast] # curl haha.com:8080/first/1.txt second! |
修改配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | [root@python vhast] # cat test.conf server { server_name haha.com; listen 8080 ; error_page 404 / 403.html ; #return 405; location / { #return 404 "find nothing!\n"; } root html / ; #到这里找请求的资源 location / first { rewrite / first(. * ) / second$ 1 last; return 200 1first !\n'; } location / second { rewrite / second(. * ) / third$ 1 break ; 表示跳出指令块后 #rewrite /second(.*) /third$1; return 200 'second!\n' ; } location / third { return 200 'third!\n' ; } } |
测试
1 2 | [root@python vhast] # curl haha.com:8080/first/1.txt third |
修改配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | server { server_name haha.com; listen 8080 ; error_page 404 / 403.html ; #return 405; location / { #return 404 "find nothing!\n"; } root html / ; location / first { rewrite / first(. * ) / second$ 1 last; return 200 1first !\n'; } location / second { #rewrite /second(.*) /third$1 break; rewrite / second(. * ) / third$ 1 last; # 用替换后的继续匹配 return 200 'second!\n' ; } location / third { return 200 'third!\n' ; # 匹配到这个资源 } } |
测试
1 2 | [root@python vhast] # curl haha.com:8080/first/1.txt third! |
修改配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | server { server_name haha.com; #listen 8080; error_page 404 / 403.html ; #return 405; location / { #return 404 "find nothing!\n"; } root html / ; location / first { rewrite / first(. * ) / second$ 1 last; return 200 1first !\n'; } location / second { #rewrite /second(.*) /third$1 break; rewrite / second(. * ) / third$ 1 last; return 200 'second!\n' ; } location / third { return 200 'third!\n' ; } location / redirect1 { rewrite / redirect1(. * ) $ 1 permanent; #301 } location / redirect2 { rewrite / redirect2(. * ) $ 1 redirect; #302 } location / redirect3 { rewrite / redirect3(. * ) http: / / haha.com:$ 1 ; #302;继承上一个 } location / redirect4 { rewrite / redirect4(. * ) http: / / haha.com:$ 1 permanent; #301 } } |
测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [root@python vhast] # curl haha.com/redirect1 <html> <head><title> 301 Moved Permanently< / title>< / head> <body> <center><h1> 301 Moved Permanently< / h1>< / center> <hr><center>nginx / 1.15 . 9 < / center> < / body> < / html> [root@python vhast] # curl haha.com/redirect2 <html> <head><title> 302 Found< / title>< / head> <body> <center><h1> 302 Found< / h1>< / center> <hr><center>nginx / 1.15 . 9 < / center> < / body> < / html> [root@python vhast] # curl haha.com/redirect3 <html> <head><title> 302 Found< / title>< / head> <body> <center><h1> 302 Found< / h1>< / center> <hr><center>nginx / 1.15 . 9 < / center> < / body> < / html> [root@python vhast] # curl haha.com/redirect4 <html> <head><title> 301 Moved Permanently< / title>< / head> <body> <center><h1> 301 Moved Permanently< / h1>< / center> <hr><center>nginx / 1.15 . 9 < / center> < / body> < / html> |
草都可以从石头缝隙中长出来更可况你呢
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏