2016-09-16 21 views
1

網址我有這樣的目錄結構,爲我的項目有條件前面加上路徑在NGINX

/var/www 
    /project-a 
    /data <-- configuration and user files/data 
    /app  <-- all the code is in sub-dirs in here 
    /pm  <-- a home brew project management app 
    /.pmdata <-- data used by pm 

我的目標是配置NGINX這樣我就可以通過 http://project-a.dev/http://project-a.dev/pm/項目管理訪問項目本身。

換句話說,我希望第二個url保持不變,但是如果url沒有指向/pm/*,應該重新編寫它以使其缺少/app作爲前綴。

我曾嘗試以下的配置,但http://project-a.dev/pm/結果在404和http://project-a.dev/第一重定向到http://project-a.dev/app/然後給出404

我在做什麼錯?

server { 
    listen 127.0.0.1:80; 
    root /var/www/project-a; 
    index index.php index.html index.htm; 
    server_name project-a.dev; 
    location/{ 
     try_files $uri $uri/app $uri/app/ =404; 
    } 
    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 
} 

回答

1

另外,您可以追加/app爲不以/pm開頭的所有的URI root值。例如:

server { 
    ... 
    root /var/www/project-a/app; 
    index index.php index.html index.htm; 
    location/{ 
     try_files $uri $uri/ =404; 
    } 
    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     include fastcgi_params; 
     fastcgi_param SCRIPT_FILENAME $request_filename; 
    } 
    location ^~ /pm { 
     root /var/www/project-a; 
     try_files $uri $uri/ =404; 

     location ~ \.php$ { 
      try_files $uri =404; 
      fastcgi_pass unix:/var/run/php5-fpm.sock; 
      include fastcgi_params; 
      fastcgi_param SCRIPT_FILENAME $request_filename; 
     } 
    } 
} 

location ~ \.php$爲嵌套位置塊執行/pm層級內PHP文件。需要使用^~修飾符來避免其他location ~ \.php$塊進行控制。詳情請見this document