nginx配置基于目录的配置及地址重写

7、需求:公司要求系统重构,需要重新配置系统框架,架构如下 域名
www.lxp.com

目录结构:发布目录是 /alidata/txall 这个目录,这个目录里边有callback usercenter ordercenter marketcenter 每个目录里边有其他的东西和一个web目录,web目录里边有index.php 或index.html,当访问 www.lxp.com/callback 时直接访问的是 callback下边的web目录,其他目录都一样的要求

这是现在的目录架构,需求是,当访问dev.shirbility.cn/callback 时 要定义到 callback/web这个目录里边(有index.htnl或index.php) 当要访问 callback 里边的其他目录时也可以直接访问。像其他的usercenter ordercenter marketcenter 也是同样的需求,配置如下


server {
    listen 80;
    server_name  www.lxp.com;
    root  /alidata/txall;
    index index.html index.php;

     location   = /callback  {
     root  /alidata/txall/callback;
     index   index.html index.php index.htm;
     rewrite  "^(.*)/callback$"   /callback/web/ last;
     }

     location  = /usercenter {
     root  /alidata/txall/usercenter;
     index  index.html index.php index.htm;
     rewrite  "^(.*)/usercenter$"  /usercenter/web/ last;
     }

     location  = /ordercenter {
     root  /alidata/txall/ordercenter;
     rewrite  "^(.*)/ordercenter$"  /ordercenter/web/ last;
     index   index.html  index.php index.htm;
      }

     location  = /marketcenter {
    root  /alidata/txall/marketcenter;
    rewrite  "^(.*)/marketcenter$"  /marketcenter/web last;
    index    index.html index.php index.htm;
      }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

```
     location   = /callback  {
     root  /alidata/txall/callback;
     index   index.html index.php index.htm;
     rewrite  "^(.*)/callback$"   /callback/web/ last;
     }
```

我是直接重定向到 /callback/web/  里边。当web后边没有/时 如(/callback/web),你在访问www.lxp.com/callback  的时候浏览器的url地址就会跳转  成 www.lxp.com/callback/web/这样,但是要求是能显示跳转,所以就加 /,这样同时能够访问到callback目录里边与web同级的其他目录或文件。

问题:当你访问www.lxp.com/callback 时可以但访问到页面,但是你访问 www.lxp.com/callback/  时就不能访问到

原因 :因为www.lxp.com/callback  和 www.lxp.com/callback/ 是不一样的
两个访问的目录是不一样的。
```
        location = /aaa {
        root /usr/share/nginx/html/aaa;
        rewrite  "^(.*)/aaa$"  /aaa/ last;
        }
```

这样配置的话,你访问 www.123.com/aaa   和 www.123.com/aaa/ 是一样的,因为他的root 是/aaa  他的rewrite之后的也是/aaa   所以没影响

但是我们的配置是
     location   = /callback  {
     root  /alidata/txall/callback;
     index   index.html index.php index.htm;
     rewrite  "^(.*)/callback$"   /callback/web/ last;
     }

root 和 rewrite后边的东西不一样,www.lxp.com/callback 放问的是重定向后的内容,但是www.lxp.com/callback/  访问的是root (/alidata/txall/callback) 是这个。所以当你后边加 / 的时候就会报404错误。加入callback目录下有index.html  或者 index.php 的时候就会直接访问到此页面。  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章