V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
icanc
V2EX  ›  分享发现

根据任意网址匹配项目

  •  
  •   icanc · 2014-10-18 11:22:11 +08:00 · 1774 次点击
    这是一个创建于 3494 天前的主题,其中的信息可能已经有所发展或是发生改变。

    刚开始使用Laravel开发项目时,我会在 Nginx 新建一份 Server 配置,并在 hosts 里添加一个与项目相关的域名,并把它指向 127.0.0.1。项目多了这样非常不方便。后来使用泛域名绑定到本机,并把 Nginx 配置成:

    server {
        listen 80;
        set $domain /Users/icanc/www;        
        if ( $host ~* ([a-z0-9]+)\.domain\.com)
        {
            set $domain /Users/icanc/www/$1/public;    
        }
        root $domain;
        index index.html index.htm index.php;
        location / { 
            try_files $uri $uri/ /index.php?$args;
            }
        location ~ \.php$ {
                try_files $uri =404;
                include fastcgi.conf;
                fastcgi_pass 127.0.0.1:9000;
            }
    }
    

    例如访问 app.domain.com,就能定向到 /Users/icanc/www/app/public

    如果你没有域名,或者没有很短的域名,可以这样做(假如你安装了 Chrome 和 SwitchySharp):

    1. 在 SwitchySharp 新建一个 http 代理 localhost,IP为127.0.0.1,端口为 80
    2. 新建一个切换规则,URL匹配模式为 http://\w+.dev\/.* ,匹配模式为 正则表达式,情景模式为 localhost
    3. 修改 Nginx 里匹配域名的部份,把 ([a-z0-9]+).domain.com 改为 ([a-z0-9]+).dev
    4. SwitchySharp 切换为自动模式

    这样访问 app.dev,就能定向到 /Users/icanc/www/app/public

    3 条回复    2014-10-18 13:29:55 +08:00
    bcxx
        1
    bcxx  
       2014-10-18 11:31:09 +08:00
    改本地 host 不就好了么……
    kofj
        2
    kofj  
       2014-10-18 12:33:31 +08:00 via Android
    楼主太折腾了,你需要一个配置管理系统
    kisshere
        3
    kisshere  
       2014-10-18 13:29:55 +08:00
    你电脑里的hosts文件表示醉了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1116 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 18:44 · PVG 02:44 · LAX 11:44 · JFK 14:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.