对于Nginx的反向代理主要在四个方面:
1、URL重写 URL rewirte
2、反向代理 reverse proxy 并启用缓存功能
3、负载均衡
4、安装第三方模块,实现健康状态检测。
一、URL重写 URL rewirte
要想使Nginx有rewirte功能,要在编译Nginx时要用到–with-http_rewirte_module这个模块,这个功能在编译的时候也是默认打开,如果不想使用这个功能就用–without-http_rewirte_module
这个功能也支持URI重写。
1、rewirte
这是个模式匹配如下:
rewrite regex replacement flag
来简单的解释一下这句的意思:rewirte 是关键字,regex是匹配的模式,replacement 是把匹配到的替换成什么,flag是标志位。
例如:将htt://www.zhou123.com/attatch.php?value=111223 改写为http://www.zhou123.com/111223/attatch
就可如下所示的形式:
rewirte ^/(attatch)\.php\?value=(.*)$ /$2/$1 last;
下面来说一下标志位:
last: 当前的内容已经完成了,可以继续以后的语句(如果有多条语句时)
break: (当重写的语句执行完成后就直接跳出去支行后面的语句)
redirect: 重定向到一个完整的URL中去如:http://。用302来表示,这个是临时性的。
permanent: 永久重定向 用301表示;
下面来举个小例子:
如用户访问:www.zhou123.com/forum/ 重写为www.zhou123.com/bbs/
rwirte ^/forum/?$ /bbs/ permanent;
编辑nginx的配置文件来测试一下:
vim /etc/nginx/nginx.conf
定位到 server 这行在这个server加入如上内容即可:
location / {
root html;
rwirte ^/forum/?$ /bbs/ permanent; #####这行是新加的内容。
index index.html;
}
在浏览器中输入:http://www..zhou123.com/forum/就会把地址改成www.zhou123.com/bbs/。
rewirte比较简单这里就不测试了。
2、 关于if的应用
语法: if (condition) {…}
应用环境;server,location
条件:
1、变量名: false values are:empty string (“”,or any string starting with “0″;)
2、对于变量进行的比较表达式,可使用=或!= 进行测试 ;
3、正则表达式的模式匹配:
~ 区分大小的模式匹配
~* 不区分字母的大小写的模式匹配
!~ 和!~* 分别对上面的两种测试取反
4、测试文件是否存在-f 或!-f
5、测试目录是否存在-d或!-d
6、测试目录、文件或链接文件的存在性-e或!-e
7、检查一个文件的执行权限-x或!-x
在正则表达式中,可以使用圆括号标记匹配到的字符串,并分别可以使用$1、$2….
3、实现域名跳转:
要在配置文件中修改成如下所示的内容:
server
{
listen 80;
server_name jump.magedu.com;
index index.html index.php;
root /www/htdocs;
rewrite ^/ http://www.magedu.com/;
}
这样就可以把jump.magedu.com 改成http://www.magedu.com/这个域名了。
4、实现域名镜像
server
{
listen 80;
server_name mirror.magedu.com;
index index.html index.php;
root /www/htdocs;
rewrite ^/(.*)$ http://www.magedu.com/$1 last;
}
nginx rewrite
精选 转载tianxiamall 博主文章分类:web server
文章标签 nginx rewrite 文章分类 Nginx 服务器
上一篇:php-fpm 简介
下一篇:nginx 负载均衡

-
nginx rewrite 测试 nginx rewrite规则
nginx通过ngx_http_rewrite_module模块支持url重写、支持if条件判断,但不支持else。 该模块需要PCRE支持,应在编译nginx时指定PCRE源码目录,nginx安装方法。 nginx rewrite指令执行顺序: 1.执行server块的rewrite指令(这里的块指的是server关键字后{}包围的区域,其它xx块类似) 2.执行location匹配 3.执行
nginx rewrite 测试 linux linux nginx nginx rewite nginx rewite 规则