Chinaunix首页 | 论坛 | 博客
  • 博客访问: 630474
  • 博文数量: 95
  • 博客积分: 2091
  • 博客等级: 大尉
  • 技术积分: 982
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-05 19:34
文章分类

全部博文(95)

文章存档

2017年(1)

2016年(20)

2015年(10)

2014年(6)

2013年(4)

2012年(1)

2011年(9)

2010年(6)

2009年(20)

2008年(17)

2007年(1)

我的朋友

分类: LINUX

2011-06-30 17:23:04

nginx中的alias和alias目录中的rewrite

一  遇到一个跨目录的用法,原来用apache用alias很容易实现,NGINX也可以实现
 
location / { 
root  /XXX/A/;
index index.do index.jsp index.html index.htm;
proxy_pass http://XXX.XXX.XXX.XXX:8080;
}

location /other/ {
                alias /XXXX/B/;
                index  index.html index.jsp;
                proxy_pass

nginx中的alias等于再定义一个location, 注意 other/ 后面的"/"千万不要省掉。


二 alias建好,但后面做伪静态遇到点小问题,就是确认过rewrite规则无误,就是转发不成功.
例如:
location / { 
root  /XXX/A/;
index index.do index.jsp index.html index.htm;
proxy_pass http://XXX.XXX.XXX.XXX:8080;
}

location /other/ {
                alias /XXXX/B/;
                index  index.html index.jsp;
rewrite ^/([0-9]+)$ /other/index.jsp?mapid=$1 last; rewrite ^/([0-9]+)/$ /other/index.jsp?mapid=$1 last;
                proxy_pass

为什么不成功呢? 打开日志检查发现访问 实际读取的是25,肯定是404了,我们需要让它取得的是 other/index.jsp?mapid=25, 原因在于我们的rewrite位置放置的不正确,应该放在 
location / 里面,放在下面如果访问的是   才会有用,所以正确的应该为:

location / { 
root  /XXX/A/;
index index.do index.jsp index.html index.htm;
rewrite ^/([0-9]+)$ /other/index.jsp?mapid=$1 last; rewrite ^/([0-9]+)/$ /other/index.jsp?mapid=$1 last;
proxy_pass http://XXX.XXX.XXX.XXX:8080;
}

location /other/ {
                alias /XXXX/B/;
                index  index.html index.jsp;
                proxy_pass


阅读(3005) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~