2016-03-14 38 views
1

我有一個LEMP服務器,我正在尋找安裝RespondCMS(http://respondcms.com/documentation/install-on-digital-ocean)。我遇到了一些API的mod_rewrite部分的一些困難。我已經嘗試了幾次迭代,但一直無法讓它顯示指示PHP應用程序正在工作的「API工作」消息。我得到了404。到目前爲止,我已經在/ etc/nginx/sites-enabled /中設置了以下設置。我在想這不是正確訪問/ api文件夾。理解它如何互動以及如何使其工作的任何幫助,都會受到讚賞。NGINX服務器指令到達API

app.domain.com 
server { 
    listen 80; 
    server_name app.domain.com; 

    root /srv/www/domain_app/app; 
    index index.php index.html index.htm; 

    access_log /var/log/nginx/respond.access.log; 
    error_log /var/log/nginx/respond.error.log; 


    location /api/ { 
      try_files $uri $uri/ /api/dispatch.php$args; 
    } 

    location/{ 
      try_files $uri $uri/ =404; 

    } 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
      root /srv/www; 
    } 

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

} 





domain.com 
server { 
    listen 80 default_server; 

    root /srv/www/html; 
    index index.php index.html index.htm; 

    server_name domain.com; 

    location/{ 
      try_files $uri $uri/ =404; 
    } 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
      root /usr/share/nginx/html; 
    } 

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

} 
+0

「dispatch.php」的路徑名是什麼? –

+0

這是/srv/www/domain_app/app/api/dispatch.php – daki

回答

0

的URL http://app.domain.com/api/dispatch.phplocation ~ \.(hh|php)$塊處理。並且該塊似乎被正確配置(合理)。

您應該移動include fastcgi_params;高於任何fastcgi_param指令,以避免後者被無意中覆蓋。在這種情況下,fastcgi_index指令是多餘的。

location /location /api/塊都有問題。 alias指令是不必要的,也是錯誤的。所有三個位置從外部server塊繼承它們的root。您應該刪除兩個alias指令。

在您的try_files $uri $uri/ $uri.php /api/dispatch.php$args;聲明中,$uri.php元素將導致PHP文件被下載而不是執行。只有try_files指令的最後一個元素可以執行在同一位置塊內未處理的操作。如果您確實需要執行無擴展名的PHP URI並且有一個默認的PHP URI,那麼您可能需要使用一個命名的位置。

所有nginx指令的有用資源是here

+0

感謝Richard的幫助。我用你的建議更新了我的配置,但仍然沒有骰子。我刪除了$ uri.php(儘管我希望擴展名更少的URI),以查看它是否有所作爲。檢查我的錯誤日誌我得到:* 1在stderr中發送FastCGI:'「PHP消息:PHP解析錯誤:語法錯誤,意外的'DEFAULT_LANGUAGE'(T_STRING)'在/srv/www/domain_app/app/setup.php在線21" 。 – daki

+0

我修復了我的PHP錯誤。當我重新安裝LEMP設置時,我遇到了一個小錯誤。 – daki

+0

現在有效:D!這是由於PHP錯誤,而不是NGINX。如此欣喜若狂,非常感謝你的一切。 – daki