2014-11-21 48 views
3

我有一個在Nginx的運行與工作服務塊,像這樣的應用程序:Nginx的別名指令不使用PHP工作

server { 

    listen 80; 
    server_name example.com; 
    root /home/deployer/apps/my_app/current/; 
    index index.php; 

    location/{ 
    index index.php; 
    try_files $uri $uri/; 
    } 

    location ~ \.php$ { 
    try_files $uri =404; 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
    } 

    location /foo { 
    root /home/deployer/apps/modules/; 
    # tried this: 
    # alias /home/deployer/apps/modules/foo/; 
    # but php is not working with alias, only with root 
    } 

} 

當我訪問/ foo的,Nginx的看在路徑爲/ home /部署/ apps/modules/foo /用於index.php文件,它可以工作。

問題:

我成立了一個部署腳本使用Capistrano的是部署到foo目錄:

/home/deployer/apps/modules/foo/ 

Capistrano的「富」目錄中創建一個「當前」目錄包含應用程序文件從Github中提取,因此我需要將根路徑更改爲:

/home/deployer/apps/modules/foo/current/ 

但是Nginx追加位置指令根指令結束....所以,當你訪問/ foo的,Nginx的嘗​​試中尋找:

/home/deployer/apps/modules/foo/current/foo/ 

使用別名應該忽略/富中的位置指令設置並從確切的別名路徑(日誌確認正在發生)提供文件,但是當我使用別名指令時,php配置沒有被正確應用,並且我得到了404返回。

如果我回到根指令並刪除「當前」目錄,它工作正常。我需要從'當前'目錄提供的文件與Capistrano部署平穩協作,但無法弄清楚如何使用別名指令來處理php。

任何人有任何想法或建議,我錯過了什麼?使用替代

回答

1

location /foo/ { 
    alias /home/deployer/apps/modules/foo/current/; 
} 

location /foo { 
    alias /home/deployer/apps/modules/foo/; 
} 
+0

也許我沒有提到我曾經嘗試過......這並不是說我無法讓Nginx查看正確的目錄,即使它發生了,php5-fpm也沒有處理PHP文件使用別名 - 它使用根(使用調試級錯誤日誌記錄來驗證正在服務的路徑,並驗證該路徑包含有效的index.php)。 – johndavid400 2014-11-21 19:21:39

+3

@ johndavid400你能澄清一下:你是否期望location/foo實際提供php文件?它將由php位置進行處理,因爲'index'指令會發出一個**內部重定向**,所以當使用別名指令找到文件時,它現在也必須在php位置中找到。我敢打賭這是你的實際問題。您將無法使用'alias'指令和'try_files'指令,因此您需要重寫請求URI。嘗試'重寫^/foo /(.*)$/$ 1 break;'而不是。在兩個位置調整路徑。 – 2014-11-21 19:47:50

+1

感謝您的建議,只是刪除了PHP位置塊中的try_files選項使其工作。 – johndavid400 2014-11-21 20:57:36

3

感謝@澤維爾 - 盧卡斯有關無法使用try_files與別名的建議。

用PHP使用別名,我不得不從原來的問題所示的PHP位置塊取出try_files指令:

try_files $uri =404; 

其實我不得不重申了/ foo的位置在PHP位置塊刪除上面的行。它看起來像這樣:

location /foo { 
    alias /home/deployer/apps/modules/foo/; 
    location ~ \.php$ { 
    # try_files $uri =404; -- removed this line 
    fastcgi_split_path_info ^(.+\.php)(/.+)$; 
    fastcgi_pass unix:/home/deployer/apps/shared/php5-fpm.sock; 
    fastcgi_index index.php; 
    include fastcgi_params; 
    } 
} 

這允許php文件直接從alias指令中列出的目錄進行處理。

+0

你先生是個天才。我只浪費了一天的時間嘗試各種形式的配置。 – icc97 2016-05-18 08:07:19