网站首页 > 技术教程 正文
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-09-19 Nginx-包教包会-入门(nginx详细讲解)
- 2024-09-19 Windows + Linux(虚拟机)打造PHP开发环境
- 2024-09-19 WordPress切换到Nginx服务器教程(wordpress nginx apache)
- 2024-09-19 nginx反向代理配置去除前缀(nginx 反向代理时去掉请求前缀)
- 2024-09-19 Nginx 面试通关秘籍-28.什么是 Nginx 的 rewrite 指令及它的作用?
- 2024-09-19 Nginx基础知识之rewrite正则表达式部分
- 2024-09-19 Nginx基础知识之apache和nginx rewrite规则对比
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)