icanc

根据任意网址匹配项目

  •  
  •   icanc · Oct 18, 2014 · 2224 views
    This topic created in 4313 days ago, the information mentioned may be changed or developed.

    刚开始使用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 replies    2014-10-18 13:29:55 +08:00
    bcxx
        1
    bcxx  
       Oct 18, 2014
    改本地 host 不就好了么……
    kofj
        2
    kofj  
       Oct 18, 2014 via Android
    楼主太折腾了,你需要一个配置管理系统
    kisshere
        3
    kisshere  
       Oct 18, 2014
    你电脑里的hosts文件表示醉了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2874 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:35 · PVG 21:35 · LAX 06:35 · JFK 09:35
    ♥ Do have faith in what you're doing.