2017-07-28 16 views
2

我們有一個自定義PHP應用程序,我們使用.htaccess文件編寫並運行在Apache上,以處理url重寫。我們正試圖將其轉換爲使用Plesk Onyx下的FPM在NGINX下工作。如何讓NGINX在自定義PHP應用程序上正確地重寫和執行?

應用程序生成類似鏈接:

https://somedomain.com/mypage (same as index/mypage) 
https://somedomain.com/index/sitemap 
https://somedomain.com/blog/some-article-name 

這些網址的映射到採取REQUEST_URI,並用它來渲染頁面響應的index.php文件。

的應用程序的結構被嵌套如下:

docroot (/) 

./index.php //handler for the request in/

./blog/index.php //handler for any request to /blog 

每個的index.php期望接收的路徑= {REQUEST_URI},以便它可以將請求映射到控制器和動作?。

我已經嘗試了多種方式讓NGINX使用tryfiles來重寫,但沒有運氣。使用重寫我可以/可以工作,但它不會渲染/ mypage或/ index/sitemap。

如果我嘗試點擊/ index/sitemap它下載index.php而不是執行它,如果我嘗試博客發生同樣的事情。實際上唯一可行的路徑是/,所有其他路徑都只是下載index.php文件。

這是我的配置,因爲它是現在,我哪裏出錯了?

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ { 
    expires 30d; 
    add_header Pragma public; 
    add_header Cache-Control 「public」; 
    try_files $uri @fallback; 
} 

location/{ 
    #index index.php index.html index.html; 
    rewrite ^/([^?]*) /index.php?path=$1 break; 
    rewrite ^blog/([^?]*) /blog/index.php?path=$1 break; 
    #try_files $uri @fallback; 

}  

回答

1

您的配置有多個問題。我會忽略第一個location區塊,因爲它似乎與您的問題無關。

第一個rewrite將永遠匹配,所以第二個rewrite將永遠不會被諮詢。第二個rewrite將永遠不會匹配,因爲nginx URI總是以/開頭。 [^?]是沒有意義的,因爲rewrite使用規範化的URI,它不包含?或查詢字符串。使用rewrite...break意味着重寫的URI在相同的位置處理,這是一個錯誤,因爲這個位置沒有配備處理PHP文件。有關更多信息,請參閱this document

使用try_files可能看起來像這樣的解決方案:

location/{ 
    try_files $uri $uri/ /index.php?path=$uri&$args; 
} 
location /blog { 
    try_files $uri $uri/ /blog/index.php?path=$uri&$args; 
} 
location ~ \.php$ { ... } 

更多見this document

相關問題