Nginx

Nginx + php5-fpm + Symfony2 =“找不到文件。”

  • October 3, 2014

這是基於官方 Symfony2 文件的站點配置

server {
   listen 80;

   server_name     project.local;
   root            /media/Storage/project/web;

   location / {
       # try to serve file directly, fallback to app.php
       try_files $uri /app.php$is_args$args;
   }

   location ~ ^/(app|app_dev|config)\.php(/|$) {
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param HTTPS off;
   }

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

server {
   listen 443;

   server_name     project.local;
   root            /media/Storage/project/web;

   ssl on;
   ssl_certificate     /etc/nginx/ssl/localhost.crt;
   ssl_certificate_key /etc/nginx/ssl/localhost.key;

   location / {
       # try to serve file directly, fallback to app.php
       try_files $uri /app.php$is_args$args;
   }

   location ~ ^/(app|app_dev|config)\.php(/|$) {
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       fastcgi_split_path_info ^(.+\.php)(/.*)$;
       include fastcgi_params;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param HTTPS on;
   }

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

起初,我遇到了No input file specified.錯誤,但是我在nginx wiki上讀到,對於這個配置,我不需要cgi.fix_pathinfo=0(實際上,他們說 Symfony2 需要它cgi.fix_pathinfo=1)。

所以我把它改回了預設(1),現在我得到了File not found.

我已經看到了他們建議使用的其他問題location ~ \.php$ {,但它們不適用於 Symfony2 應用程序。

如果可能的話,我想堅持使用 Symfony2 官方配置。

為什麼會發生這種情況,解決方案是什麼?

編輯1:

我已將cgi.fix_pathinfo背面0更改為並將配置更改為:

server {
   listen 80;

   server_name     project.local;
   root            /media/Storage/project/web;

   location / {
       # try to serve file directly, fallback to app.php
       try_files $uri /app.php$is_args$args;
   }

   location ~ "^(.+\.php)($|/)" {
       fastcgi_split_path_info ^(.+\.php)(.*)$;

       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param SCRIPT_NAME $fastcgi_script_name;
       fastcgi_param PATH_INFO $fastcgi_path_info;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       include fastcgi_params;
   }

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

server {
   listen 443 ssl;

   server_name     project.local;
   root            /media/Storage/project/web;

   ssl_certificate     /etc/nginx/ssl/localhost.crt;
   ssl_certificate_key /etc/nginx/ssl/localhost.key;

   location / {
       # try to serve file directly, fallback to app.php
       try_files $uri /app.php$is_args$args;
   }

   location ~ "^(.+\.php)($|/)" {
       fastcgi_split_path_info ^(.+\.php)(.*)$; 
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       fastcgi_param SCRIPT_NAME $fastcgi_script_name;
       fastcgi_param PATH_INFO $fastcgi_path_info;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
       include fastcgi_params;
   }

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

然後按照 IRC 上的建議重新啟動 nginx 和 php5-fpm,但是,出現了著名的Input file not specified錯誤。

**編輯2:**這是error.log

2014/06/25 22:11:45

$$ error $$11922#0:* 3 FastCGI 在標準錯誤中發送:“無法打開主腳本:/media/Storage/project/web/app_dev.php(沒有這樣的文件或目錄)”同時從上游讀取響應標頭,客戶端:127.0.0.1 ,伺服器:project.local,請求:“GET /app_dev.php HTTP/1.1”,上游:“fastcgi://unix:/var/run/php5-fpm.sock:”,主機:“project.local”

**編輯 3:**這是 access.log

127.0.0.1 - -

$$ 25/Jun/2014:22:11:45 +0200 $$“GET /app_dev.php HTTP/1.1” 404 56 “-” “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/34.0.1847.116 Chrome/34.0.1847.116 Safari/537.36”

symfony2 日誌文件是空的。我不認為 symfony 曾經被處決過。

**編輯 4:**我已經按照這個 SO anwser將我的配置更改為:

server {
   listen 80;

   server_name     project.local;
   root            /media/Storage/project/web;

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

   location / {
       index app.php;
       if (-f $request_filename) {
         break;
       }
       rewrite ^(.*)$ /app.php last;
   }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~ (app|app_dev).php {
       include fastcgi_params;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_param PATH_INFO $fastcgi_path_info;
       fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
       fastcgi_param HTTPS off;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
   }
}

server {
   listen 443 ssl;

   server_name     project.local;
   root            /media/Storage/project/web;

   ssl_certificate     /etc/nginx/ssl/localhost.crt;
   ssl_certificate_key /etc/nginx/ssl/localhost.key;

   error_log /var/log/nginx/ssl_error.log;
   access_log /var/log/nginx/ssl_access.log;

   location / {
       index app.php;
       if (-f $request_filename) {
           break;
       }
       rewrite ^(.*)$ /app.php last;
   }

   # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
   #
   location ~ (app|app_dev).php {
       include fastcgi_params;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_param PATH_INFO $fastcgi_path_info;
       fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
       fastcgi_param HTTPS on;
       fastcgi_pass unix:/var/run/php5-fpm.sock;
   }
}

現在我遇到了Access denied.錯誤。是的,有些東西改變了:)。噓,還沒有工作:(。

**編輯5:**我已經chown loostro:www-data /media/Storage/project -R然後chmod g+s /media/Storage/project -R,仍然得到Access denied

編輯 6/var/run/php5-fpm.sock權限是:

srw-rw---- 1 www-data www-data 0 cze 26 11:03 php5-fpm.sock

/etc/php5/fpm/pool.d/www.conf文件是預設文件(適用於 Ubuntu 14.04 發行版),除了我在教程後未註釋的這些更改:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

摘要:我的問題是錯誤的文件權限。nginx的官方 symfony2配置是可以的。我將文件移動到 NTFS 儲存驅動器,並且在移動文件權限期間似乎已更改。

  • chown loostro:www-data -R /media/Storage/project- 將所有者更改為 loostro 並將組更改為 www-data(loostro 是我開發應用程序的使用者,php5-fpm 作為 www-data 執行),遞歸
  • chmod 755 -R /media/Storage/project- 將項目的文件權限更改為 rwx (owner) rx (group, other),遞歸
  • cd /media/Storage/project進入我的項目目錄
  • chmod 775 -R app/cache app/logs web/uploads private/uploads- 以遞歸方式更改可上傳目錄 rwx(所有者、組)和 rx(其他)的文件權限
  • chmod g+s -R /media/Storage/project- 遞歸地保留目錄中新創建文件的使用者和組和文件權限

引用自:https://serverfault.com/questions/607927