网站首页 > 精选教程 正文
nginx的转发模块ngx_http_rewrite_module
包含以下指令:
break
if
return
rewrite
rewrite_log
set
uninitialized_variable_warn
break
break指令用来停止当前的ngx_http_rewrite_module指令集合(current set of ngx_http_rewrite_module directives)。
比如:
if ($slow) { limit_rate 10k; break;
if
条件判断,如果满足条件,括号内的指令将被执行。条件可以为:
变量名:true or false
比较运算: = or !=
正则表达式: ~(区分大小写匹配case-sensitive matching)or ~*(不区分大小写匹配case-insensitive matching)
-f: 判断文件是否存在
-d:判断文件,文件夹,符号连接是否存在
-x: 检查文件是否可执行
例如:
if ($http_user_agent ~ MSIE) {
return
停止处理并发挥特殊的http code 给客户端。
return (301 | 302 | 303 | 307) url;
return (1xx | 2xx | 4xx | 5xx) ["text"];
rewrite
rewrite regex URL [flag];
如果请求的URI满足regex,则URI被替换。如果替换的URI是http或https开头,则停止以后处理并返回给客户端。
flag
的可选值为:
last:停止当前的ngx_http_rewrite_module指令并以替换的URI来查找新的location。
break:停止当前的ngx_http_rewrite_module指令集合。
redirect:返回302.
permanent:返回永久的转发:301 code.
注意:last
指令会重新查找新的location,而break会跳出当前的location or if direct。
因此,当使用于location内部中时,last会跳出后重新匹配。
location /download/ {
假如你访问URL:/download/a/media/a.mp3。
上面将匹配:/download/a/mp3/a.mp3
。
如果,将break更换为last:
location /download/ {
那么过程就变成了:
/download/a/media/a.mp3-->/download/a/mp3/a.mp3-->/download/a/mp3/a.mp3-->403
set
设置变量。eg:
set $is_mobile "pc"; if ( $http_user_agent ~* "(Android|iPhone|iPod|Symbian|IEMobile|BlackBerry)"){ set $is_mobile "mobile";
应用
例子来自于:nginx blog:
server {
add or remove www
# add 'www'server {
默认转发:
server {
强制HTTPS:
server {
或者:
# NOT RECOMMENDEDif ($scheme != "https") {
这种,需要额外处理,不推荐使用。
location / {
移除不支持文件:
location ~ \.(aspx|php|jsp|cgi)$ { return 410; #deny all;}
自定义路由:
rewrite ^/listings/(.*)$ /listing.html?listing=$1 last;
参考资料
https://www.nginx.com/blog/creating-nginx-rewrite-rules/
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
猜你喜欢
- 2024-10-19 nginx反向代理IIS出现response header返回Locaiton问题
- 2024-10-19 远程代码执行漏洞 远程代码执行漏洞怎么修复
- 2024-10-19 攻击和审计Docker容器04 docker 攻击
- 2024-10-19 亿级流量网站架构核心技术「笔记」(一)
- 2024-10-19 开发环境下如何进行安全加固呢 装开发环境
- 2024-10-19 架构学习之路——高可用高并发系统设计原则
- 2024-10-19 交易型系统设计的一些原则 交易系统的交易规则
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- nginx反向代理 (57)
- nginx日志 (56)
- nginx限制ip访问 (62)
- mac安装nginx (55)
- java和mysql (59)
- java中final (62)
- win10安装java (72)
- java启动参数 (64)
- java链表反转 (64)
- 字符串反转java (72)
- java逻辑运算符 (59)
- java 请求url (65)
- java信号量 (57)
- java定义枚举 (59)
- java字符串压缩 (56)
- java中的反射 (59)
- java 三维数组 (55)
- java插入排序 (68)
- java线程的状态 (62)
- java异步调用 (55)
- java中的异常处理 (62)
- java锁机制 (54)
- java静态内部类 (55)
- java怎么添加图片 (60)
- java 权限框架 (55)
本文暂时没有评论,来添加一个吧(●'◡'●)