2016-09-15 31 views
0

這是我的地盤 - 可用/ myServer上,正確鏈接到網站啓用/ myServer上:PHP7 FPM和Nginx的 - 獲得404

server { 
    listen 80; 
    server_name sub.domain.net; 

    location/{ 
      root /home/tyrion/saveup-compute; 
      index index.php index.html index.htm 
      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/www; 
    } 

     # pass the PHP scripts to FastCGI server listening on the php-fpm socket 
     location ~ \.php$ { 
      try_files $uri =404; 
      fastcgi_pass unix:/var/run/php7.0-fpm.sock; 
      fastcgi_index index.php; 
      fastcgi_param SCRIPT_FILENAME /home/username/myServer/$fastcgi_script_name; 
      include fastcgi_params; 
     } 
} 

我去到PHP FPM文件,並設置660和www數據和設置在我的目錄中輸入www-data:myuser。不過,我不能提供一個單一的PHP文件。 (HTML工作得很好)。

Nginx的訪問日誌:

MYIP - - [15/Sep/2016:23:09:13 +0000] "GET /info.php HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" 

Nginx的錯誤日誌:

2016/09/15 23:09:13 [error] 38415#38415: *2 open() "/home/myUser/myServer/404.html" failed (2: No such file or directory), client: 62.155.132.7, server: sub.somain.net, request: "GET /info.php HTTP/1.1", host: "sub.somain.net" 

php7.0-fpm.log:

[15-Sep-2016 21:54:49] NOTICE: fpm is running, pid 35917 
[15-Sep-2016 21:54:49] NOTICE: ready to handle connections 
[15-Sep-2016 21:54:49] NOTICE: systemd monitor interval set to 10000ms 
[15-Sep-2016 22:56:58] NOTICE: Terminating ... 
[15-Sep-2016 22:56:58] NOTICE: exiting, bye-bye! 
[15-Sep-2016 22:56:58] NOTICE: configuration file /etc/php/7.0/fpm/php-fpm.conf test is successful 

[15-Sep-2016 22:56:58] NOTICE: fpm is running, pid 37925 
[15-Sep-2016 22:56:58] NOTICE: ready to handle connections 
[15-Sep-2016 22:56:58] NOTICE: systemd monitor interval set to 10000ms 
+1

你應該嘗試'try_files $ uri $ uri//index.php?$ args;'或者這些行 – Perspective

+1

你確定你的文件位於'/ home/tyrion/saveup-compute'嗎?你在使用任何類型的'mod_rewrite'路由嗎? – Blake

+0

沒有mod_rewrite,但也許我應該考慮它。 – nottinhill

回答

0

似乎這裏是兩個問題。

第一個問題是location ~ \.php$塊缺少root指令意味着try_files $uri =404;語句將始終因404響應而失敗。

將404響應重定向到/404.html並從錯誤日誌中,似乎沒有名爲/home/tyrion/saveup-compute/404.html的文件。

如果要將HTML文件和PHP文件放在不同的目錄層次結構中,則需要將root指令添加到location ~ \.php$塊,以便try_files指令可以正常工作。

如果你把你的HTML文件和PHP文件放在同一目錄層次,那麼你應該移動root /home/tyrion/saveup-compute;語句轉換成server塊,所以,同樣的值由所有location塊的繼承。

你也應該改變的SCRIPT_FILENAME定義,以便它使用正確的root值(包括fastcgi_params第一,以避免它靜靜地覆蓋你的定義):

include fastcgi_params; 
fastcgi_param SCRIPT_FILENAME $request_filename; 

更多見this document