2014-02-20 52 views
6

使用nginx web服務器和php。 nginx正在工作,我看到'歡迎來到nginx!'但是當我嘗試訪問一個php頁面時,我得到了'拒絕訪問'。我也安裝了php-fastcgi。在nginx和php上拒絕訪問

這裏是我的nginx的默認的conf:

# redirect server error pages to the static page /50x.html 
# 
error_page 500 502 503 504 /50x.html; 
location = /50x.html { 
    root /usr/share/nginx/html; 
} 

# proxy the PHP scripts to Apache listening on 127.0.0.1:80 
# 
#location ~ \.php$ { 
# proxy_pass http://127.0.0.1; 
#} 

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 
# 
location ~ \.php$ { 
    root /usr/share/nginx/html; 
    fastcgi_index index.php; 
    fastcgi_pass unix:/var/run/php5-fpm.sock; 
    include fastcgi_params; 
    # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 
    fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
    include  fastcgi_params; 
} 

# deny access to .htaccess files, if Apache's document root 
# concurs with nginx's one 
# 
location ~ /\.ht { 
    deny all; 
} 

我/etc/php5/fpm/php.ini

活化狀態在/etc/php-fpm.d/www.conf security.limit_extensions = .php .php3 .php4 .php5 .htmllisten = /var/run/php5-fpm.sockcgi.fix_pathinfo = 0

我重新啓動了nginx和php5-fpm。

感謝您的幫助。

回答

7

做這樣的,你有你的次要位置

location/{ 

     try_files $uri $uri/ =404; 

     root /path/to/your/www; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     include fastcgi_params; 

     fastcgi_param PATH_INFO   $fastcgi_path_info; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 

    } 

這兩個參數是魔法醬:

fastcgi_param PATH_INFO   $fastcgi_path_info; 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
+0

太棒了!這就是訣竅!謝謝! – raice

0

我知道有些人可能出現的情況時,nginx的和PHP無法訪問文件:

  1. 最有可能php-fpm過程由不具有相應.php文件讀取權限的用戶運行。 這給普通錯誤Access denied.

  2. nginx過程不會對包含該網站的文件root目錄讀取和遍歷權限。 這給出了403 Forbidden錯誤。

  3. php-fpm進程無法遍歷目錄的絕對路徑爲root。 這給出File not found錯誤。

由於作者提到,該問題只在訪問php文件時出現,所以我會說第一種情況適用於此處。

我認爲案例是nginx作爲一個用戶運行,php-fpm作爲另一個用戶,只有php-fpm用戶被遺忘給讀取權限。

+0

我/etc/php-fpm.d/www改變用戶和組.conf文件和網絡文件夾的所有者更改爲相同的用戶,但不起作用,我給出相同的結果。我發送nginx錯誤日誌(2014/02/20 17:19:39 [error] 2989#0:* 6 stderr發送FastCGI:「腳本'/ usr/share/nginx/html'的訪問已被拒絕(請參閱在讀取來自上游的響應報頭時,客戶端:10.10.125.16,server:localhost,request:「GET /test.php HTTP/1.1」,上游:「fastcgi:// unix:/ var/run/php5 -fpm.sock:「,host:」10.254.10.19「) – Hasan

+0

如果您禁用security.limit_extensions,它會發生嗎? 你是否也重新啓動了php-fpm? – liepumartins