1,问题描述
upstream datacollectbackend{
#ip_hash;
server 10.234.1.211:6100 max_fails=5 fail_timeout=30s;
server 10.234.1.26:7100 max_fails=5 fail_timeout=30s;
}
location ~* ^/OCC_DATACO_WEB/.*$ {
include deny.conf;
proxy_pass http://datacollectbackend;
include proxy.conf;
error_log logs/datacollection_error.log error;
access_log logs/datacollection_access.log main;
}
现在就是觉得OCC_DATACO_WEB名字太长了,想换成dt,但是工程还没有上线,需要先做个测试,就是让www.linuxidc/dt/possystem去调用OCC_DATACO_WEB工程里面去。
也就是说以前用www.linuxidc/OCC_DATACO_WEB/possystem访问,现在改成www.linuxidc/dt/possystem访问。但是线上工程还是OCC_DATACO_WEB,但是现在dt工程还没有做完,处于过渡期要等新的dt完善后在移除OCC_DATACO_WEB换成dt
2,用location的proxy_pass做跳转
直接将dt的跳转到和 OCC_DATACO_WEB一样的proxy_pass,如下所示
location ~* ^/dt/.*$ {
include deny.conf;
proxy_pass http://datacollectbackend;
include proxy.conf;
error_log logs/dt_error.log error;
access_log logs/dt__access.log main;
}
然后重启nginx,通过www.linuxidc/dt/possystem访问,结果报错如下:
Sorry,找不到该页面
您使用的URL可能拼写有误,该页可能已经移动,或者它可能只是临时脱机。
重新键入正确网址,或者返回首页
3,用rewrite来取代location
location ~* ^/dt/.*$ {
rewrite /dt/(.*) /OCC_DATACO_WEB/$1 break;
}
然后重启nginx,使用www.linuxidc/dt/possystem访问,已经生效了,成功了。
4,总结
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向。rewrite只能放在server{},location{},if{}中,并且只能对域名后边的除去传递的参数外的字符串起作用,例如 http://linuxidc.com/a/we/index.php?id=1&u=str 只对/a/we/index.php重写。语法rewrite regex replacement [flag];
如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用proxy_pass反向代理。
表明看rewrite和location功能有点像,都能实现跳转,主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。很多情况下rewrite也会写在location里,它们的执行顺序是:
执行server块的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
如果其中某步URI被重写,则重新循环执行1-3,直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误。
本次实践中,是需要将dt的工程访问都指向OCC_DATACO_WEB工程里面去,所以需要仅仅location加proxy_pass是不够的,需要rewrite的。
更多Nginx相关教程见以下内容:
CentOS 6.2实战部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
使用Nginx搭建WEB服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm
搭建基于Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3下Nginx性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3下配置Nginx加载ngx_pagespeed模块 http://www.linuxidc.com/Linux/2013-09/89657.htm
CentOS 6.4安装配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
Nginx安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm
Nginx日志过滤 使用ngx_log_if不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm
Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-01/126991.htm
